0

Background I have two instances of a web application which I want accessible at mydomain.com/ and mydomain.com/staging/. I have configured nginx to proxy requests to the proper instance depending on the path, but when a link is clicked in the staging instance, the path (in the HTML) refers to the non-staging application.

What I have I want to configure nginx to proxy requests to the staging environment whenever an http request is made with referrer mydomain.com/staging/.... I have this much working.

What I need I need to redirect the request to the proper /staging url. I cannot figure out how to get (example.com/<all this>) the path out of the requested url and append it to my /staging path for redirection. Here is the relevant portion of my config.

location @puma {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_redirect off;

  # if mydomain.com/products/1 is requested with a /staging referrer
  # I need to redirect to /staging/products/1 instead of always /staging/

  if ($http_referer ~* /.*\.com/staging$ ) {
    set $request_url /staging/; 
    return 301 $request_url;
  }
  proxy_pass http://puma_production;
}
6
  • Did you consider the Nginx sub_filter option instead of proxying? It could be a far neater way to (conditionally) rewrite URLs: nginx.org/en/docs/http/ngx_http_sub_module.html
    – JayMcTee
    Oct 29, 2015 at 13:46
  • isn't it what this post is about ? stackoverflow.com/questions/16671699/… Oct 29, 2015 at 13:48
  • 1
    Make everything easier on yourself; use a subdomain rather than a directory. Oct 29, 2015 at 14:21
  • Unfortunately I don't have the ability to create a subdomain without going through levels of approval. This was the quickest way to do it without opening other ports or making dns changes.
    – steve
    Oct 30, 2015 at 2:08
  • Well then you could fix your application to correctly set prefix to urls when it served from subfolder.
    – Alexey Ten
    Oct 30, 2015 at 7:50

0

You must log in to answer this question.

Browse other questions tagged .