Nginx – Upstream timed out (110: Connection timed out) with nginx + Tomcat + Amazon EC2

amazon ec2nginxtomcat

On an Amazon EC2 instance, we have nginx and Tomcat running. Nginx is proxying all requests to Tomcat on port 8080 running on the same instance.

This worked fine for a day, but then started seeing a lot of upstream timed out (110: Connection timed out) errors in the nginx logs and the site was inaccessible. Now Tomcat is accessible all right at port 8080. So the problem seems to be somewhere between nginx and Tomcat.

This is what nginx.conf looks like,

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    server {
        listen       80;
        server_name  domainname.com;
        rewrite ^/(.*) http://www.domainname.com/$1 permanent;
    }

    server {
        listen       80;
        server_name  www.domainname.com;
        location / {
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://127.0.0.1:8080;
            proxy_read_timeout 120;
            proxy_connect_timeout 120;
        }

    }
}

Any insights would be helpful.

Update:
After restarting the instance, the site has started to work. I have absolutely no idea what the problem was, but I'll monitor it for a while for such problems.

Best Answer

Please use the following for redirecting:

server {
    listen       80;
    server_name  domainname.com;
    return 301 $scheme://www.$server_name$request_uri;
}

Add the following to your configuration:

error_log /var/log/nginx/error.log debug;

And evaluate, or post the result here.