• 8
name Punditsdkoslkdosdkoskdo

Nginx Redirect via Proxy, Rewrite & Preserve URL

In Nginx we have been trying to redirect a URL as follows: ->

where the user still sees the original URL in their browser. Once the user is redirected, say they click on the link to /section/index.html, we would want this to make a request that leads to the redirect ->

and again still preserve the original URL.

Our attempts have involved various solutions using proxies and rewrite rules, and below shows the configuration that has brought us closest to a solution (note that this is the web server configuration for the web server). However, there are still two problems with this:

  • It does not perform the rewrite properly, in that the request URL received by the web server includes /some/path and therefore fails to serve the required page.
  • When you hover on a link once a page has been served, /some/path is missing from the URL

    server {
        listen          80;
        location /some/path/ {
            proxy_set_header Host $host;
        location / {
            index index.html;
            root  /var/www/;

We are looking for a solution that only involves changing the web server configuration on We are able to change the config on (also Nginx), however we want to try and avoid this because we will need to repeat this setup for hundreds of different servers whose access is proxied through

You should use URI part in proxy_pass directive. Also, you mixed up order arguments of proxy_redirect directive, and probably you don't need it at all. Nginx has reasonable default for this directive.

In this case your location block could be really simple:

location /some/path/ {
    # note this slash  -----------^
    proxy_set_header Host $host;
  • 1
Reply Report

When that slash is added to an nginx proxied jenkins, you are presented with the “It appears that your reverse proxy set up is broken" error.

proxy_pass          http://localhost:8080/;

Remove this -----------------------------^

It should read

proxy_pass          http://localhost:8080;
  • 0
Reply Report

First, you shouldn't use root directive inside the location block, it is a bad practice. In this case it doesn't matter though.

Try adding a second location block:

location ~ /some/path/(?<section>.+)/index.html {
    proxy_set_header Host $host;

This captures the part after /some/path/ and before index.html to a $section variable, which is then used to set the proxy_pass destination. You can make the regex more specific if you require.

  • 2
Reply Report