I need to have a reverse proxy that is capable of forwarding requests to upstream applications that have identical architecture, but are not Identical. I have no control over the code running on the upstream server. It is a commercial product that interfaces with milking robots. I have been for the past 16 months using Nginx to forward requests upstream to one server, using a location block like this:

location /T4C { proxy_pass; proxy_read_timeout 90; }

It works fine, but unfortunately the web application is written in such a way that every 5 seconds it redirects you to "http://ip/T4C/Content/StartPage.aspx" This means that my location name must always be "/T4C" If I try /foo it will load the startup page but as soon as one of the refreshes triggers, it will timeout trying to access /T4C/Content/StartPage.aspx I tried using a cookie:

location /crews {
  add_header Set-Cookie targetip=; #Tried with and without the slash

  proxy_pass; }

location /T4C {
  proxy_pass  http://$cookie_targetip/T4C/; }#Tried with and without the slashes, all permutaions

This results in a redirect loop, or a 500 internal server error for reasons that I don't quite understand.( I think 500 happens when there is no cookie set )

I have also tried a rewrite, and I'm currently looking more into that but again, I don't quite understand the mechanics.

I've also tried reading about 'origin' but the examples seem to be either A/B testing or load balancing. I want to make it clear here that the user needs to be able to select via URL which backend server they get connected to, and then that choice has to persist even though the browser will keep trying to direct it to /T4C/*** I know the ideal situation would be to change the webapp but unfortunately that's beyond my control. The systems are all programmed identically and that code is not mine, nor can I change it.

I hope I formulated a coherent question. Thanks for reading this far.



I think I have it working. Credit to Anuboiz for putting me onto the http_sub _module. That is indeed what I needed. I believe what I'm doing is intercepting the redirects and changing their URL. I found out that the URL contains a parameter instructing the application where to redirect to.

location /CREWS/  {
proxy_read_timeout  90;
sub_filter_once off; #########This is important. Defaults to ON
sub_filter 'ReturnUrl=%2fT4C%2f' 'ReturnUrl=%2fCREWS%2f' ;
sub_filter '/T4C/' '/CREWS/' ;
sub_filter '/T4C/Content/Login.aspx?ReturnUrl=%2fT4C%2f' '/CREWS/Content/Login.aspx?ReturnUrl=%2fCREWS%2f';
sub_filter_types '*';


You need the ngx_http_sub_module. It's not included in upstream nginx (but may still be included in your linux distro), so you may have to build nginx from source. It allows to substitude arbitrary strings in response body.

If I correctly understood, in your case you will need something like:

location /T4C  {
    proxy_read_timeout  90;
    sub_filter '' '';
  • Would I be able to do that filter based on a cookie, like: sub_filter '' '$cookie_targetip'; – Andrew William Down Crews Apr 28 '17 at 22:58
  • @AndrewWilliamDownCrews Yes, you should be able to use variables as well.. – Anubioz Apr 28 '17 at 23:05
  • Even the raw version without variables gives 502 bad gateway: ` 2017/04/28 23:44:53 [error] 25043#25043: *765 recv() failed (104: Connection reset by peer) while reading response header from upstream, client:, server: , request: "GET /T4C// HTTP/1.0", upstream: "", host: "" – Andrew William Down Crews Apr 28 '17 at 23:46
  • I checked nginx -V to confirm that I do indeed have --with-http_sub_module so it appears that either I'm implementing your suggestion incorrectly or that doesn't work. I removed all other location blocks. – Andrew William Down Crews Apr 29 '17 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.