devops

Routing requests through nginx by querystring

published on
I’m writing this up as much for my own future reference as I am to help others, since I found the documentation a bit lacking in this area. For reasons not worth getting into, I needed to build a proof-of-concept to transparently proxy requests to different servers based on a querystring value, using nginx as a reverse proxy. The TL;DR solution: worker_processes 2; events { worker_connections 1024; } http { upstream server1 { server web1:8080; } upstream server2 { server web2:8080; } map $arg_queryval $node { default server2; "abcd1234" server1; } server { listen 80; location / { proxy_pass http://$node; } } } This transparently routes any request to the proxy with a querystring argument of queryval=abcd1234 to server 1. Read More...