The difference between nginx configuration location and proxy_pass with or without slashes

nginx configuration slash parsing 

Many people are hesitant about location and proxy_pass with or without slashes when configuring nginx. Today’s article will help you understand how to configure location and proxy_pass slashes in nginx.

location /test {
   
       proxy_pass http://192.168.1.55/var/;}
Suppose there are two existing nginx servers

nginx A: 192.168.1.55

nginx B: 192.168.1.56

Browser access http://192.168.1.55/test/api

The following table is my configuration of different rules in nginx A, request nginx A, observe nginxB to the conclusion.

Focus on the analysis of proxy_pass , which can be divided into three forms:

1.http://ip:port

2.http://ip:port/

3.http://ip:port/xxx

According to whether the string is connected after the ip:port is classified as 2 types , "/" is also a string, so 1 is classified as a type, 2 and 3 are classified as b types

When the string is not connected after class a, nginx will transfer the original request path to the next station nginx intact

When a string is connected after class b, nginx will remove the location from the original request path, and then splice the remaining string into proxy_pass to generate a new request path, and then transfer the new request path to the next station nginx

Example analysis:

Case 6: The ip:port of proxy_pass is followed by the string "/var/", so the location: "/test" is removed from the original request path "/test/api" and changed to "/api", and then " /api" is spliced ​​to proxy_pass: http://192.168.1.56/var/ and a new request path is generated: "http://192.168.1.56/var//api", so the request received by nginx at the next stop is /var//api.

公式:/var/+(/test/api -/test)=/var//api

Case 7. The ip:port of proxy_pass is followed by the string "/var", so the location: "/test/" is removed from the original request path: "/test/api" and becomes "api", and then "api" is spliced After going to proxy_pass: http://192.168.1.56/var, a new request url is generated: "http://192.168.1.56/varapi", so the request received by nginx at the next stop is "/varapi".

公式:/var+(/test/api -/test/)=/varapi

 

Guess you like

Origin blog.csdn.net/s827292890/article/details/129583746