Nginx, reverse proxies and DNS resolution

Nginx is a pretty awesome high performance web server and reverse proxy. It’s often used in conjunction with other HTTP servers such as Java/Tomcat and Ruby/Unicorn, as it allows static content to be served directly from disk by Nginx and for connections from slow clients to be queued and buffered by Nginx, rather than taking up time of the expensive/scarce application server worker processes.


A typical Nginx reverse proxy configuration to a single backend using proxy_pass to a local HTTP server application on port 8080 would look something like this:

server {
    proxy_pass http://localhost:8080

Another popular approach is having a defined upstream group (which can be used for multiple servers, or a single one if desired), for example:

upstream upstream-localhost {
    server localhost:8080;

server {
    proxy_pass http://upstream-localhost;

Generally this configuration works fine for most of our use cases – we typically have a 1-to-1 mapping between a backend application server and Nginx, so the configuration is very simple and reliable – any issues are usually with the backend application, rather than Nginx itself.


However on occasion there are times when it’s desirable to have Nginx talking to a backend on another server.

I recently implemented an OAuth2 gateway using Nginx-Lua, with the Nginx gateway doing the OAuth2 authentication in a small Lua module before passing the request through to the backend application. This configuration ran on a pair of bastion servers, which reverse proxy the request through to an Amazon ELB which load balances a number of application servers.

This works perfectly 95% of the time, but Amazon ELBs (even internal) have a tendency to change their IP addresses. Normally this doesn’t matter, since you never reference ELBs via their IP address and use their DNS name instead, but the default behaviour of the Nginx upstream and proxy modules is to resolve DNS at startup, but not to re-resolve DNS during the operation of the application.

This leads to a situation where the Amazon ELB IP address changes, Amazon update the DNS record, but Nginx never re-resolves the DNS record and stays pointing at the old IP address. Subsequently requests to the backend start failing once Amazon drops services from the old IP address.

This lack of re-resolution of backends is a known limitation/issue with Nginx. Thankfully there is a workaround to force Nginx to re-resolve addresses, as per this mailing list post by setting proxy_pass to a variable, which then forces re-resolution of the DNS names as Nginx treats variables differently to static configuration.

server {
    set $backend_upstream "";
    proxy_pass $backend_upstream;


A resolver (DNS server address) also needs to be configured. When using parametrised backends, a resolver must be configured in Nginx (it is unable to use the local OS resolver) and must point directly to a name server IP address.

If your name servers aren’t predictable, you could install something like dnsmasq to provide a local resolver on which then forwards to the dynamically assigned name server, or take the approach of pulling the name server details from the host using something like Puppet Facts and then writing it into the configuration file when it’s generated on the host.

Nginx >= 1.1.9 will re-resolve DNS records based on their TTL, but it’s possible to override this with any value desired. To verify correct behaviour, tcpdump will quickly show whether re-resolution is working.

# tcpdump -i eth0 port 53
15:26:00.338503 IP > 15459+ A? (54)
15:26:00.342765 IP > 15459 1/0/0 A (70)
15:26:52.958614 IP > 63771+ A? (54)
15:26:52.959142 IP > 63771 1/0/0 A (70)

It’s a bit of an annoyance in an otherwise fantastic application, but as long as you are aware of the limitation, it is not too difficult to resolve the issue by a bit of configuration adjustment.

This entry was posted in Uncategorized and tagged , , , , , , , , , , , , . Bookmark the permalink.

9 Responses to Nginx, reverse proxies and DNS resolution

  1. Happy Reader says:

    Thank you! This was exactly what I needed!

  2. Arun Sharma says:

    This helped :) thank you

  3. Dave Konopka says:

    Thanks for the post. It was helpful.

  4. A.J. says:

    but the issue is in case of ELB what resolver <ip? i put . I only have dns name of my elb?

  5. prographo says:

    it’s not just ELBs, same with RDS — and it’s a big limitation of nginx

  6. perfect. thank you

  7. Sergey says:

    Ha, nice, you had the same problem with dynamic DNS pointers. Good article. I wrote about our AWS ELB related case just a few days ago

  8. Winston Nolan says:

    dude, you’re my hero <3

  9. Sebastian says:

    Thanks for writing this up

Leave a Reply