The slash problem in location is easier to understand, and the one without a slash is a fuzzy match. For example:
location /doc
- It can match /doc/index.html or /docs/index.html.
location /doc/
- It can only match /doc/index.html, not /docs/index.html.
For the slash problem in proxy_pass, it must be considered in combination with location.
proxy_pass without URL method
There is no / after the IP and port, and it is a method without URI . nginx will retain the path in the location . Therefore, when accessing http://127.0.0.1/docs/, you are actually accessing http://127.0.0.1:8080/docs/.
location /docs/ {
proxy_pass http://127.0.0.1:8080;
}
location /jenkins/ {
proxy_set_header host $host;
proxy_pass http://192.168.11.128:8080;
}
tomcat日志
192.168.11.128 - - [04/Jun/2023:07:50:59 +0800] "GET /jenkins/index.html HTTP/1.0" 404 648
proxy_pass with URI
There is / after this IP and port, which is a URI method . nginx will use aliases to replace the URL . Therefore, when accessing http://127.0.0.1/docs/, you are actually accessing http://127.0.0.1:8080/, and /docs/ is replaced with /.
location /docs/ {
proxy_pass http://127.0.0.1:8080/;
}
location /article/ {
proxy_set_header host $host;
proxy_pass http://192.168.11.128:8080/;
}
Backend tomcat log
192.168.11.128 - - [04/Jun/2023:08:08:56 +0800] "GET /index.html HTTP/1.0" 404 648
Expand the method with URI:
location /article/ {
proxy_pass http://127.0.0.1:8080/docs/;
}
This same IP and port are followed by /, which is also a URI method. Therefore, when accessing http://127.0.0.1/article/, you are actually accessing http://127.0.0.1:8080/docs/, and /article/ is replaced by /docs/.
Examples are as follows:
location /article/ {
proxy_set_header host $host;
proxy_pass http://192.168.11.128:8080/docs/;
}
192.168.11.128 - - [04/Jun/2023:08:00:26 +0800] "GET /docs/index.html HTTP/1.0" 404 648
It is this way of writing with URI that often makes mistakes.
For example:
location /article/ {
proxy_pass http://127.0.0.1:8080/docs;
}
When accessing http://127.0.0.1/article/index.html, the original intention is to access http://127.0.0.1:8080/docs/index.html. However, /article/ is replaced by /docs, so the actual access is http://127.0.0.1:8080/docsindex.html.