Score:0

NGINX Set Reverse Proxy Header based on the URL

cf flag

I have set up a reverse proxy for the static content on the site. Everything works, the only issue is there are a few pages that the static content is not at that location. I am trying to do something where I ONLY proxy that content for some of the pages, or easier in reverse, proxy the content for all pages but a few.

Current Proxy

location /sites/default/files/ {    
    proxy_set_header Host url.com;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $host;
    proxy_pass https://url.com;
}

But I need some way to do the following (I know this syntax is 100% wrong, just making up to give an idea what I am trying to do)

location /sites/default/files/ {
    if (request_url != '/cart' || request_url != 'checkout') {
        proxy_set_header Host url.com;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $host;
        proxy_pass https://url.com;
    }
}
Score:0
cf flag

After hours of testing and Googling I have a fix for the solution. There were other similar ones out there but none that were exactly what I was trying to do. Here is what I ended up doing to get that to work.

First I created a variable map that conditionally sets the value based on the $http_referer value that is within NGINX

map $http_referer $resources_location {
    default     "url.com";
    "~*/page2"  "url2.com";
    "~*/page3"  "url2.com";
}

This takes the variable $http_referer and sets the variable $resources_location based on the value of the $http_referer.

the default is the main url, and then I have regex expressions to determine of the page has /page2, /page3 in it.

The one catch I assume is https://url.com/page2 would trigger it, so would https://url.com/sub/page2. I do not have the need to worry about that, but one might so I would test also.

Then I use the new variable that is set in the proxy

location /sites/default/files/ {        
    proxy_set_header Host $resources_location;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $host;
    proxy_pass https://${resources_location};
}  

Im a newbie to NGINX, so if there is something that can be done better, let me know!!

cn flag
Did you mean "url3" for "page3"?
cf flag
@AlexisWilke yeah, just an error in removing the identifying data we actually use.
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.