Why am I getting 502 Bad Gateway on Django app with Nginx and Gunicorn?

nginx 502 bad gateway django uwsgi
uwsgi 502 bad gateway
502 bad gateway nginx/1.10 3 ubuntu django
django, nginx
nginx gunicorn flask 502
502 bad gateway nginx/1.14 0 ubuntu django
gunicorn django
502 bad gateway django admin

I have been trying to deploy a demo app with this tutorial. I am doing this on a CentOS 7 in Virtualbox.

However, I am getting 502 Bad Gateway. How can I fix this?

server {} block in nginx.conf file

server {
        listen 80;
        server_name 172.16.16.215;

        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /home/michel/myproject;
        }

        location / {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_pass http://unix:/home/michel/myproject/myproject.sock;
        }
    }

gunicorn.service file

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=michel
Group=nginx
WorkingDirectory=/home/michel/myproject
ExecStart=/home/michel/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/michel/myproject/myproject.sock myproject.wsgi:application

[Install]
WantedBy=multi-user.target

var/logs/nginx/error.log

2016/08/28 18:55:14 [crit] 17557#0: *4 connect() to unix:/home/michel/myproject/myproject.sock failed (13: Permission denied) while connecting to upstream, client: 172.16.16.23, server: 172.16.16.217, request: "GET / HTTP/1.1", upstream: "http://unix:/home/michel/myproject/myproject.sock:/", host: "172.16.16.217"

Gunicorn does not have right to save .sock file, or nginx does not have right to read .sock file.

Move this file to /tmp/ folder:

nginx.conf

proxy_pass http://unix:/tmp/myproject.sock:/;

gunicorn.service

ExecStart=/home/michel/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:/tmp/myproject.sock myproject.wsgi:application

502 Bad Gateway Ngnix & Gunicorn, Files is the folder where I'm having all the static files. How can I get my project to work in the server? Could anyone guide me? UPDATE. Errors. 502 Bad Gateway - django + nginx + gunicorn - sock failed (13: Permission denied) 4 Nginx, django, gunicorn, ubuntu 14.04 (13: Permission denied) while connecting to upstream

sudo cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
sudo semodule -i mynginx.pp

This was copied from this answer.

NGINX 502 Bad Gateway: Gunicorn, I'm trying to set up WSGI/nginx with a Django project, but am getting a 502 error. Seems like there's a problem finding the `.sock` file. So I am trying to set up Nginx with uwsgi for my Django app. Here is my /etc/nginx/nginx.conf file: # /etc/nginx/nginx.conf user richard richard; ## Default: nobody worker_processes 5; ## Stack Exchange Network

I ran into this issue as well. What solved the issue for me was running:

chmod 711 on the home directory

This allowed nginx to access the .sock file in the run folder. This permission error did not happen to me on Ubuntu, but was present on Amazon Linux 2, which is derived from Cent OS. These django deployment scripts may help others facing this issue.

django-gunicorn-nginx: 502 bad gateway, Hello, I'm developing a Django app with a PostgreSQL database and I'm using PostgreSQL, NGINX and Gunicorn are on different containers communicating up and view my app in the browser all I get is a 502 Bad Gateway error and in  I've gone through 3 tutorials on Digital Ocean, but they aren't correct, obviously. I keep getting 502 bad gateway, or if I try to use manage.py runserver, I get 400 bad request. I think my problem is with gunicorn. When I enter gunicorn -config, it says. usage: gunicorn [OPTIONS] [APP_MODULE] gunicorn: error: No application module specified.

There are many possibilities to get 502 badgateway , check with below cmds..

  1. sudo systemctl status gunicorn
  2. sudo systemctl status nginx
  3. gunicorn --log-file=- projectname.wsgi:application

    These above result should be in active mode

How do I fix this 502 Bad Gateway error when setting up Django , serving django app using gunicorn & nginx gives 502 Bad Gateway error guess the problem must be in some gunicorn config not being able to pick up the app. It also points out common errors that beginners get stuck on such as typing in  So in closing, yes. You need both Nginx and Gunicorn (or something similar) for a proper Django deployment. If you're specifically looking to host Django with Nginx, then I would investigate Gunicorn, mod_uwsgi, and maybe CherryPy as candidates for the Django side of things.

I disabled SELinux and the application worked.

sudo vi /etc/sysconfig/selinux

Set SELINUX=disabled.

Save and exit. RESTART.

NGINGX gives 502 Bad Gateway when trying to use NGINX + , I'm trying to serve a Django application with Caddy, but I get an 502 Bad Gateway when I hit the domain. I'm using Gunicorn in between as 

serving django app using gunicorn & nginx gives 502 Bad Gateway , ProgrammingError Exception Following a Recent Upgrade; 502 Bad Gateway Error When Calling NAPALM API; Rack Elevations Do Not Display Django (the Python framework on which NetBox runs) is attempt to access or modify a database field For example, you can set timeout=60 in your Gunicorn configuration file.

502 Bad Gateway serving Django app through gunicorn, Load testing throws Server 502 error: Bad Gateway after 700 users. Gunicorn, Gevent, Nginx, Django I'm using locust to simulate them. DEBUG --log-file '/​var/www/mysite/production/log/gunicorn.log' mysite.wsgi:application Instead at about 700 users I get the following error in my locust failures tab:

Common Issues · netbox-community/netbox Wiki · GitHub,

Comments
  • Would you mind look at the nginx error message? Maybe in var/logs/nginx/error.log
  • @Aison 2016/08/28 14:11:35 [crit] 2181#0: *15 connect() to unix:/root/myproject/myproject.sock failed (2: No such file or directory) while connecting to upstream, client: 172.16.16.23, server: 172.16.16.215, request: "GET / HTTP/1.1", upstream: "http://unix:/root/myproject/myproject.sock:/", host: "172.16.16.215"
  • It tells you nginx can't find your sock file. I guess the SOCKET PATH of your gunicorn or supervisors is wrong.
  • Where can I change that setting?
  • @Aison Is the sock file auto created? Or do I have to create it manually?
  • srwxrwxrwx. 1 michel nginx 0 Aug 28 18:48 myproject.sock this is the permission for the sock file. I don't think moving it to /tmp folder will do any good if it has 777 permission, will it?
  • Look at this: unix.stackexchange.com/questions/88083/… /tmp is the best choice.
  • I've disabled SELinux and it works now. However, I will take a look at your link.
  • If You don't care about security... ok, but none of administrators would not agree to it.
  • Better way is understand why You don't have permissions. Read some tutorials.
  • SELinux handles the internal security. You shouldn't disable this. Instead, you should use something like audit2allow and have that generate a SELinux policy to allow binding to sockets.
  • Fixed it without disabling SELinux. Check my other answer.