1

I have reverse proxy like this:

server {
    listen 8080;

    location = /my-proxy {
      proxy_pass https://somehost/v1
    }
}

Now I would like to pass it to /v2 if HTTP request header x-version-toggle is set to v2, something like this:

server {
    listen 8080;

    location = /my-proxy {
      if ($http-x-version-toggle == 'v2') {
        proxy_pass https://somehost/v2
      }
      proxy_pass https://somehost/v1
    }
}

What is the precise syntax to achieve this?

Jeroen Kransen
  • 1,379
  • 3
  • 19
  • 45

1 Answers1

2

You can use map operator provided by nginx

map $http_x_version_toggle $backend {
        default http://10.41.115.241:8000/; 
        v2 http://127.0.0.1:9101/;
}

And then in the location use it as below

proxy_pass $backend
Shrey Gupta
  • 392
  • 4
  • 10
  • This is incomplete, for using `proxy_pass` with variables you also need: https://stackoverflow.com/questions/5743609/dynamic-proxy-pass-to-var-with-nginx-1-0 – zenw0lf Apr 08 '22 at 19:54