Nginx installation

The installation

The tar ZXVF nginx - 1.2.9. Tar. Gz# extract nginx
cdNginx 1.2.9 -# enter directory
./configure --prefix=/opt/soft/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module   Configure the installation module
make install   # installation
Copy the code

–prefix: specifies the installation directory. The default installation directory is /usr/local/nginx.

–with-http_ssl_module: installs the HTTPS service module

Start the

/opt/soft/nginx/sbin/nginx
/opt/soft/nginx/sbin/nginx -s stop   # fast shutdown
/opt/soft/nginx/sbin/nginx -s quit   # graceful shutdown
/opt/soft/nginx/sbin/nginx -s reload   # reloading the configuration file
/opt/soft/nginx/sbin/nginx -s reopen   # reopening the log files
Copy the code

Set the boot automatically:

echo "/opt/soft/nginx/sbin/nginx -c /opt/soft/nginx/conf/nginx.conf" >> /etc/rc.local
Copy the code

Nignx Configures virtual hosts, reverse proxies, and load balancers

Virtual host

Configure listen and server_name for the server module

Based on the domain name

server { listen 80; server_name test.a.com; Location / {proxy_pass http://192.168.0.1;# reverse proxy to other sites} } server { listen 80; server_name test.b.com; Location / {proxy_pass http://192.168.0.2;# reverse proxy to other sites}}Copy the code

Note: Configuration file download server

server {
	listen       80;
	server_name  file.download.com;
	charset utf-8;
	location ~ ^/(.*)$ {
		add_header Content-Disposition "attachment; filename=The $1";   # set the header
		alias "C:/Robot_Download/The $1";   The local location of the file}}Copy the code

Based on the port

server {
	listen 80;
	server_name localhost;
	alias /data/html/index.html;   Static resources can also be pointed to by root, location, etc
}
server {
	listen 81;
	server_name localhost;
	root /data/html/index.html;   Static resources can also be pointed to using alias, location, etc
}
Copy the code

Based on the IP

Server {listen 100.100.100.100:80; server_name localhost; location / {alias /data/html/index.html;   Static resources can also be pointed to as alias, root, etc}} server {listen 100.100.100.101:80; server_name localhost; location / {alias /data/html/index.html;   Static resources can also be pointed to as alias, root, etc}}Copy the code

The reverse proxy

Configure proxy_pass for the location module

server { listen 80; server_name test.b.com; Location / {proxy_pass http://192.168.0.2;Reverse proxy to another application server or Web server}}Copy the code

Load balancing

Configure proxy_pass for upstream and location modules

upstream tomcat_server_pool{ ip_hash; Server 127.0.0.1:8090 weight = 10;# Set the access weight, the higher the weight, the easier to accessServer 127.0.0.1:8100 weight = 10; Server 127.0.0.1:8110 weight = 7; } server { listen 80; server_name test.b.com; location / { proxy_pass http://tomcat_server_pool;Reverse proxy to other server collections}}Copy the code

** IP_hash: ** Resolve session problems using load balancing based on the IP_hash policy. Each request is allocated according to the hash result of the access IP, so that each visitor has a fixed access to the back-end server, which can better solve the session problem.

Location mapping rule

Alias and root

location  /svn/ {
	root /data/ftp/;
	autoindex on;
}
Copy the code

Access 127.0.0.1/ SVN /a.jpg: /data/ FTP/SVN /a.jpg is displayed

location  /svn/ {
	alias /data/ftp/;
	autoindex on;
}
Copy the code

Access 127.0.0.1/ SVN /a.jpg: /data/ FTP /a.jpg will be entered

Whether the proxy_pass URL has a/distinction

Note: both alias and root urls are preceded by a slash

Location /proxy/ {proxy_pass http://127.0.0.1/; }Copy the code

If you access 127.0.0.1/proxy/a.jpg, the system requests http://127.0.0.1/a.jpg

Location /proxy/ {proxy_pass http://127.0.0.1; }Copy the code

Access: 127.0.0.1 / proxy/a. pg: will request to: http://127.0.0.1/proxy/a.jpg

Location/XXX/is different from location ^~ / XXX /

location = / {   # indicates matching access to the root directory
	root   html;   # HTML in the current installation directory, / HTML in the server root directory
	index  index.html index.htm;
}
location /svn/ {   # indicates the matched IP address :port/ SVN /
	root /data/;
	autoindex on;
}
}
location ^~ /svn/ {   # indicates that anything that contains SVN/will be matched
	root /data/;
	autoindex on;
}
Copy the code

Location/XXX/indicates the matching IP address :port/ XXX. Note the following:

  1. Can match to test.com/xxx/home.jpg;
  2. Can’t match to the test.com/folder/xxx/home.jpg;
  3. If you need to match to the latter, change to:location /folder/xxx/

Science:

The default is location = / (exact match), and another is location/(fuzzy match).

The difference between the two is that a fuzzy match will continue to match even if it matches, while an exact match will not.

Example: in the example above, if location = / is replaced by location /, request 112.74.55.239/ SVN / :

  1. First will match/, the requested physical path becomes:/usr/local/nginx/html
  2. Continue to match/svn/, the actual access physical path becomes:/usr/local/nginx/html/data/svn/

rewrite

location ~ \.php${
	rewirte "^/php/(.*)$" http://localhost:8090/The $1
}
Copy the code
  • willlocalhost/php/test.phpRedirected tolocalhost:8090/test.php. If the regular expression (regex) matches the request URI (request URI), this URI is replaced by replacement
  • If the regular expression (regex) contains “} “or”; Character, which requires single or double quotation marks to enclose the regular expression
  • If there is a new request parameter in the replacement string, the previous parameter is appended to it; to avoid this, add “? “to the replacement string. Rewrite ^/users/(.*)$/show? user=$1? last; =

The optional flag parameters are as follows:

  1. last
  • Completes the current request processing and re-matches the location with the replaced URI;
  • After rewriting, a new request is made to the server module, matching the location.
  • Nginx returns 500 errors if the rematch loop is repeated more than 10 times;
  • Return 302 HTTP status code;
  • The redirected URL is displayed in the address bar of the browser
  1. break
  • End the current request processing, use the current resource, do not execute the rest of the statement in location;
  • Return 302 HTTP status code;
  • The redirected URL is displayed in the address bar of the browser
  1. redirect
  • Temporary jump, return 302 HTTP status code;
  • The redirected URL is displayed in the address bar of the browser
  1. permanent
  • Permanent jump, return 301 HTTP status code;
  • The redirected URL is displayed in the address bar of the browser

Commonly used configuration

The back-end can obtain the real client IP address, not the IP address of the proxy

proxy_set_header Host $http_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;
Copy the code

Request.getscheme () cannot obtain the real protocol

proxy_set_header X-Forwarded-Proto $scheme;
Copy the code

Websocket configuration

map $http_upgrade $connection_upgrade {
default upgrade;
	' ' close;
}
Copy the code