PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 1, 2022

[FIXED] How can I make NGINX reverse proxy conditional on HTTP header value

 September 01, 2022     nginx-config, nginx-reverse-proxy     No comments   

Issue

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?


Solution

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


Answered By - Shrey Gupta
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing