title: Conditionally rewrite request url based on referer - Nginx
body: <p>What I am trying to achive; is when the request coming from <code>http://&lt;ip&gt;/vault/ui/</code> (referer in the request header) and it includes the <code>http://&lt;ip&gt;/v1/*</code> endpoint, to be rewriten or redirected to <code>http://&lt;ip&gt;/vault/v1/</code></p>
  <p>can someone please help me to solve this issue?</p>
  <p><strong>/etc/nginx/sites-enabled/reverse-proxy.conf</strong></p>
  <pre><code>
  upstream command_center_vault {
     server command-center-0.blinchik.io:28200;  
  }
  
  server {
  
      listen 80;
      listen [::]:80;
  
  
  location /vault/ {
  
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Accept-Encoding &quot;&quot;;
     
       proxy_pass &quot;http://command_center_vault/vault/&quot;;
       proxy_redirect /ui/ /vault/ui/;
    }
  
  location /vault/v1/ {
  
       proxy_pass &quot;http://command_center_vault/v1/&quot;;
  
    }
  
  }
  </code></pre>
  <p><strong>Headers</strong></p>
  <p><a href="https://i.stack.imgur.com/H6zwL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/H6zwL.png" alt="enter image description here" /></a></p>
  <hr />
  <h1>Update</h1>
  <p>A little bit more context, the overarching architecture looks as in the picture below.</p>
  <p><a href="https://i.stack.imgur.com/qA42a.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qA42a.png" alt="enter image description here" /></a></p>
  <p>the configuration of nginx server in the private subnet looks like this:</p>
  <p><strong>private subnet nginx</strong></p>
  <pre><code>upstream consul_server {
  
     server brain-consul-server-0.blinchik.io:8500;
     server brain-consul-server-1.blinchik.io:8500;
     server brain-consul-server-2.blinchik.io:8500;
     
   }
  
  upstream vault_server {
  
     server brain-vault-server-0.blinchik.io:8200;
     server brain-vault-server-1.blinchik.io:8200;
  
  }
  
  
  server {
  
     listen 28500;
     listen [::]:28500;
  
     location /consul/ {
  
         proxy_pass &quot;http://consul_server&quot;;
         sub_filter_once off;
         sub_filter_types application/javascript text/html;
         sub_filter &quot;/v1/&quot; &quot;/consul_v1/&quot;;
  
     }
  
     location /consul_v1/ {
         proxy_pass &quot;http://consul_server/v1/&quot;;
     }
  }
  
  server {
  
     listen 28200;
     listen [::]:28200;
  
     location /vault/ {
  
         proxy_pass &quot;http://vault_server/&quot;;
  
         port_in_redirect off;
  
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header Accept-Encoding &quot;&quot;;
  
         proxy_redirect /ui/ /vault/ui/;
         sub_filter_once off;
         sub_filter '&lt;head&gt;' '&lt;head&gt;&lt;base href=&quot;/vault/&quot;&gt;';
         sub_filter '&quot;/ui/' '&quot;ui/';
  
         #inspired by this repo https://github.com/Folcky/hashicorp-vault-and-nginx
  
  
     }
  
     location /v1/ {
            proxy_pass &quot;http://vault_server/v1/&quot;;
     }
  
  }
  </code></pre>
  <p><strong>public subnet nginx</strong></p>
  <pre><code>upstream command_center_vault {
     server command-center-0.blinchik.io:28200;  
  }
  
  server {
  
      listen 80;
      listen [::]:80;
  
  
  location /vault/ {
  
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Accept-Encoding &quot;&quot;;
     
       proxy_pass &quot;http://command_center_vault/vault/&quot;;
       proxy_redirect /ui/ /vault/ui/;
    }
  
  location /vault/v1/ {
  
       proxy_pass &quot;http://command_center_vault/v1/&quot;;
  
    }
  
  }
  
  </code></pre>
  <p>the consul part works fine. if I change in the public subnet configuration the location of <code>/vault/v1/</code> to <code>/v1/</code> then it works as well. But the problem that other products that I intend to add it to the reverse proxy (like Nomad) also uses the <code>/v1/</code> path and in this case there will be a conflict.</p>