502 bad gateway - 111 connection refused

November 10, 2017 5.5k views
Django Nginx Ubuntu 16.04

I'm currently reading the brilliant book "TDD with Python" by Harry Percival and since a few days I'm at that hairy bit where you have to get a droplet at Digital Ocean, create your domain and connect them using some strange spells.

I'm quite confident that I set up my act according to the tutorials:
nameserver:
$ whois webscraper.pw
--> points to the Digital Oceans nameservers

At my domain registrar, I created an A-record pointing to the IP of my droplet.

This is what I do when I SSH into my droplet:
$ sudo systemctl start nginx
[sudo] password for pippin:
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ jobs
Returns nothing ... (is that right??)
$ geckodriver -V
geckodriver 0.18.0
$ firefox -V
Mozilla Firefox 56.0

This is what Django returns:
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
...
self.browser = webdriver.Firefox()
...
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused

And this is what I get from nginx:
$ cat /var/log/nginx/error.log
2017/11/10 09:18:01 [error] 5449#5449: *35 connect() failed (111: Connection refused) while connecting to upstream, client: 88.130.52.47, server: staging.webscraper.pw, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "staging.webscraper.pw"
2017/11/10 09:21:52 [error] 5449#5449: *39 connect() failed (111: Connection refused) while connecting to upstream, client: 88.130.52.47, server: staging.webscraper.pw, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "207.154.246.158"
2017/11/10 09:21:52 [error] 5449#5449: *39 connect() failed (111: Connection refused) while connecting to upstream, client: 88.130.52.47, server: staging.webscraper.pw, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8000/favicon.ico", host: "207.154.246.158"
2017/11/10 09:25:15 [error] 5449#5449: *42 connect() failed (111: Connection refused) while connecting to upstream, client: 88.130.52.47, server: staging.webscraper.pw, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "207.154.246.158"
2017/11/10 10:58:30 [error] 1443#1443: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 155.94.88.58, server: staging.webscraper.pw, request: "GET / HTTP/1.0", upstream: "http://127.0.0.1:8000/"
2017/11/10 12:40:31 [error] 1443#1443: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 88.130.52.47, server: staging.webscraper.pw, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "staging.webscraper.pw"

If you have any ideas I'd be glad if you share them with me.

2 comments
  • pinging the domain returns:
    PING webscraper.pw (207.154.246.158) 56(84) bytes of data.
    64 bytes from 207.154.246.158 (207.154.246.158): icmp_seq=1 ttl=58 time=25.2 ms

    and the subdomain:
    PING staging.webscraper.pw (207.154.246.158) 56(84) bytes of data.
    64 bytes from 207.154.246.158 (207.154.246.158): icmp_seq=1 ttl=58 time=25.7 ms

  • I managed to get rid of the 502 error by restarting the dev server with manage.py runserver ...

    So now I can wget ip-address or access my domain site and get the response I was hoping for. However, doing the functional tests throw me back into the "connection refused" loop again, this time coming from django ...
    Oh well, to be continued ...

1 Answer

Sorry to see that your question hasn't received an answer yet. Unfortunately, after this much time, it is unlikely that an answer for this specific question will be provided. For people still landing here via search, a Connection Refused in nginx's error logs usually means that the upstream server (whatever is running on port 8000 in this case) isn't running. I'd recommend to start by making sure that it is running and that the ports are configured correctly. If that doesn't fix it, check the logs of the upstream server.

Have another answer? Share your knowledge.