nginx django uwsgi page not found error

2019-08-01 01:44发布

问题:

I am trying to setup uwsgi and Django on Nginx but showing page not found error and error logs are empty. I cannot identify the error because the error logs are empty.

Error log /var/log/nginx/error.log:

-rw-r--r-- 1 www-data root 0 Feb 26 12:31 error.log

uswgi is running properly because I tested this on following method:

uwsgi --http :8080 --home /home/flybegins/python/django/venv/ --chdir /home/flybegins/python/django/sample -w sample.wsgi

virtual host 

    server {
        listen 80;
        server_name test.aaaaaaa.com;
        error_log  /var/log/nginx/error.log
        location /static/ {
            root /home/flybegins/python/django/sample/
        }

        location / {
            include         uwsgi_params;
            uwsgi_pass      unix:/home/flybegins/python/django/sample/sample.sock;
        } }

Virtual host permission:

-rw-r--r-- 1 root root 333 Feb 27 08:54 test.aaaa.com

Thanks in advance!

回答1:

You need to install python plugin for uwsgi

sudo apt-get install uwsgi-plugin-python

or for python 3

sudo apt-get install uwsgi-plugin-python3


回答2:

You are running your project using port 8080 with this code:

uwsgi --http :8080 --home /home/flybegins/python/django/venv/ --chdir /home/flybegins/python/django/sample -w sample.wsgi

And you are trying to bind NGINX to a socket file that doesn't exist using this configuration:

location / {
            include         uwsgi_params;
            uwsgi_pass      unix:/home/flybegins/python/django/sample/sample.sock;
        }

That why it doesn't work.



回答3:

I did two mistakes One is nginx virtual host configuration and another one is socket permission error

uWSGI Configuration

[uwsgi]
project = prd
base = /home/flybegins/python/django

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

master = true
processes = 5

gid = www-data
uid = www-data

socket = /var/uwsgi/%(project).sock
chmod-socket = 664
vacuum = true

To create the space for the socket to exist, you just have to pick a persistent directory (e.g. not /run or /tmp) and make www-data (the user nginx runs as) the owner of it, as such:

$ sudo mkdir /var/uwsgi
$ sudo chown www-data:www-data /var/uwsgi

My nginx virtual host configuration

server {
    listen 80;
    server_name testserver1.com;
    access_log /home/flybegins/log/python/testserver1.com/access.log;
    error_log /home/flybegins/log/python/testserver1.com/error.log error;

 location  /static {
      alias  /home/flybegins/python/django/prd/static_files/;
    }

  location / {
        include         uwsgi_params;
        uwsgi_pass      unix:/var/uwsgi/prd.sock;
    }


}