Nginx Proxy Redirection Without Changing URL

I have nginx (: 80) and an upstream server (: 8080) running on my machine.

  • I want to proxy all requests in / assets / (*.?) To the upstream / upstream / $ 1 folder.
  • The upstream server redirects (302) / upstream / file_id to /real/file/location.ext

Here is my code:

location /assets/ { rewrite ^/assets/(.*) /upstream/$1 break; proxy_pass http://127.0.0.1:8000; } 

This seems to work, but on the client side I get a redirected location:

 http://myserver.com/real/file/location.ext 

I want to hide it so that it stays:

 http://myserver.com/assets/file_id 

The idea is for the upstream server to find the real location of the file, but let nginx serve the file without giving away its real location. Is it possible?

+4
source share
1 answer

at first you use 8000 in proxy_pass, but you note that your port is 8080.

Secondly, deleting the rewrite line should do the trick, because you really use the rewrite rule here and you never get into the proxy_pass line. Something like the following should work:

 location /assets/ { include proxy_params; proxy_pass http://127.0.0.1:8080; } 

There are also proxy_rewrite and proxy_redirect commands that can help you with this upstream-redirect being handled internally by nginx.

Hope this helps!

0
source

Source: https://habr.com/ru/post/1501425/


All Articles