Dynamic proxy_pass to $var with nginx 1.0

I’ve recently stumbled upon this need myself and have found that in order to use variables in a proxy_pass destination you need to set a resolver as your error.log would most probably contain something like no resolver defined to resolve ...

The solution in my case was to setup the following using a local DNS for DNS resolution:

location ~ /proxy/(.*) {
    resolver [::1];
    proxy_pass http://$1;

In your case this should work:

location /proxy {
    resolver [::1];
    set $target http://proxytarget.example.com;
    proxy_pass $target;

For resolver to work, you need to install bind9 locally.
For Debian/Ubuntu:

sudo apt-get install bind9

More information on nginx and dynamic proxy_passing here: http://www.nginx-discovery.com/2011/05/day-51-proxypass-and-resolver.html

Replaced the previous public DNS with a local one for security issues.

Leave a Comment