Django + uwsgi + nginx redirect to default page “W

2019-03-13 16:04发布

问题:

I'm a very beginner in python and django. However I'm trying to create a server to deploy my application. But when I want to access my app, I always get the default nginx page "Welcome to nginx".

This server is on Ubuntu 12.04 (precise) I've installed nginx, python, django and uwsgi packages with apt. Next I've created a django project to /var/www/djangoApps and a django app to /var/www/djangoApps/testApp

This is my /etc/nginx/sites-available/djangoApps :

server {
    listen 80
    server_name django.pommesky.com;
    rewrite ^(.*) http://www.django.pommesky.com/$1 permanent;
}

server {
    listen 80;
    server_name www.django.pommesky.com;
    access_log /var/log/nginx/djangoApps_access.log;
    error_log /var/log/nginx/djangoApps_error.log;

    location /media {
        alias /var/www/djangoApps/media/;
    }

    location /static {
        alias /var/www/djangoApps/static/;
    }

    location / {
        uwsgi_pass unix:///run/uwsgi/app/djangoApps/socket;
        include uwsgi_params;
    }
}

And this is my /etc/uwsgi/apps-available/djangoApps.ini :

env = DJANGO_SETTINGS_MODULE=djangoApps.settings
module = django.core.handlers.wsgi:WSGIHandler()
chdir = /var/www/djangoApps
socket = /run/uwsgi/djangoApps/socket
logto = /var/log/uwsgi/djangoApps.log

The uwsgi log doesn't show anything, everything seems to run well, it finishes by spawned uWSGI worker ... But /var/log/nginx/djangoApps_access.log; and /var/log/nginx/djangoApps_error.log; don't exist, which is very strange. I can't figure out what's wrong with my configuration. Please help me ...

回答1:

The domain django.pommesky.com doesn't look like it's alive, so it's possible that Nginx is receiving requests with wrong Host: field in the HTTP request header. (sect. 14.23) So Nginx serves a default catch-all page.

You can disable the default Nginx site by removing the /etc/nginx/sites-enabled/default link, and then restarting the daemon.

sudo rm -v /etc/nginx/sites-enabled/default
sudo service nginx restart

You can reenable by recreating the link:

sudo ln -sf /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
sudo service nginx restart

The other thing you can try is to setup Bind or another DNS daemon to serve a fake pommesky.com zone with all the subdomains you want and use that DNS while you're developing your site.

Of course you can also register that domain with a hosting provider, and then use the DNS zone editor in its control panel to setup your subdomains and all the PTRs you want to whatever public IP addresses you need.