Summary of slash ('/') in nginx

1. Location

Each location of nginx is a matching directory. The strategy of nginx is: when an access request comes, the access address will be parsed and matched one by one from top to bottom. When matching, the strategy in the curly brackets of the corresponding location will be executed and the corresponding location will be processed according to the strategy. Request a response.
Based on access address: http://www.wandouduoduo.com/wddd/index.htmlFor example, nginx configuration is as follows:

location /wddd/ { 
  proxy_connect_timeout 18000; ##修改成半个小时 
  proxy_send_timeout 18000; 
  proxy_read_timeout 18000; 
  proxy_pass http://127.0.0.1:8080; 
}

When accessing, this location will be matched, thereby forwarding the request proxy to the Tomcat service on port 8080 of the local machine. After Tomcat responds, the information will be returned along the original path.

1.1 location does not have '/'

The request can fuzzy match all strings starting with string

1.2 location has '/'

Only the character itself can be matched exactly.

For example: Configuring location /wandou can match /wandoudouduo requests, or /wandou*/duoduo
, etc. All directories starting with wandou can be matched. The location /wandou/ must exactly match the request for the /wandou/ directory, and
cannot match the request for /wandouduoduo/ or /wandou*/duoduo.

2. URL automatically adds '/'

Sometimes the accessed address is required to end with /. If the user forgets to enter /, Nginx will automatically add /.
Demonstrate the problem through an example:

server {
	listen	80;
	server_name localhost;
	location / {
		root html;
		index index.html;
	}
}

To access the above resources, it is very simple. You only need to access it directly through http://192.168.200.133. There is no need to add / after the address, but if you modify the above configuration to the following:

server {
	listen	80;
	server_name localhost;
	location /frx {
		root html;
		index index.html;
	}
}

At this time, if we want to access the above resources, according to the above access method, we can access through http://192.168.200.133/frx/, but if there is no slash after the address, such as http://192.168.200.133/frx , there will be a problem with the page. If you do not add a slash, the Nginx server will automatically make a 301 redirect internally. The redirected address will have a command called server_name_in_redirect to determine the redirected address:

  • If this directive is on, the redirected address is: http://server_name/directory name/
  • If the directive is off, the redirected address is: http://domain name/directory name/ in the original URL

So take the address just now, visit http://192.168.200.133/frx. If you don’t add a slash, then follow the above rules:

  • If the instruction server_name_in_redirect is on, the 301 redirect address becomes http://localhost/frx/, the IP changes, and there is a problem with the address.
  • If the directive server_name_in_redirect is off, the 301 redirect address becomes http://192.168.200.133/frx/. This meets our expectations

Note that the server_name_in_redirect directive was on by default before Nginx version 0.8.48, and was later changed to off, so we do not need to consider this issue in this version now, but if it is a version before 0.8.48 and server_name_in_redirect is set to on, how do we Rewrite to solve this problem?
Solution
We can use the Rewrite function to automatically add a slash to URLs that do not have a slash at the end.

server {
	listen	80;
	server_name localhost;
	server_name_in_redirect on;
	location /frx {
		if (-d $request_filename){   # 如果请求的资源目录存在
			rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent; # $2 获取第二个括号的值:/
		}
	}
}

$1 is the value of the first bracket, $2 is the value of the second bracket.

3. proxy_pass

The http://192.168.199.27/frx/xu request will be sent here.

3.1 Do not add directories

Case localtion proxy_pass match
1 /frx http://192.168.199.27 /frx/xu
2 /frx/ http://192.168.199.27 /frx/xu
3 /frx http://192.168.199.27/ //xu
4 /frx/ http://192.168.199.27/ /xu

If '/' is added after proxy_pass, it means that the matching string between the request and location is removed.
If '/' is not added, all requests will be appended to the end of the address.

3.2 Add directory

Case localtion proxy_pass match
1 /frx http://192.168.199.27/bing /bing/xu
2 /frx/ http://192.168.199.27/bing /bingxu
3 /frx http://192.168.199.27/bing/ /bing//xu
4 /frx/ http://192.168.199.27/bing/ /bing/xu

In short, if there is a directory after proxy_pass, with or without '/', Nginx will remove the content matching location from the request path, and then splice the remaining string of the request path into proxy_pass to generate a new request path.

For example: the ip:port of proxy_pass is followed by a string, so the location /frx/ is removed from the original request path /frx/xu
and becomes xu, and then xu is spliced ​​to http://192.168.1.48/bing A new request is generated, so the request received by the other address is
/bingxu.

4. root and alias

Both of these instructions can specify the path to access resources, so what is the difference between the two?
for example

  1. Create an images directory under /usr/local/nginx/htmlthe directory and put a mv.pngpicture in the directory. Then enter the configuration file and add the following content:
location /images {
    root /usr/local/nginx/html;
}

The path to access the image is: http://192.168.91.200/images/mv.png
If it is root, the request is/usr/local/nginx/html/images/mv.png

location /images {
    alias /usr/local/nginx/html;
}

If it is alias, if you access the above address again, a 404 error will appear on the page. Check the error log and you will find that the address is incorrect, so you verified:

  • The processing result of root is: root path + location path . The location path includes matching subsequent requests, that is, including /mv.png

/usr/local/nginx/html/images/mv.png

  • The result of alias processing is: replace the location path with the alias path

/usr/local/nginx/html/mv.png
You need to change the path after alias to:

location /images {
    alias /usr/local/nginx/html/images;
}

If the location path ends with /, the alias must also end with /, and there is no requirement for root.
Modify the above configuration to:

location /images/ {
    alias /usr/local/nginx/html/images;
}

There will be problems with access. If you check the error log, the path is still wrong, so you need to add / after alias.

location /images/ {
    alias /usr/local/nginx/html/images/;
}

summary:

  • The processing result of root is: root path + location path
  • The result of alias processing is: replace the location path with the alias path
  • alias is the definition of a directory alias, and root is the meaning of the top-level directory.
  • If the location path ends with /, the alias must also end with /, and there is no requirement for root.
    • alias does not support location =

5. Examples

Access via nginx127.0.0.1/api/test

  1. Neither location nor proxy_pass add '/'
location /api {
    proxy_pass http://127.0.0.1:8888;
}

The actual access address is127.0.0.1:8888/api/test

  1. location plus proxy_pass but not
location /api/ {
    proxy_pass http://127.0.0.1:8888;
}

The actual access address is127.0.0.1:8888/api/test

  1. Add both location and proxy_pass
location /api/ {
    proxy_pass http://127.0.0.1:8888/;
}

The actual access address is127.0.0.1:8888/test

  1. Location does not add proxy_pass
location /api {
    proxy_pass http://127.0.0.1:8888/;
}

The actual access address is127.0.0.1:8888//test

Summary: As long as a slash is added after the proxy_pass port, the location will be replaced and will not be added to the actual access path, including (proxy_pass
http://127.0.0.1:8888/xxx, which is actually a directory). .

Guess you like

Origin blog.csdn.net/hansome_hong/article/details/131578675