0

Say I am trying to proxy all requests coming in for jpg, png and mp3 files like so:

http://example.com/some/url/file.png

to

http://example.net/data/some/url/file.png

Notice it's the exact same path to another server but with data added.

Her's what I have so far:

location ~* .(jpg|png|mp3)$ {
        proxy_pass https://example.net/data/;
        proxy_redirect https://example.net/data/ /
    }

However I keep getting error

"proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block

What is going wrong and how can I correctly write this location block?

Shaunak
  • 103
  • 3

1 Answers1

2

You need to change an URI using rewrite directive if you want to pass a different URI to the backend in the regex matching location block (the same is true for the named locations):

location ~* \.(jpe?g|png|mp3)$ {
    rewrite (.*) /data$1 break;
    proxy_pass https://example.net;
    proxy_redirect https://example.net/data/ /
}
Ivan Shatsky
  • 2,726
  • 2
  • 7
  • 19