27
    location /product {
        proxy_pass http://10.0.0.25:8080;
    }

if I use my first location description for product folder, I should use http://mysdomain.com/product/ and I can not use http://mysdomain.com/product from browser. I mean I should use a slash end of url. I want to access product folder with two stuation.

is there s difference between this:

    location /product/ {
        proxy_pass http://10.0.0.25:8080;
    }

2 Answers 2

24

These locations are different. First one will match /production for example, that might be not what you expected. So I prefer to use locations with a trailing slash.

Also, note that:

If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, or memcached_pass, then in response to a request with URI equal to this string, but without the trailing slash, a permanent redirect with the code 301 will be returned to the requested URI with the slash appended.

If you have something like:

location /product/ {
    proxy_pass http://backend;
}

and go to http://example.com/product, nginx will automatically redirect you to http://example.com/product/.

Even if you don't use one of these directives above, you could always do the redirect manually:

location = /product {
    rewrite ^ /product/ permanent;
}

or, if you don't want redirect you could use:

location = /product {
    proxy_pass http://backend;
}
7
  • 1
    I am using proxy_pass myip:8080/product , then call browser this address. Browser redirects me to myip/product and gives error page can not view.
    – barteloma
    Commented Jun 25, 2014 at 7:01
  • Use backticks for code. Markdown parsed you comment and it's hard to find out what your code really is.
    – Alexey Ten
    Commented Jun 25, 2014 at 7:04
  • 1
    Thanks, it was important to know that if /product/ is added then even the browser send it /product is bound to receive the 301 from the server. Quite valid point indeed.
    – Sur Max
    Commented Aug 25, 2018 at 12:13
  • @barteloma - same for me. ip:port/path redirects to ip/path/. The :port is lost.
    – olfek
    Commented Aug 28, 2023 at 12:22
  • 1
    @olfek did you change port_in_redirect directive?
    – Alexey Ten
    Commented Aug 28, 2023 at 12:43
8

No, these are not the same--you will need to use a trailing slash with a regex to match both, i.e.

location ~ /product/?

See this related answer for a more detailed response on how to match the entire URL.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .