Home > OS >  502 bad gateway: Nginx, gunicorn, Django
502 bad gateway: Nginx, gunicorn, Django

Time:04-17

Here is my nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen 80;
        server_name 0.0.0.0;

        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /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:/root/myproject/myproject.sock;
        }
    }

}

And my project have this structure:

This is my Directory

This is my gunicorn.service file

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

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

[Install]
WantedBy=multi-user.target

error.log

2022/04/13 12:45:30 [crit] 20474#20474: *8 connect() to unix:/root/myproject/myproject.sock failed (2: No such file or directory) while connecting to upstream, client: 10.81.234.10, server: 0.0.0.0, request: "GET / HTTP/1.1", upstream: "http://unix:/root/myproject/myproject.sock:/", host: "10.10.89.8"

My gunicorn service is working properly and nginx.conf is also working properly with no errors.

All permissions have been granted to myproject directory Still I am getting a 502 Bad Gateway error.

Please let me know if any additional information needed

CodePudding user response:

The issue is you are creating the Socket file in different location and pointing the nginx to different location. Your socket file is created at /run/gunicorn.sock as you can see from this command

ExecStart=/root/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock myproject.wsgi:application

and referring to it in different location(/root/myproject/myproject.sock) in nginx configuration as you can see here

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

You can solve this in 2 ways

  1. by changing gunicorn conf from
ExecStart=/root/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock myproject.wsgi:application

to

ExecStart=/root/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/root/myproject/myproject.sock myproject.wsgi:application
  1. By changing nginx conf from
            proxy_pass http://unix:/root/myproject/myproject.sock;

to

            proxy_pass http://unix:/run/gunicorn.sock;

I recommend using the first approach

Restart both gunicorn and nginx after these changes

  • Related