ShellRox
ShellRox

Reputation: 2602

Connecting Nginx and Django

This is my first time working in Nginx. I know that in order to connect Django and Nginx easily I require uWSGI.

Setup

uWSGI:

I think my uWSGI setup was completely fine, I've used it to run my Django website on HTTP port 80:

uwsgi --http :80 --root /root/Env/firstsite --chdir /root/mysite -w mysite.wsgi

But since this is not a very practical method for the "best" security, I had to use Nginx. In this case I've used "The uWSGI Emperor" mode and then i've created a new file /etc/uwsgi/sites/mysite.ini:

[uwsgi]
project = mysite
base = /root

chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application

master = true
processes = 5

socket = %(base)/%(project)/%(project).sock
chmod-socket = 666
vacuum = true

In this case, Instead of HTTP ports I've used unix socket which uses uwsgi protocol.

Finally, I've created /etc/systemd/system/uwsgi.service file (which from my understanding initiates uwsgi on startup):

[Unit]
Description=uWSGI Emperor service
After=syslog.target

[Service]
ExecStart=/usr/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

Nginx:

My Nginx setup was pretty simple, I've just modified the existing server block to fit uWSGI configuration:

server {
    listen 80;
    server_name mysite.com www.mysite.com;

    location = favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /root/mysite;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/run/uwsgi/mysite.sock;
    }
}

Initiation:

sudo systemctl start nginx
sudo systemctl start uwsgi
sudo systemctl enable nginx
sudo systemctl enable uwsgi

This process doesn't output any errors.

Problem:

Webserver returns default html response of Nginx - when I expect it to return default html response of Django. Is there any reason for this? I feel like I'm stumbling across a very simple problem here since I couldn't find it anywhere on stackoverflow.

Possible causes:

  1. /run/uwsgi/mysite.sock in nginx configuration doesn't exist for some reason, which makes me think that this can be the cause.

  2. In systemctl status nginx there is an error Failed to read PID from file /run/nginx.pid: Invalid argument which should be the cause for the problem above? (systemctl status wsgi has no errors).

  3. From the problems above, I'm thinking that these problems are caused by my Nginx configuration, Is this correct?

  4. It directs to 50x.html which is for errors 500, 502, 503, 504 (in this case it is HTTP 502, bad gateway - this has to do something with configuration).

Main probable cause:

In systemctl status nginx there is an error Failed to read PID from file /run/nginx.pid: Invalid argument which should be the cause for the problem above? (systemctl status wsgi has no errors).

Checked this problem & solution but it doesn't work.

Please make sure that /run/uwsgi/ directory is empty, maybe this is the reason.

Thank you!

Upvotes: 1

Views: 502

Answers (1)

ShellRox
ShellRox

Reputation: 2602

Fixed the problem!

Nginx server configuration:

server {
    listen 80;
    server_name mysite.com www.mysite.com;

    location = favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /root/mysite;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/run/uwsgi/mysite.sock;
    }
}

didn't match uWSGI configuration:

[uwsgi]
project = mysite
base = /root

chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application

master = true
processes = 5

socket = %(base)/%(project)/%(project).sock
chmod-socket = 666
vacuum = true

As visible, the path to the unix socket that is specified in Nginx configuration is incorrect. Therefore, these socket paths must be matched in configuration: /run/uwsgi/%(project).sock.

Upvotes: 1

Related Questions