A somewhat simpler solution, that worked for me, is to disable absolute redirects with absolute_redirect off; as in the following example:
server { listen 80; server_name localhost; absolute_redirect off; location /foo/ { proxy_pass http://bar/; }If I run curl on on http://localhost:8080/foo, I can see that the Location header in the redirect HTTP response is given as /foo/ and not http://localhost/foo/.
https://stackoverflow.com/questions/15555428/nginx-causes-301-redirect-if-theres-no-trailing-slash