'Can a response from an http request alter the base address in the next client request?

I have an octoprint server running at http://192.168.1.205. I also have an nginx server hosting myDomain. I want to be able to use the nginx server to pass on a request for http://myDomain/octo to http://192.168.1.205 using a reverse proxy. Here is my nginx code...

server {
  server_name myDomain;
  location /octo/ {
    rewrite ^/octo/(.*) /$1 break;  #strip /octo from url
    proxy_pass http://192.168.1.205;
  }
}

The first http://myDomain/octo request is passed on to http://192.168.1.205 correctly. But after the first response the code in the client makes another request to http://myDomain/moreUri. Since this uri doesn't have /octo nginx doesn't know to send it to http://192.168.1.205/moreUri. Is there a way to have nginx change something in the first response so that the client then makes following requests to http://myDomain/octo/moreUri?

I was able to accomplish this for a case where the octoprint server responded with a redirect. I used ...

proxy_redirect http://192.168.1.205/ http://myDomain/octo/;

and it worked. But that only works on redirects and the following requests were wrong again.



Solution 1:[1]

Is there a way to have nginx change something in the first response so that the client then makes following requests to http://myDomain/octo/moreUri?

I am not aware that this is possible.

What about to adjust the nginx configuration accordingly ? using location / to process all requests within that block and add an additional redirect directive to address the "Since this uri doesn't have /octo nginx doesn't know to send it to http://192.168.1.205/moreUri" should do the trick.

server {
  server_name myDomain;
  location / {
    rewrite ^/octo/(.*) /$1 break;  #strip /octo from url
    rewrite ^/(.*)/(.*) /octo/$2 break;  #rewrite /moreURI to /octo/moreURI
    proxy_pass http://192.168.1.205;
  }
}

No matter if the above nginx reconfiguration fixes your issue, i assume the root cause why you need to configure the nginx as reverse proxy in this way might be a misconfigured (or not optimally configured) application. Check the config file if it is possible to configure the applications base path. If so, set it to /octo/ (so the application itself prepends /octo/ to all the links it presents to the user and all requests to the backend automatically) and adjust the rewrite rules accordingly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mottek