NginX trailing slash in proxy pass url

前端 未结 2 433
梦如初夏
梦如初夏 2020-12-23 10:14

I know that this question has been asked multiple times but after a trying a lot of solutions I am still stuck.

I am using NginX to proxy pass to a NodeJs applicatio

2条回答
  •  春和景丽
    2020-12-23 10:48

    I guess you use proxy_pass without URI. In that case nginx pass original URI as it was, not normalized one. So you should use proxy_pass http://backend/;

    UPDATE

    So I was right. Just add URI part to proxy_pass like this:

    location = /a {
        proxy_pass http://127.0.0.1:8080/a;
        ...
    }
    
    location ^~ /a/ {
        proxy_pass http://127.0.0.1:8080/a/;
        ...
    }
    

    As stated in nginx documentation if proxy_pass used without URI (i.e. without path after server:port) nginx will put URI from original request exactly as it was with all double slashes, ../ and so on.

    On the other side, URI in proxy_pass acts like alias directive, means nginx will replace part that matches location prefix (in out case it's /a in first location and /a/ is second) with URI in proxy_pass directive (which I intentionally made the same as location prefix) so URI will be the same as requested but normalized (without doule slashes and all that staff). Be careful with trailing slashes. Nginx replaces part literally and you could end up with some strange url.

    Here is an example with trailing slash in location, but no trailig slash in proxy_pass.

    location /one/ {
        proxy_pass http://127.0.0.1:8080/two;
        ...
    }
    

    if one go to address http://yourserver.com/one/path/here?param=1 nginx will proxy request to http://127.0.0.1/twopath/here?param=1. See how two and path concatenates.

提交回复
热议问题