Question: 502 error django + nginx + gunicorn - connect() recv()


502 error django + nginx + gunicorn - connect() recv()

Answers 2
Added at 2017-01-03 16:01

I've set up a Digital Ocean one-click app (django + nginx + gunicorn are installed and setup Defaults worked for me, but after I tried to apply code changes via service gunicorn restart I received a 502 error with the following nginx error log line:

 connect() to unix:/home/django/gunicorn.socket failed (111: Connection refused) while connecting to upstream, client:, server: _, request: "GET / HTTP/1.1",upstream: "http://unix:/home/django/gunicorn.socket:/"..

I have looked into similar issues and found that often it's caused by a mistake in ALLOWED_HOSTS, everything is right there, but I also tried to replace 'ip' or '' with single '*' and got a different error:

recv() failed (104: Connection reset by peer) while reading response header from upstream, client:, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: ""

EDIT: ok, now it's easier, gunicorn error log is telling us that there is an import error with rest_framework, But it's installed and it's present in Installed app (and spelled right), I'v checked like thousand times so what's really wrong with all that?

     File "/usr/lib/python2.7/dist-packages/django/core/", line 14, in get_wsgi_application
  File "/usr/lib/python2.7/dist-packages/django/", line 18, in setup
  File "/usr/lib/python2.7/dist-packages/django/apps/", line 108, in populate
  File "/usr/lib/python2.7/dist-packages/django/apps/", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/", line 37, in import_module
  File "/usr/lib/python2.7/dist-packages/gevent/", line 93, in __import__
    result = _import(*args, **kwargs)
  File "/home/django/django_project/blog/", line 5, in <module>
    from rest_framework import serializers
  File "/usr/lib/python2.7/dist-packages/gevent/", line 93, in __import__
    result = _import(*args, **kwargs)
ImportError: No module named rest_framework

(the only issue for now)EDIT2: I have reinstalled the app folder, checked again everything. Now it's telling that ImportError: No module named django_ajax. Python 2.7 , Django 1.8, Gunicorn 19.4.5 ( despite everything seems to be correct enter image description here

EDIT: Résponse of pip freeze:

-e git://
Answers to

502 error django + nginx + gunicorn - connect() recv()

nr: #1 dodano: 2017-01-06 00:01

I am not sure, whether it shall be considered as answer, because I don't understand why exactly it works this way. If someone explains properly I'll mark one's answer as right one . So, this line worked for me

  sudo -H pip install djangorestframework

Despite packages were reported to be installed they were not.And despite they were visible after pip freeze. So I don't know real reason why just pip install didn't work but seemed to work instead(?), although all permissions are correct, and I tried from both root and other username with same privileges.

nr: #2 dodano: 2017-01-06 04:01

When you do Sudo, the package is installed for the root user and it installed globally. Check this link to understand how sudo pip works. Now that your package is installed globally, it is working.

I still have doubts about which Python is it using. In the error messages, it was trying to access packages under global dist-packages directory. Not from the virtual environment. So, your gunicorn send to be linked to the Python installed globally not the virtual environment one.

Ideally, the errors occurred should point to the dist-packages under the /path/to/virtual_env/virtual_env/lib/python2.7/dist-packages, as this is the python & the library environment that want to use. That seems to be the issue.

If this is the case, can you do the following

DJANGODIR=/path/to/django-project/ DJANGO_SETTINGS_MODULE=django-project.settings

Activate the virtual environment


I think this should solve everything... Please try it out and lemme know if I missed out something

Source Show
◀ Wstecz