Question

Django, Nginx, Uwsgi 502 Bad Gateway error

Posted February 2, 2021 1.8k views
NginxDjango

I am trying to run my Django app using Nginx On CentOS7. When I run uwsgi --http :8000 --module ubergfapi.wsgieverything 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"'

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.

×
Submit an Answer
1 answer

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

  • Thank you for your answer! I checked it, SELinux is disabled

    • Hi there,

      In this case, I believe that the problem could be due to the socket file being inside the /root folder. Usually, web services do not have access to that directory.

      I could suggest moving the app outside the root directory. For example you could try creating a new user in the /home directory and setting up the app there.

      Let me know how it goes.
      Regards,
      Bobby