SoFunction
Updated on 2025-03-03

Two ways to slash of proxy_pass in Nginx

1. The type of proxy_pass

Nginx'sOfficial websiteDivide proxy_pass into two types:

  • One type is that only contains the IP and port number (even the / after the port is not, so pay special attention here), such as proxy_pass http://localhost:8080, this method is calledNo URI method
  • Another type is that there are other paths after the port number, including only a single /, such as proxy_pass http://localhost:8080/, and other paths, such as proxy_pass http://localhost:8080/xxx. This is calledWith URI

(It is very important to understand the above two types first! The following will be understood in my way. As long as you read this article carefully, you will be very clear about the slash problem of proxy_pass!)

2. Description of the description of the method without URI

I will give you an example based on the above two types. For the sake of easy understanding, let’s explain here that we only need to see if there is / after the ip+ port in proxy_pass, and we can distinguish whether it does not have a URI or a URI method. For example: http://localhost:8080, this is a typical way without URI, such as: http://localhost:8080/, http://localhost:8080/aaa, http://localhost:8080/a/b/c, these are collectively called URI methods.

2.1. No URI method (Example 1)

If the client request is: http://localhost/test1/xxx

location /test1/ {
  proxy_pass http://localhost:8080;
}

Let’s take a look at the first example. http://localhost:8080 in proxy_pass does not have the following /, which is a typical method without URI.

To get the request path after the proxy, remember one sentence:Without URI, retain the content after location, and after splicing the proxy pathSee the steps below for details

(1) First, all contents after location are retained

http://localhost/test1/xxx

(2) Behind the splicing agent path

Final request: http://localhost:8080/test1/xxx

2.2. No URI method (Example 2)

If you don't understand Example 1 above, let's take a few more examples to ensure that you can understand this transformation process. See Example 2

If the client request is: http://localhost/test2/xxx

location /test2 {
  proxy_pass http://localhost:8080;
}

This time, there is no right/ in the location, or the same sentence:Without URI, the content after location is retained and the proxy path is spliced.

See the following steps for disassembly:

(1) First, all contents after location are retained

http://localhost/test2/xxx

(2) Behind the splicing agent path

Final request: http://localhost:8080/test2/xxx

2.3. Summary of methods without URI

It can be seen that the easiest way is without URI. Whether the location is with right/ or not, it is kept directly spliced ​​to the back of proxy_pass.

3. Examples of URI methods

The method of bringing URI is difficult to understand, but as long as I follow the method I gave, it is also very easy. Let’s take a look at the following examples.

Still in one sentence:With URI, remove location, and the remaining content on the right is spliced ​​into the proxy path

3.1. Method with URI (Example 1)

If the client requests: http://localhost/test1/xxx

location /test1/ {
  proxy_pass http://localhost:8080/;
}

See the following steps for disassembly:

(1) First, the location is removed on the client side to obtain the remaining content on the right

http://localhost/test1/xxx         --------------------------->         xxx(The content on the right obtained

(2) Spliced ​​into the proxy path

Final request: http://localhost:8080/xxx

3.2. Method with URI (Example 2)

Still the same sentence:With URI, remove location, and the remaining content on the right is spliced ​​into the proxy path

If the client requests: http://localhost/test2/xxx

 location /test2 {
  proxy_pass http://localhost:8080/;
 }

See the following steps for disassembly:

(1) First, the location is removed on the client side to obtain the remaining content on the right

http://localhost/test2/xxx         -------------------------------->     /xxx(The content on the right obtained

(2) Spliced ​​into the proxy path

Final request: http://localhost:8080//xxx              http://localhost:8080//xxx (There are two/ here)

3.3. Method with URI (Example 3)

Still the same sentence:With URI, remove location, and the remaining content on the right is spliced ​​into the proxy path

If the client requests: http://localhost/test3/xxx

location /test3/ {
  proxy_pass http://localhost:8080/aaa;
}

See the following steps for disassembly:

(1) First, the location is removed on the client side to obtain the remaining content on the right

http://localhost/test3/xxx      ------------------------>       xxx(The content on the right obtained

(2) Spliced ​​into the proxy path

Final request: http://localhost:8080/aaaxxx

http://localhost:8080/aaaxxx (There is no / between aaa and xxx, obviously this is wrong)

3.4. Method with URI (Example 4)

Still the same sentence:With URI, remove location, and the remaining content on the right is spliced ​​into the proxy path

If the client requests: http://localhost/test4/xxx

location /test4/ {
  proxy_pass http://localhost:8080/aaa/;
}

See the following steps for disassembly:

(1) First, the location is removed on the client side to obtain the remaining content on the right

http://localhost/test4/xxx      ------------------------>       xxx(The content on the right obtained

(2) Spliced ​​into the proxy path

Final request: http://localhost:8080/aaa/xxx

3.5. Method with URI (Example 5)

Still the same sentence:With URI, remove location, and the remaining content on the right is spliced ​​into the proxy path

If the client requests: http://localhost/test5/xxx

 location /test5 {
  proxy_pass http://localhost:8080/aaa;
 }

See the following steps for disassembly:

(1) First, the location is removed on the client side to obtain the remaining content on the right

http://localhost/test5/xxx      ------------------------>       /xxx(The content on the right obtained

(2) Spliced ​​into the proxy path

Final request: http://localhost:8080/aaa/xxx

3.6. Method with URI (Example 6)

Still the same sentence:With URI, remove location, and the remaining content on the right is spliced ​​into the proxy path

If the client requests: http://localhost/test6/xxx

 location /test6 {
  proxy_pass http://localhost:8080/aaa/;
 }

See the following steps for disassembly:

(1) First, the location is removed on the client side to obtain the remaining content on the right

http://localhost/test6/xxx      ------------------------>       /xxx(The content on the right obtained

(2) Spliced ​​into the proxy path

Final request: http://localhost:8080/aaa//xxx

http://localhost:8080/aaa//xxx (There are two / between aaa and xxx, obviously this is wrong)

3.7. Summary of the method with URI

I guess a careful friend will find a problem, that is, when one of the location and proxy_pass does not have /(For example, Example 2, Example 3, Example 6), the final request path is unusual, so here is a suggestion: when writing reverse proxy in the future, either location and proxy_pass should be brought / or neither, otherwise the error in the above example will occur.

This is the article about the implementation of the slash problem of proxy_pass in Nginx. For more related contents of Nginx proxy_pass slash, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!