Score:0

How to reverse proxy specific extension names with Nginx

cn flag

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?

Score:2
gr flag

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/ /
}
Shaunak avatar
cn flag
Perfect! thank you
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.