Nginx common commands

Ln -s/usr/local/nginx/sbin/nginx/usr/local/bin/into the CD/usr/local/sbin directory nginx/sbin/nginx start:. / nginx

Stop:./nginx -s stop Forces the process to be killed

Exit:./nginx -s quit Stop the nginx process after it is processed

Query: ps aux | grep nginx

Restart: stop./nginx -s quit before starting./nginx

Reload the configuration file:./nginx -s reload

Nginx configuration HTTPS

The configuration reference: blog.csdn.net/smartdt/art…

  1. The default nginx configuration does not have the http_SSL_modules module and needs to be reconfigured

Run commands to check whether the configuration is complete

/usr/local/nginx/sbin/nginx -V

  1. Configuration http_ssl_mudules

2.1 Going to the Nginx Directory (downloaded file package directory, not installation directory)

./configure –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module

2.2 build

make

3. Configure the nginx configuration file

Download the HTTPS certificate configured for nginx

The cert directory is in the conf directory

server { listen 443 default ssl; server_name localhost; ssl on; root html; index index.html index.htm; ssl_certificate cert/xiaoyueyue.crt; ssl_certificate_key cert/xiaoyueyue.key;

ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:! NULL:! aNULL:! MD5:! ADH:! RC4; Ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { root html; index index.html index.htm; }Copy the code

}

Nginx alias configuration details

Alias is an alias for location. The path specified by alias is the actual path specified by alias regardless of location. (Location is ignored) Otherwise, the file cannot be accessed (this is the case when accessing the alias subdirectory)

The following is an example

  • 1. When visiting xxxx.com/pic/1.jpg… html/images/1.jpg
    1. When accessing xxx.com/pic/my/1.jp…

After location is matched to PIC, the path following PIC is merged into the Alias directory for access

location /pic/ { alias html/images/ }

Configuration Precautions

For example, / PIC / : can end with a /, / PIC: can end with a /, and/PIC: can end with a /. If the “/” is not used, the path can be accessed with or without a “/”. If the “/” follows the path, the path must be followed by “/”. Alias = / / location = / location = / location = / location = / location = / location = / location = / location = / The path configured for alias ends with a slash (/)

If the alias configuration path points to a specific file, when accessing xxx.com/pic you can directly access…

location /pic {
  alias html/images/1.jpg
}
Copy the code

Nginx root configuration

The directory configured in root is the upper directory of the path directory in location. The directory specified by path must exist in the root directory; otherwise, files cannot be accessed

The specific configuration access example is as follows:

When visiting xx.com/pic/1.jpg… The PIC directory exists under the HTML directory

location /pic {
  root html;
}
Copy the code

Configuration Precautions

  • In the root directory configuration, whether the path directory matching location is followed by a slash (/) or not does not affect access
  • Generally, the root directory is set to location /, and the alias directory is set to location /path

Proxy_pass configuration in nginx

Proxy_pass is mainly used for proxy forwarding in Nginx

The specific configuration is as follows: visit xxx.com/test/index….

1. Whose access server specific address to http://127.0.0.1/index.html

Location /test/ {proxy_pass http://127.0.0.1/; }Copy the code
  1. The access server specific address to http://127.0.0.1/test/index.html
Location /test/ {proxy_pass http://127.0.0.1; }Copy the code
  1. The access server specific address to http://127.0.0.1/my/index.html
Location /test/ {proxy_pass http://127.0.0.1/my/; }Copy the code
  1. Its access server specific address for http://127.0.0.1/myindex.html, this kind of situation is special, if proxy_pass behind the path of the file directory and no end “/”
Location /test/ {proxy_pass http://127.0.0.1/my; }Copy the code

Configuration Precautions

  • When the proxy_pass path ends with “/”, it indicates the absolute path. The real proxy path does not concatenate the path value of location
  • If the proxy_pass path does not end with a slash (/), it indicates a relative path. The real proxy path will concatenate the path value of location
  • The path in location should end with a slash (/). To facilitate configuration and standardization, it is recommended that all proxy_pass paths end with a slash (/)
  • If the path in location ends in a “/ “but the path in proxy_pass does not end in a “/” and has a directory, the access may fail

Re matching configuration in Nginx

Nginx matching rules can be divided into four types

  • location = /path {}
  • lcoation /path {}
  • location ^~ /path {}
  • location ~* /path {}

1. Accurate match with general directory url can use this method, if with/access, will not go to find other rules

location = / {
  root html;
}
Copy the code
  1. Path matches, and if it matches /path, it looks for other regular matching rules
location /path {
}
Copy the code
  1. A path with ^~ matches. If it matches /path, the search is stopped and no other re is matched
location ^~ /path {
}
Copy the code
  1. Regular match
// case sensitive, http://xxx.com/pic location ~ / PIC {} // case insensitive, Can match to the http://xxx.com/pic, also can match to the http://xxx.com/pic location ~ * / PIC {} / / the match ends in a character location ~ * \. (JPG | PNG) ${}Copy the code
  1. Nginx internal jump
location /index/ {
  error_page 404 @auto_index
}
loaction @auto_index {}
Copy the code

Static resource caching configuration in Nginx

Nginx configures reverse proxy

The function of reverse proxy is to allow users to access the Nginx proxy server first, and then access the internal network, which can be optimized as an intermediate proxy layer

HTTP {upstream srrapp {server 127.0.0.1:3000; } server { listen 8899; server_name localhost; location / { proxy_pass http://srrapp } } }Copy the code

Nginx configures the history route access

The front end uses the browerHistory route to package and publish the Nginx configuration

New server Server {listen 8899; server_name localhost;

location / { root html/high-speed; try_files $uri $uri/ /index.html = 404; // configure # index index.html index.htm; }Copy the code

}

Restart nginx./nginx -s reload

Nginx configures load balancing

Upstream module: when accessing a route, it can forward the request to different servers to process the request to achieve load balancing. Load rules can be configured. By default, the weight is set to 4 as follows: four requests are made every 8003 and 8004 requests are made once

upstream backend { server xxx:8003 weight=4; server xxx:8004; } server { listen 8001; location /login { proxy_pass http://backend; }} server {listen 8003; location /login { alias html/test/; index index.html index.htm; } } server { listen 8004; location /login { alias html/ index index.html index.htm } }Copy the code

Location configuration rules in nginx

Priority can be divided into three categories: accurate matching > ordinary matching > regular matching

1. Accurate match

location = /patt {
}
Copy the code
  1. Ordinary match
Location /patt {} location /patt/xx {}Copy the code
  1. Regular match
Location ~ patt {}Copy the code