Using proxy pass in nginx (using alias and root)

We learned the matching rules of location together. If you don't know, please refer to my article( The use of location in nginx )Today, it's very simple to learn the proxy pass matching process in nginx

There are mainly two cases of proxy pass matching

1. Only IP (domain name) + port is behind the url of proxy  pass proxy, and nothing else (including "/") is allowed.)

At this time, the path of the proxy needs to append the path after ip+port in the requested url to the path after proxy pass

For example:

Suppose the request path of HTTP is: http: / / 120.24.95.148:9998 / nginx / Hello? Name = tao long

nginx profile content

server {
        listen       9998;
        server_name     120.24.95.148;
    
     	#Matching rule
        location /nginx {
               proxy_pass http://120.24.95.148:10010;
        }
   

}

here

URL after proxy_pass = http://120.24.95.148:10010, nothing

This is to append the content of "nginx/hello?name=taolong" in the http request path to the url of the proxy pass

The path of the final agent is: http://120.24.95.148:10010/nginx/hello?name=taolong

2. Besides ip (domain name) + port, there are other contents behind the url of proxy  pass proxy

At this time, the matching logic needs to append the unmatched content in the request to the location to the url of proxy pass

For example:

Suppose the request path of HTTP is: http: / / 120.24.95.148:9998 / nginx / Hello? Name = tao long

The content of nginx configuration file is as follows:

server {
        listen       9998;
        server_name     120.24.95.148;
    

     	#Matching rule
        location /nginx/hello {
        	   #Note that this is the end of "/", unmatched content in the request url:? Name = tao long
               proxy_pass http://120.24.95.148:10010/hello;
        }
        #At this time, the output result is: http://120.24.95.148:10010/hello?name=taolong
	
		#Matching rule
        location /nginx {
        	   #Note that this is the end of "/", unmatched content in the request url: / Hello? Name = tao long
               proxy_pass http://120.24.95.148:10010/;
        }
        #At this time, the output result is: http://120.24.95.148:10010/hello?name=taolong
}

Proxy ﹣ u pass is over. Next, I will mention that nginx is similar to the above situation, that is, the use of root and alias

root and alias use

When root is used, it is similar to the first case above, and directly corresponds to the directory specified by root

When using alias, similar to the second case above, append the matching content to the url of alias

		#Test path / root
        #Location content / etc/nginx/html/root/a.html;
        location /root {
                root /etc/nginx/html;
                index a.html;
        }


        #Test path / root/test
        #Location content / etc/nginx/html/root/test/b.html;
        location /root/test {
                root /etc/nginx/html;
                index a.html;
        }


        #Test path / alias/test/a
        #Location content / etc/nginx/html/test/a/b.html
        location /alias {
                alias /etc/nginx/html;
                index b.html;
        }


        #Test path / alias/test/
        #Location content / etc/nginx/html/a.html
        location /alias/test {
                alias /etc/nginx/html;
                index a.html;
        }
·		#The regular $1 represents the contents of the first matched path variable corresponding to the matched. *
        #Test path / aliasregex/test -- "/ etc/nginx/html/test/a.html
        #Test path / aliasregex/a --, located content / etc/nginx/html/a/a.html
        location ~ /aliasregex/(.*) {
            alias /etc/nginx/html/$1;
            index a.html;
        }

40 original articles published, 21 praised, 30000 visitors+
Private letter follow

Tags: Nginx

Posted on Sat, 11 Jan 2020 01:41:53 -0500 by Hannes2k