Search code examples
nginxproxyamazon-ec2resque

proxy_pass in nginx to private IP of EC2 instance


I have two Amazon EC2 instances. Let me call them X and Y. I have nginx installed on both of them. Y has resque running on port 3000. Only X has a public IP and domain example.com. Suppose private IP of Y is 15.0.0.10

What I want is that all the requests come to X. And only if the request url matches the pattern /resque, then it should be handled by Y at localhost:3000/overview, which is the resque web interface. It seems like this can be done using proxy_pass in nginx config.

So, In nginx.conf, I have added the following :

location /resque {
    real_ip_header X-Forwarded-For;
    set_real_ip_from 0.0.0.0/0;
    allow all;

    proxy_pass  http://15.0.0.10:3000/overview;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

But now, when I visit http://example.com/resque from my web browser, it shows 502 Bad Gateway.

In /var/log/nginx/error.log on X,

2014/02/27 10:27:16 [error] 12559#0: *2588 connect() failed (111: Connection 
refused) while connecting to upstream, client: 123.201.181.82, server: _, 
request: "GET /resque HTTP/1.1", upstream: "http://15.0.0.10:3000/overview", 
host: "example.com" 

Any suggestions on what could be wrong and how to fix this ?


Solution

  • Turns out that the server should be running on 0.0.0.0 if it needs to be reachable by addressing the IP of the instance.

    So to solve my problem, I stopped the server running resque on 127.0.0.1:3000 and restarted it to bind to 0.0.0.0:3000. Rest everything remains the same as above and it works. Thanks.

    For reference : Curl amazon EC2 instance