Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with IP and Nginx as reverse proxy

Tags:

I configured my Nginx as simple reverse proxy.

I'm just using basic setting

location / {
    proxy_pass foo.dnsalias.net;
    proxy_pass_header Set-Cookie;
    proxy_pass_header P3P;
}

The problem is that after some time (few days) the site behind nginx become unaccessible. Indead nginx try to call a bad ip (the site behind nginx is at my home behind my box and I'm a using a dyn-dns because my ip is not fixe). This dyn-dns is always valid (I can call my site directly) but for obscure reason Nginx get stuck with that..

So as said, nginx just give me 504 Gateway Time-out after some time. It looks like the error come when my ip change at home. Here is a sample of error log:

[error] ... upstream timed out (110: Connection timed out) while connecting to upstream, client: my.current.ip, server: myreverse.server.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://my.old
.home.ip", host: "myreverse.server.com"

So do you know why nginx is using ip instead of the DN ?

like image 340
max54 Avatar asked Nov 16 '14 12:11

max54


People also ask

How do you check if Nginx reverse proxy is working?

To check the status of Nginx, run systemctl status nginx . This command generates some useful information. As this screenshot shows, Nginx is in active (running) status, and the process ID of the Nginx instance is 8539.

Can Nginx do reverse proxy?

The benefits of using Nginx as a reverse proxy include: Clients access all backend resources through a single web address. The reverse proxy can serve static content, which reduces the load on application servers such as Express, Tomcat or WebSphere.

Is Nginx proxy or reverse proxy?

Nginx is an open source web server that can also serve as a reverse proxy. Apart from being used to host websites, it's also one of the most widely used reverse proxy and load balancing solutions.


2 Answers

If the proxy_pass value doesn't contain variables, nginx will resolve domain names to IPs while loading the configuration and cache them until you restart/reload it. This is quite understandable from a performance point of view.

But, in case of dynamic DNS record change, this may not be desired. So two options are available depending on the license you possess or not.

Commercial version (Nginx+)

In this case, use an upstream block and specify which domain name need to be resolved periodically using a specific resolver. Records TTL can be overriden using valid=time parameter. The resolve parameter of the server directive will force the DN to be resolved periodically.

http {    

    resolver X.X.X.X valid=5s;

    upstream dynamic {
        server foo.dnsalias.net resolve;
    }

    server {

        server_name www.example.com;

        location / {
            proxy_pass http://dynamic;
            ...
        }

    }

}

This feature was added in Nginx+ 1.5.12.

Community version (Nginx)

In that case, you will also need a custom resolver as in the previous solution. But to workaround the unavailable upstream solution, you need to use a variable in your proxy_pass directive. That way nginx will use the resolver too, honoring the caching time specified with the valid parameter. For instance, you can use the domain name as a variable :

http {  

    resolver X.X.X.X valid=5s;

    server {

        server_name www.example.com;
        set $dn "foo.dnsalias.net"; 

        location / {
            proxy_pass http://$dn;
            ...
        }

    }

}

Then, you will likely need to add a proxy_redirect directive to handle redirects.

like image 67
Xavier Lucas Avatar answered Oct 07 '22 22:10

Xavier Lucas


Maybe check this out http://forum.nginx.org/read.php?2,215830,215832#msg-215832

resolver 127.0.0.1;
set $backend "foo.example.com";
proxy_pass http://$backend;

In such setup ip address of "foo.example.com" will be looked up
dynamically and result will be cached for 5 minutes.
like image 38
DOA Avatar answered Oct 07 '22 22:10

DOA