Nginx resolver address from /etc/resolv.conf

Is it possible to set resolver address in nginx proxy configuration from /etc/resolv.conf?

It can be useful for example in docker or in virtualenvironment.


Unfortunately there's no easy way to do this because nginx use it's own resolver implementation. The two solutions I see are :

1) You generate the resolver list from a script and include it, e.g. :

echo resolver $(awk 'BEGIN{ORS=" "} $1=="nameserver" {print $2}' /etc/resolv.conf) ";" > /etc/nginx/resolvers.conf

http {

    include resolvers.conf;

}

2) You recompile nginx with a third party module like the (very) experimental perl module and write a variable handler :

http {

    perl_modules perl/lib;
    perl_set $resolvers '

        sub {
            return system("awk BEGIN{ORS=\" \"} /nameserver/{print \$2}" /etc/resolv.conf");
        };

    resolver "$resolvers";
}

Now, if you are a hell of a C coder (prepare your eyes for some blood), you can still write an alternative patch or module to make it work this way.


If you're using the Openresty version of nginx then you can use their special local argument to the resolver directive which when set to local=on, means the standard path of /etc/resolv.conf will be used by the resolver (for more details see Openresty resolver docs):

resolver local=on;