nginx related (load balancing, reverse proxy server front-end cross-domain problem solving, etc.)

1. Installation depends
yum the install GCC
yum the install PCRE-devel
yum the install zlib zlib-devel
yum the install OpenSSL OpenSSL-devel

2. download extract Nginx
CD / usr / local
mkdir Nginx
CD Nginx
wget http://nginx.org/download/nginx -1.13.7.tar.gz (download the tar package)
tar -xvf nginx-1.13.7.tar.gz (extracting file)

3. install nginx
enter nginx directory
cd /usr/local/nginx-1.13.7
execute the command
. / configure
make command
make
run make install command
make install

Configuring nginx
CD to nginx configuration files, configuration information corresponding to the data, the main server is configured to upstream and

5. Example
Redirection:
the listen 80; 80 # listening port
server_name www.dbspread.com; # domain
#rewrite rule
index the index.jsp index.html index.htm;
the root / usr / local / Nginx / HTML; # define server The default location of the site root
# redirection
IF ($ Host = 'www.dbspread.com'!) {
  the rewrite ^ / http://www.dbspread.com/$1 permanent $ (*.); #permanent 301 represents permanent weight orientation mark
}

Backup system:
upstream neiwei_servers {
  Server 127.0.0.1:8080;
  Server 127.0.0.1:8091 Backup;
}

LOCATION / {waiwei

  client_max_body_size 100m;
  proxy_connect_timeout 4;
  proxy_send_timeout 1200;
  proxy_read_timeout 1200;
  root /root/web/waiwei/webapps;
  proxy_pass http://waiwei_servers;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header REMOTE-HOST $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

nginx防盗链:
server {
  listen 80;
  server_name www.dbspread.com *.dbspread.com;
  location ~* \.(rmvb|jpg|png|swf|flv)$ { #rmvb|jpg|png|swf|flv 表示对rmvb|jpg|png|swf|flv后缀的文件实行防盗链
    valid_referers none blocked www.dbspread.com; # expressed www.dbspread.com opened whitelist the domain, such as references to download / av123.rmvb in www.test.com of index.html, invalid
    root HTML / b;
    IF ($ invalid_referer) {# If the request is not requested from www.dbspread.com whitelist to direct or redirect the page to return 403.html 403
      #rewrite ^ / http://www.dbspread.com/403.html;
      return 403;
    }
  }

}

static and dynamic separation:
# static separation ~ match ending in * (to html | htm | gif | jpg | jpeg | bmp | png | ico | txt | js | css ending walk this), certainly not longer. the better, if you have 10,000 users online, all save a few months, cross-care system
LOCATION ~ * \ (HTML | HTM | GIF | JPG | jpeg | BMP | PNG | ico | TXT | JS | CSS).. $
{
  root / var / local / static; # nginx static resources stored on the machine-mounted
  #proxy_pass http://www.static.com; # static resources may be stored on a remote server
  expires 30d; within 30 days as long as the access # once he took from the cache
}

log:
# Logging levels are [debug | info | notice | warn | error | crit] error_log level is divided into debug, info, notice, warn, error, crit default crit, production environment with error
#crit record log a minimum, and debug record log up to
access_log /usr/local/logs/web2/access.log main;
error_log /usr/local/logs/web2/error.log Crit;

 

Guess you like

Origin www.cnblogs.com/smj1990/p/11771802.html