order
As a lightweight and high-performance web service software, Nginx is widely used because of its characteristics of less memory and strong concurrency. Many large domestic Internet companies also favor Nginx. Like BAT (Baidu, Alibaba and Tencent), TMD (headlines, meituan and didi) and so on. Students who have used Nginx know that you only need to change the configuration and start it accurately as needed, so you can access it gracefully. Therefore, Nginx is very interested in the configuration file, which requires us to confirm it again and again when changing the configuration file, otherwise it may lead to tragedy due to negligence? In the real case, because there is less than one character "/" during configuration, the access fails and an error is reported, so a complaint is received. So how did it happen? The reason is that Nginx is configuring proxy_ It is caused by less "/" characters when transferring through the pass agent. Some students have questions about whether to add "/", is the difference really that big? With this question, let's explore this problem.
Detailed explanation of location directory matching
- Each location of nginx is a matching directory. The strategy of nginx is: when an access request comes, it will resolve the access address, match one by one from top to bottom, execute the policy in the corresponding location brace on the matching, and make corresponding decisions to the request according to the policy.
- By access address: http://www.wandouduoduo.com/wddd/index.html For example, nginx is configured as follows:
location /wddd/ { proxy_connect_timeout 18000; ##Change it to half an hour proxy_send_timeout 18000; proxy_read_timeout 18000; proxy_pass http://127.0.0.1:8080; }
The location will be matched during access, so as to forward the request agent to the local 8080Tomcat service. After Tomcat, the information will be returned in the original way. Summary: if there is no "/" in location, the request can vaguely match all strings starting with the string. If there is "/", it can only match the character itself accurately.
The following is an example:
The configuration location /wandou can match the / wandoudou duo request, or / wandou*/duoduo, etc. as long as the directory starting with wandou can be matched. The location /wandou / must exactly match the request of / wandou / directory, and cannot match the request of / wandouduoduo / or / wandou*/duoduo.
proxy_ Four differences between pass and "/"
The access addresses are: http://www.wandouduoduo.com/wddd/index.html For example. All requests match directory / wddd/
First: add "/"
location /wddd/ { proxy_pass http://127.0.0.1:8080/; }
According to the test result, the requested agent jumps to: http://127.0.0.1:8080/index.html
Second: No "/"
location /wddd/ { proxy_pass http://127.0.0.1:8080; }
According to the test result, the requested agent jumps to: http://127.0.0.1:8080/wddd/index.html
Third: add directory and "/"
location /wddd/ { proxy_pass http://127.0.0.1:8080/sun/; }
According to the test result, the requested agent jumps to: http://127.0.0.1:8080/sun/index.html
Fourth: add directory without "/"
location /wddd/ { proxy_pass http://127.0.0.1:8080/sun; }
According to the test result, the requested agent jumps to: http://127.0.0.1:8080/sunindex.html
summary
If you add "/" after the location directory, you can only match the directory. If you do not add "/", you can not only match the directory, but also fuzzy match the directory. Proxy_ With or without "/" in pass, the proxy jump address is directly spliced.
In order to deepen your impression, you can use the following configuration experiment to test:
server { listen 80; server_name localhost; # http://localhost/wddd01/xxx -> http://localhost:8080/wddd01/xxx location /wddd01/ { proxy_pass http://localhost:8080; } # http://localhost/wddd02/xxx -> http://localhost:8080/xxx location /wddd02/ { proxy_pass http://localhost:8080/; } # http://localhost/wddd03/xxx -> http://localhost:8080/wddd03*/xxx location /wddd03 { proxy_pass http://localhost:8080; } # http://localhost/wddd04/xxx -> http://localhost:8080//xxx , please pay attention to the double slash here and analyze it carefully. location /wddd04 { proxy_pass http://localhost:8080/; } # http://localhost/wddd05/xxx -> http://localhost:8080/hahaxxx , please note that there is no slash between haha and XXX here. Analyze the reason. location /wddd05/ { proxy_pass http://localhost:8080/haha; } # http://localhost/api6/xxx -> http://localhost:8080/haha/xxx location /wddd06/ { proxy_pass http://localhost:8080/haha/; } # http://localhost/wddd07/xxx -> http://localhost:8080/haha/xxx location /wddd07 { proxy_pass http://localhost:8080/haha; } # http://localhost/wddd08/xxx -> http://localhost:8080/haha//xxx , notice the double slash here. location /wddd08 { proxy_pass http://localhost:8080/haha/; } }