I'm trying to get my django project up and running on an aws ec2 instance. I'm using gunicorn with nginx, and I'm not really sure how I can tackle this problem. I've spent a couple hours on it already, including looking at other posts on this site.. but I'm still stuck. Here's what's wrong: Along with the 502 Bad Gateway, my nginx error logs keep giving me back this:

2015/07/17 08:32:32 [error] 8049#0: *18 connect() failed (111: Connection refused) while connecting to upstream, client: ip.ip.ip.ip, server: ip.ip.ip.ip, request: "GET / HTTP/1.1", upstream: "", host: "ec2-numbers.us-west-1.compute.amazonaws.com"

My /etc/nginx/sites-available/at_api.conf looks like this (Is the indentation okay on this?):

server {
listen 80;
server_name ip.ip.ip.ip;
access_log /var/log/nginx/site_access.log;
error_log /var/log/nginx/site_error.log;
location /static/ {
alias /home/ubuntu/static/;
location / {
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;

This is my first time setting up my django project on ec2... so I'm not really sure if this is the right way to be doing this. Any tips? p.s. I've seen another similar post saying that php-fpm wasn't configured properly, but I'm using django, so I'm not using any php.

Edit: My at_api/gunicorn.conf.py

proc_name = "at_api"
bind = ''
loglevel = "error"
workers = 2

Edit 2: Netstat

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0    *               LISTEN      -
tcp        0      0*               LISTEN      -
tcp        0      0    *               LISTEN      8463/nginx: worker
tcp6       0      0 :::22                   :::*                    LISTEN      -
udp        0      0    *                           -
udp        0      0 *                           -
udp6       0      0 :::21956                :::*                                -
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name    Path
unix  2      [ ACC ]     STREAM     LISTENING     8754     -                   /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     52566    -                   /var/run/supervisor.sock.8446
unix  2      [ ACC ]     STREAM     LISTENING     6691     -                   @/com/ubuntu/upstart
unix  2      [ ACC ]     STREAM     LISTENING     9075     -                   /var/run/acpid.socket
unix  2      [ ACC ]     STREAM     LISTENING     35450    -                   /var/run/postgresql/.s.PGSQL.5432
unix  2      [ ACC ]     SEQPACKET  LISTENING     14550    -                   /run/udev/control

Connection refused means that your gunicorn software isn't listening to the 8001 port you have configured in your nginx. You have to check your gunicorn configuration.

  • hmmm ... i added that to my post. do you think it looks all right? – pyramidface Jul 17 '15 at 9:13
  • Unfortunately I am not familiar with gunicorn software, so I can't tell for sure. The bind statement looks fine to me. You can check with netstat -lnp if the gunicorn is really running and listening to the port. – Tero Kilkanen Jul 17 '15 at 9:21
  • For the local address, it does say LISTEN. So that means gunicorn should be listening? If it's listening, what does that mean in the scope of this issue? – pyramidface Jul 17 '15 at 9:36
  • No, nginx is listening on port 80, as the output of netstat -lnp tells you (PID / Program name column). So, gunicorn isn't started if that is the only port which is in LISTEN state. – Tero Kilkanen Jul 17 '15 at 9:40
  • I just posted the entire netstat because I wasn't sure which one might possibly be related to gunicorn. What do you make of it? (I guess gunicorn not being there means it isn't listening eh? And if it isn't listening... then that means my problem's solution probably lies in tweaking with gunicorn?) – pyramidface Jul 17 '15 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.