title: 502 Bad Gateway nginx/1.14.0 (Ubuntu) digitalocean vps django, nginx and gunicorn
body: <p>I put up a site in production a week ago made using django and postgres on digitalocean and followed the django nginx, gunicorn and postgres tutorial <a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#configure-nginx-to-proxy-pass-to-gunicorn" rel="nofollow noreferrer">here</a> and used digitalspace for our static file. everything was running smoothly and okay for the whole week while we were testing the website in production (left Debug=true for testing reasons). suddenly a few hours ago after i was testing some stuff i got a 502 bad gateway error. i checked the nginx error logs and heres the error:</p>
  
  <blockquote>
    <p>2019/12/23 00:30:00 [error] 7988#7988: *2202 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 125.0.0.1, server: www.myapp.com, request: "GET /reviews/business/id=3/ HTTP/1.1", upstream: "<a href="http://unix:/run/gunicorn.sock:/reviews/business/id=3/" rel="nofollow noreferrer">http://unix:/run/gunicorn.sock:/reviews/business/id=3/</a>", host: "myapp.com", referrer: "<a href="https://myapp.com/category/Salon/" rel="nofollow noreferrer">https://myapp.com/category/Salon/</a>"
    2019/12/23 00:30:12 [error] 7988#7988: *2208 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 125.0.0.1, server: www.myapp.com, request: "GET /reviews/business/id=3/ HTTP/1.1", upstream: "<a href="http://unix:/run/gunicorn.sock:/reviews/business/id=3/" rel="nofollow noreferrer">http://unix:/run/gunicorn.sock:/reviews/business/id=3/</a>", host: "myapp.com", referrer: "<a href="https://plentypot.biz/category/Salon/" rel="nofollow noreferrer">https://plentypot.biz/category/Salon/</a>"
    2019/12/23 00:30:25 [error] 7988#7988: *2212 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 125.0.0.1, server: www.myapp.com, request: "GET / HTTP/1.1", upstream: "<a href="http://unix:/run/gunicorn.sock:/" rel="nofollow noreferrer">http://unix:/run/gunicorn.sock:/</a>", host: "myapp.com"</p>
  </blockquote>
  
  <p>i ran nginx -t and it returned successful meaning everything is okay with the configurations</p>
  
  <p>the gunicorn error logs by using the command journalctl -u gunicorn, which outputed:</p>
  
  <pre><code>Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 systemd[1]: Started gunicorn daemon.
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]: [2019-12-16 13:00:49 +0000] [10979] [INFO] Starting gunicorn 20.0.4
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]: [2019-12-16 13:00:49 +0000] [10979] [INFO] Listening at: unix:/run/gunicorn
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]: [2019-12-16 13:00:49 +0000] [10979] [INFO] Using worker: sync
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]: [2019-12-16 13:00:49 +0000] [10997] [INFO] Booting worker with pid: 10997
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]: [2019-12-16 13:00:49 +0000] [10997] [ERROR] Exception in worker process
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]: Traceback (most recent call last):
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:   File "/home/cybafrican/djangoprojectdir/mydjangoenv/lib/python3.7/site-pa
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:     worker.init_process()
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:   File "/home/cybafrican/djangoprojectdir/mydjangoenv/lib/python3.7/site-pa
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:     self.load_wsgi()
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:   File "/home/cybafrican/djangoprojectdir/mydjangoenv/lib/python3.7/site-pa
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:     self.wsgi = self.app.wsgi()
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:   File "/home/cybafrican/djangoprojectdir/mydjangoenv/lib/python3.7/site-pa
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:     self.callable = self.load()
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:   File "/home/cybafrican/djangoprojectdir/mydjangoenv/lib/python3.7/site-pa
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:     return self.load_wsgiapp()
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:   File "/home/cybafrican/djangoprojectdir/mydjangoenv/lib/python3.7/site-pa
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:     return util.import_app(self.app_uri)
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:   File "/home/cybafrican/djangoprojectdir/mydjangoenv/lib/python3.7/site-pa
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:     mod = importlib.import_module(module)
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:   File "/home/cybafrican/djangoprojectdir/mydjangoenv/lib/python3.7/importl
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:     return _bootstrap._gcd_import(name[level:], package, level)
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:   File "&lt;frozen importlib._bootstrap&gt;", line 1006, in _gcd_import
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:   File "&lt;frozen importlib._bootstrap&gt;", line 983, in _find_and_load
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:   File "&lt;frozen importlib._bootstrap&gt;", line 965, in _find_and_load_unlocke
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]: ModuleNotFoundError: No module named 'myapp.wsgi'
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]: [2019-12-16 13:00:49 +0000] [10997] [INFO] Worker exiting (pid: 10997)
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]: [2019-12-16 13:00:49 +0000] [11000] [INFO] Booting worker with pid: 11000
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]: [2019-12-16 13:00:49 +0000] [11000] [ERROR] Exception in worker process
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]: Traceback (most recent call last):
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:   File "/home/cybafrican/djangoprojectdir/mydjangoenv/lib/python3.7/site-pa
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:     worker.init_process()
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:   File "/home/cybafrican/djangoprojectdir/mydjangoenv/lib/python3.7/site-pa
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:     self.load_wsgi()
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:   File "/home/cybafrican/djangoprojectdir/mydjangoenv/lib/python3.7/site-pa
  Dec 16 13:00:49 ubuntu-s-1vcpu-1gb-nyc3-01 gunicorn[10979]:     self.wsgi = self.app.wsgi()
  
  </code></pre>
  
  <p>my gunicron configuration file:</p>
  
  <pre><code>
  [Unit]
  Description=gunicorn daemon
  Requires=gunicorn.socket
  After=network.target
  
  [Service]
  User=cybafrican
  Group=www-data
  WorkingDirectory=/home/cybafrican/djangoprojectdir
  ExecStart=/home/cybafrican/djangoprojectdir/mydjangoenv/bin/gunicorn \
            --access-logfile - \
            --workers 3 \
            --bind unix:/run/gunicorn.sock \
           myapp.wsgi:application
  
  [Install]
  WantedBy=multi-user.target
  
  </code></pre>
  
  <p>my project path /home/cybafrican/djangoprojectdir:</p>
  
  <pre><code>└── djangoprojectdir
      ├── Pages
      ├── Recommend
      ├── Responses
      ├── Templates
      ├── Users
      ├── media
      ├── mydjangoenv
      ├── myapp
      ├── profiles
      └── static
  </code></pre>
  
  <p>i have went through everything online but cant seem to find any help with regards to this
  Thank you</p>