Nginx Proxy Route by Query Parameter

NginxProxyRoutebyQueryParameter

Herecomesourrequirement,IneedtoroutetodifferentbackendbasedontheURLqueryparameters.Ifparameter=1,Ineedgoserver1,parameter=2,Ineedgoserver2.

HereishowItestthat

resolver8.8.8.8;

set$ocpServer'https://ocp.sillycat.com';

set$testServer'http://requestbin.fullcontact.com/1ki7o9g1';

location/{

if($http_user_agent='oldclients'){

proxy_passhttp://local-sillycat-external-ip:5080;

}

if($http_user_agent!='oldclients'){

proxy_pass$ocpServer;

}

#fortestingtheheader,commentsout

if($arg_api='describeNode'){

proxy_pass$testServer;

}

proxy_set_headerX-Real-IP$remote_addr;

proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;

proxy_set_headerX-Sillycat-Agent'nginx';

proxy_ssl_server_nameon;

}

Inthiscase,Iwillgotodifferentroutebasedonhttp_user_agentheader,butthetopconditionisapi=describeNode,itthisqueryparameterexists,Iwillgoto$testServerwhichmytestingendpoint.

References:

https://stackoverflow.com/questions/23988344/making-nginx-check-parameter-in-url#

https://serverfault.com/questions/811912/can-nginx-location-blocks-match-a-url-query-string

相关推荐