Url Matching Rule

location [=|~|~*|^~|@] /uri/ {
  ...
} 
Copy the code
  • =: indicates that the following URL is matched exactly
  • ~: indicates a regular match, but is case sensitive
  • ~ *: indicates a regular match and is case insensitive
  • ^ ~: indicates that common characters are matched. If this option is matched, only this option is matched. It is generally used to match directories
  • @: “@” defines a named location that is used for internal orientations, such as error_page

The matching order of the above matching rules is as follows:

  1. Directives with the = prefix match the query exactly. If found, stop searching;
  2. All remaining regular strings, longest match. If the match uses the prefix ^~, the search stops;
  3. Regular expressions, the order defined in the configuration file;
  4. If rule 3 produces a match, the result is used. Otherwise, use the result of rule 2.

Destination address processing rule

Once the URI is matched, the next step is to proxy to the target service address.

upstream api_server {
  server 10.0.101.62:8081;
  server 10.0.101.61:8082;
}

location / {
        rewrite^ (. *) $ http://10.0.101.62:8000/my-moduleThe $1 break;
}

location^ ~ /my-module/ {
    root   /data/my-module/dist;
    rewrite ^/my-module/(.*)$  /The $1 break;
    index  index.html index.htm;
}

location /my-module/api {
    proxy_pass  http://api_server;
    proxy_set_header Host $host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header  your-custome-header    "myHeader";
    proxy_set_header X-NginX-Proxy true;
}
Copy the code

In the above configuration, the default access/is redirected to /my-module, and then returns static files such as HTML under /data/my-module/dist.

Accessing /my-module/ API proxies to our API server address, which is a default round-robin load balancing configuration.

Here is the log for accessing localhost. The home page was redirected twice.

Request URL: http:/ / 10.0.101.62:8000 /
Request Method: GET
Status Code: 302 Moved Temporarily
Location: http:/ / 10.0.101.62:8000 / flash /

Request URL: http:/ / 10.0.101.62:8000 / flash /
Request Method: GET
Status Code: 302 Moved Temporarily
Location: http:/ / 10.0.101.62:8000 / flash/index. The HTML

Request URL: http:/ / 10.0.101.62:8000 / flash/index. The HTML
Request Method: GET
Status Code: 304 Not Modified
Copy the code

Difference between alias and root

The actual file access path of root will be combined with the path in the URL

Alias Indicates the actual file access path. The path in the URL is not concatenated

The following is an example:

location^ ~ /sta/ {  
   alias /usr/local/nginx/html/static/;  
}
Copy the code

Request: the actual visit: http://test.com/sta/sta1.html/usr/local/nginx/HTML/static/sta1. HTML file

location^ ~ /tea/ {  
   root /usr/local/nginx/html/;  
}
Copy the code

Request: http://test.com/tea/tea1.html

Actual access: / usr/local/nginx/HTML/tea/tea1. HTML file

Obviously, the second redirect is not needed. The intention is to access static HTML files in the corresponding directory when accessing /flash/. But because root splices flash and cannot find the corresponding file, rewrite the URL to remove the flash module prefix and use rewrite, which will return a 302 redirect.

Next, we change root to alias

location^ ~ /flash/ {
    alias   /data/flash/dist/;
    #rewrite ^/flash/(.*)$ /$1 break;
    index  index.html index.htm;
}
Copy the code

Redirect directly once and return to HTML

Request URL: http:/ / 10.0.101.62:8000 /
Request Method: GET
Status Code: 302 Moved Temporarily

Request URL: http:/ / 10.0.101.62:8000 / flash /
Request Method: GET
Status Code: 200 OK (from disk cache)
Copy the code

The difference between the last and break keywords

Only break is used, meaning that the match does not continue after this point.

Difference between permanent and redirect

  • Rewrite… Permanent Indicates permanent redirection. The status code in the request log is 301
  • Rewrite… Redirect Temporary redirection. The status code in the request log is 302

A configuration scheme for converting port 80 to 443, that is, HTTP to HTTPS, is as follows

server {
    listen 80;
    server_name demo.com;
    rewrite^ (. *) $ https://${server_name}The $1 permanent; 
}
Copy the code

Returns 301 permanently redirects to the corresponding HTTPS:

Request URL: http://demo.com/flash/index.html
Request Method: GET
Status Code: 301 Moved Permanently
Location: https://demo/flash/index.html
Copy the code

Some usage scenarios

The demo above is pretty much my usual proxy configuration with a separate front and back end. Here are some of the scenarios encountered.

Configure a static file download service, our following software will often see the index/page.

server {
        listen       8888;        # port
        server_name  _;   # service name

        charset utf-8,gbk;
        root    /data/download;  # display the root index directory
        autoindex on;             Enable the index function
        autoindex_exact_size off; # turn off the exact size of the file (bytes) and display only the approximate size (KB, MB, GB)
        autoindex_localtime on;   Display local time instead of GMT time
}
Copy the code

Configure HTTP redirection to HTTPS

server {
    listen 80;
    server_name demo.com;
    rewrite^ (. *) $ https://${server_name}The $1 permanent; 
}
                   
server {
    listen       443;
    server_name  demo.com;
    charset utf-8;
    location / {
       alias   /data/web;
       indexindex.html index.htm; }}Copy the code

Configure the static front-end page

location / {
    alias   /data/web;
    index  index.html index.htm;
}
Copy the code

Configure the reverse proxy, for example we visit demo.com/api/aaa/bbb… , only the domain name is switched, and the URI is the same.

upstream api_server {
  server 10.0.101.62:8081;
  server 10.0.101.61:8082;
}
location /api {
    proxy_pass  http://api_server;
    proxy_set_header Host $host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header  your-custome-header    "myHeader";
    proxy_set_header X-NginX-Proxy true;
}
Copy the code

When configuring the reverse proxy, remove the prefix. For example, our service demo.com/users/aaa/b… , we want to delegate to users.com/aaa/bbb, i.e. switch… The ending /.

location ^~/users/ {
    proxy_set_header Host $host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://users.com/;
}
Copy the code

In reverse proxy, you want to customize the change URI. Rewrite regular changes using rewrite.

$1 indicates the content of the string matched by the re.
location^ ~ /flash/ {
    root   /data/flash/dist/;
    rewrite ^/flash/(.*)$  /The $1 break;
    index  index.html index.htm;
}

Change the URI and re-proxy to the new address
location ^~/order/ {
    proxy_set_header Host $host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;

    rewrite ^/order/(.*)$ /The $1 break;
    proxy_pass http://order;
}
Copy the code

Proxies across domains, such as Bing Daily map, do not support ajax to obtain the image address, we can write a support interface.

http://101.200.218.760/proxy/bing/HPImageArchive.aspx?format=js&idx=0&n=1

The proxy object is:

Cn.bing.com/HPImageArch…

location ^~/proxy/bing/ {
    add_header 'Access-Control-Allow-Origin' 'http://localhost:8088';
    add_header 'Cache-Control' 'public, max-age=604800';

    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

    rewrite ^/proxy/bing/(.*)$ /The $1 break;
    proxy_pass https://cn.bing.com/; 
}
Copy the code

source

  • www.cnblogs.com/duhuo/p/832…
  • www.cnblogs.com/woshimrf/p/…