How Nginx Decides Which Server Block Will Handle a Request

Respect to Understanding Nginx Server and Location Block Selection Algorithms

Parsing the “server_name” Directive to Choose a Match

Next, to further evaluate requests that have equally specific listen directives, Nginx checks the request’s “Host” header. This value holds the domain or IP address that the client was actually trying to reach.

Matching Location Blocks

Similar to the process that Nginx uses to select the server block that will process a request, Nginx also has an established algorithm for deciding which location block within the server to use for handling requests.

Location Block Syntax

Location blocks generally take the following form:

1location optional_modifier location_match {
2
3    . . .
4
5}

The modifiers below will cause the associated location block to be interpreted as follows:

When Does Location Block Evaluation Jump to Other Locations?

Another instance where the processing location may be reevaluated is with the try_files directive. This directive tells Nginx to check for the existence of a named set of files or directories. The last parameter can be a URI that Nginx will make an internal redirect to.

Consider the following configuration:

1root /var/www/main;
2location / {
3    try_files $uri $uri.html $uri/ /fallback/index.html;
4}
5
6location /fallback {
7    root /var/www/another;
8}

proxy_pass反向代理配置中url后面加不加/的说明

path 后面加 /

1location  /proxy/ {
2          proxy_pass http://192.168.1.5:8090/;
3}
4这样,访问http://192.168.1.23/proxy/就会被代理到http://192.168.1.5:8090/。p匹配的proxy目录不需要存在根目录/var/www/html里面
5注意,终端里如果访问http://192.168.1.23/proxy(即后面不带"/"),则会访问失败!因为proxy_pass配置的url后面加了"/"
6浏览器访问http://103.110.186.23/proxy的时候,会自动加上"/”(同理是由于proxy_pass配置的url后面加了"/"),并反代到http://103.110.186.5:8090的结果
1location  /proxy/ {
2          proxy_pass http://192.168.1.5:8090;
3}
4那么访问http://192.168.1.23/proxy或http://192.168.1.23/proxy/,都会失败!
5这样配置后,访问http://192.168.1.23/proxy/就会被反向代理到http://192.168.1.5:8090/proxy/
1location  /proxy/ {
2          proxy_pass http://192.168.1.5:8090/haha/;
3}
4这样配置的话,访问http://103.110.186.23/proxy/代理到http://192.168.1.5:8090/haha/
1location  /proxy/ {
2          proxy_pass http://192.168.1.5:8090/haha;
3}
4上面配置后,访问http://192.168.1.23/proxy/index.html就会被代理到http://192.168.1.5:8090/hahaindex.html
5同理,访问http://192.168.1.23/proxy/test.html就会被代理到http://192.168.1.5:8090/hahatest.html
6注意,这种情况下,不能直接访问http://192.168.1.23/proxy/,后面就算是默认的index.html文件也要跟上,否则访问失败!

path路径后面不带 /

1location  /proxy {
2          proxy_pass http://192.168.1.5:8090/;
3}
1location  /proxy {
2          proxy_pass http://192.168.1.5:8090;
3}
4这样配置的话,浏览器访问http://103.110.186.23/proxy会自动加上"/”(即变成http://103.110.186.23/proxy/),代理到192.168.1.5:8090/proxy/
1location  /proxy {
2          proxy_pass http://192.168.1.5:8090/haha/;
3}
4这样配置的话,浏览器访问http://103.110.186.23/proxy会自动加上"/”(即变成http://103.110.186.23/proxy/),代理到http://192.168.1.5:8090/haha/
1location  /proxy {
2          proxy_pass http://192.168.1.5:8090/haha;
3}
4这样配置的话,浏览器访问http://103.110.186.23/proxy,和第三种结果一样,同样被代理到http://192.168.1.5:8090/haha/