Report this

What is the reason for this report?

Django, Nginx, Uwsgi 502 Bad Gateway error

Posted on February 2, 2021

I am trying to run my Django app using Nginx On CentOS7. When I run uwsgi --http :8000 --module ubergfapi.wsgi everything works, but when I running uwsgi --socket uwsgi_nginx.sock --module ubergfapi.wsgi --chmod-socket=666 I got 502 Bad Gateway when I request my_domain:8000.

nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

tail -f /var/log/nginx/error.log

021/02/02 01:57:30 [crit] 24601#0: *7 connect() to unix:///root/ubergf/client/uwsgi_nginx.sock failed (13: Permission denied) while connecting to upstream, client: 212.164.39.205, server: jza.cuk.mybluehost.me, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///root/ubergf/client/uwsgi_nginx.sock:", host: "my.domain"
2021/02/02 01:57:36 [crit] 24601#0: *7 connect() to unix:///root/ubergf/client/uwsgi_nginx.sock failed (13: Permission denied) while connecting to upstream, client: 212.164.39.205, server: jza.cuk.mybluehost.me, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///root/ubergf/client/uwsgi_nginx.sock:", host: "my.domain"
2021/02/02 01:59:30 [crit] 24876#0: *1 connect() to unix:///root/ubergf/client/uwsgi_nginx.sock failed (13: Permission denied) while connecting to upstream, client: 212.164.39.205, server: jza.cuk.mybluehost.me, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///root/ubergf/client/uwsgi_nginx.sock:", host: "my.domain"
2021/02/02 01:59:32 [crit] 24877#0: *3 connect() to unix:///root/ubergf/client/uwsgi_nginx.sock failed (13: Permission denied) while connecting to upstream, client: 212.164.39.205, server: jza.cuk.mybluehost.me, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///root/ubergf/client/uwsgi_nginx.sock:", host: "my.domain"
2021/02/02 02:00:01 [error] 24877#0: *5 open() "/usr/share/nginx/html/whm-server-status" failed (2: No such file or directory), client: 127.0.0.1, server: _, request: "GET /whm-server-status HTTP/1.0"
2021/02/02 02:00:23 [crit] 24876#0: *7 connect() to unix:///root/ubergf/client/uwsgi_nginx.sock failed (13: Permission denied) while connecting to upstream, client: 212.164.39.205, server: jza.cuk.mybluehost.me, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///root/ubergf/client/uwsgi_nginx.sock:", host: "my.domain"

I checked. The socket uwsgi_nginx.sock is in the directory

Nginx.conf

ser nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location / {
        }
        error_page 404 /404.html;
        location = /404.html {
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

the symlink nginx config

# the upstream component nginx needs to connect to
upstream django {
    server unix:///root/ubergf/client/uwsgi_nginx.sock; # for a file socket
    # server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name my_domain.me; # substitute your machine's IP address or FQDN
    charset     utf-8;
    # max upload size
    client_max_body_size 75M;   # adjust to taste
    # Django media
    location /media  {
    alias /root/ubergf/client/media;  # your Django project's media files - amend as required
    }
    location /static {
    alias /root/ubergf/client/staticfiles; # your Django project's static files - amend as required
    }
    # Finally, send all non-media requests to the Django server.
    location / {
    uwsgi_pass  django;
    include     /root/ubergf/client/deployment/uwsgi_params; # the uwsgi_params file you installed
    }
}

/var/log/nginx/error.log

/var/log/nginx/error.log: line 1: `2021/02/01 03:50:01 [error] 29110#0: *1751 open() "/usr/share/nginx/html/whm-server-status" failed (2: No such file or directory), client: 127.0.0.1, server: _, request: "GET /whm-server-status HTTP/1.0"'



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

If you are using SELinux it is possible that it is causing the problem. I would recommend checking your /var/log/messages log for more information.

If this is the case, you could set it permissive with the following command:

sudo semanage permissive -a httpd_t

Let me know how it goes.

Regards, Bobby

Heya,

1. Correct uWSGI Socket Permissions

The log indicates that Nginx does not have permission to access the uwsgi_nginx.sock file. Update your uWSGI command to set appropriate permissions:

uwsgi --socket uwsgi_nginx.sock --module ubergfapi.wsgi --chmod-socket=666

Ensure the directory containing uwsgi_nginx.sock is accessible by Nginx. For example:

chmod 755 /root/ubergf/client

2. Move the Socket File

Storing the socket file in /root/ is not ideal because Nginx runs as the nginx user and may not have permissions to access /root/. Move the socket file to a more accessible location, such as /run/ or /var/run/:

uwsgi --socket /run/uwsgi_nginx.sock --module ubergfapi.wsgi --chmod-socket=666

Update your Nginx configuration to reflect the new socket location:

upstream django {
    server unix:/run/uwsgi_nginx.sock;
}

3. Ensure uWSGI is Running

Check if the uwsgi process is running and that the socket file is created:

ps aux | grep uwsgi
ls -l /run/uwsgi_nginx.sock

4. Adjust Nginx User

Ensure that Nginx is running as a user with access to the socket file. The default is often nginx or www-data. Confirm the Nginx user in /etc/nginx/nginx.conf:

user nginx;

If using a different user, ensure it has the necessary permissions.


5. Verify SELinux Configuration

If SELinux is enabled, it may block access to the socket. Temporarily disable SELinux to confirm:

sudo setenforce 0

If this resolves the issue, create a proper SELinux rule for the socket:

sudo chcon -t httpd_var_run_t /run/uwsgi_nginx.sock

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.