Location: location –> route.

Nginx server is very core configuration, general Nginx operation personnel in the modification of nginx configuration, most of the configuration is also around the location of the modification.

Let’s look at a simple location configuration:

location / {
     root   home/;
     index  index.html;
}
Copy the code

This configuration means that any path accessing the nginx server is redirected to the index.html page in the home directory.

The following details the location path matching rules. Location matching is divided into three matching modes.

= = = = = = = =

/login/demo. HTML = /login/demo.html
location = /login/demo.html {
       *******
}
Copy the code

2. Matches ~ or ~*. The former is case sensitive and the latter is case insensitive

location ^~ /images/ {
# Match any query that starts with /images/ and stop the search. Any regular expressions will not be tested.
}
 
location ~* .(gif|jpg|jpeg)$ {
Match any request that ends in.gif,.jpg, or.jpeg
}
 
Copy the code

3. Generally unsigned matching. Even if unsigned matching is in progress, it will not break and will continue to match downward.

To summarize: = strict match. If the query matches, the search is stopped and the request is processed immediately. ~ is case-sensitive matching (regular expressions are available)! ~ is case sensitive and does not match ~* is case insensitive. ~* is case insensitive and does not match ^~ If this prefix is used for a regular string, then nginx is told not to test the regular expression if the path matches.


Proxy_pass Reverse proxy

1: When we have cross-domain problems and the client cannot support CORS, the best solution is to let the server do the proxy. Open a route on the nginx configuration of the front-end page server, and then use proxy to request resources under another domain name. 2: After the separation of front and background, after the independent development of the front end, proxy_pass can be used to reverse proxy to the background service, or proxy can be used to reverse proxy if the server deployment address is inconvenient to expose.

Simple examples:

location /login {
     proxy_pass   http://www.sohu.com/
} 
Copy the code

When we visit http://192.168.0.101:8080/login will jump straight to the sohu’s front page. Note The current access address is the proxy address of Sohu.

Note that the address after proxy does not have a slash:

http://192.168.0.101:8080/login/index.html
There is a slash: absolute address, which eventually jumps to:www.sohu.com/index.htmlNo slash: relative address, eventually jump to:www.sohu.com/login/index…


Rewrite has 5 command patterns

Rewrite is all about modifying urIs, but be aware that rewrite has the side effect of rematching locations. Because proxy_pass is processed later than location, break is needed to prevent rewrite from missing proxy_pass in the next location match.

1, break; As follows:

This directive says that if the /login match is successful, the demo.html file will be looked up directly in the home path
Then jump to demo.html. Note that this is an internal hop, so the browser's url will not change, and it will still end with /login.
location  /login {
       rewrite  ^/  /demo.html  break;
       root  home/;
}
Copy the code

2, redirect; The following

# is the same as break, but this means an external jump to the demo.html page, but the browser address will automatically change to demo.html
location  /login {
       rewrite  ^/  /demo.html  redirect;
       root  home/;
}
Copy the code

3, permanent; Similar to redirect.

4, the last; If the last modifier is used, nginx matches the /demo.html address with the address of another location, finds the matching address, and continues. /demo.html location, and then internal jump to /demo.html page, as follows:

location  /login {
       rewrite  ^/  /demo.html  last;
       root  home/;
}
 
location  /demo.html {
       rewrite  ^/  /demo.html  break;
       root  home/;
}
Copy the code

No modification, no modification. You can see that there is no embellishment behind this rewrite. When you match a location without any decorations, you don’t stop. You keep matching the location below. Use the last matched until the last matched. The following

location  /login {
       rewrite  ^/  /demo.html ;
       root  home/;
}
Copy the code

Upstream: Load configuration

Upstream implements load configuration policies. Nginx provides polling/weights /ip_hash. Third-party policies can be used for special requirements (rarely used).

upstream test{
    server 192.168. 0101.:8081;
    server 192.168. 0102.:8081;
}
 
upstream test1 {
    server 192.168. 0101.:8081 weight=2;
    server 192.168. 0102.:8081 weight=1;
}
 
upstream test2 {
    ip_hash
    server 192.168. 0101.:8081;
    server 192.168. 0102.:8081;
}
 
server{
   listen   80;
   server_name  localhost;
 
   location /login {
       proxy_pass   http://test/}}Copy the code

When accessing: http://localhost/login, nginx will be at server 192.168.0.101:8081; Server 192.168.0.102:8081 Polling access between the two services.

Upstream test1: Connect to the above service twice and connect to the below service once

Upstream test2: uses the hash value of the client’s IP address to separate services accessed by the same client.


Reference: blog.csdn.net/zhanglei082… www.cnblogs.com/lianxuan176…