Nginx为Django应用程序响应404但静态文件有效

时间:2015-12-29 19:30:00

标签: python django nginx http-status-code-404 uwsgi

我跟着this tutorial,以便在使用uWSGI和Nginx的DigitalOcean VPS上部署我的Django应用程序。静态文件运行良好,但Django应用程序本身没有。 (404 Not Found - nginx / 1.9.6)

# Where to look for content (static and media)
root    /srv/www/$host/;

# Allow gzip compression
gzip_types text/css application/json application/x-javascript;
gzip_comp_level 6;
gzip_proxied any;
# Look for files with .gz to serve pre-compressed data
gzip_static on;

server {        
    listen 80;
    # nginx docs recommend try_files over "if"
    location    /   {
        # Try to serve existing files first
        try_files $uri @proxy =404;
    }
    location @proxy {
        # Pass other requests to uWSGI
        uwsgi_pass unix://srv/apps/_/server.sock;
        include uwsgi_params;
    }
}

1 个答案:

答案 0 :(得分:2)

try_files的目的是测试是否存在本地文件,如果没有找到则执行默认操作。在您的情况下,该默认操作是调用名为@proxy的命名位置。这就是你需要的:

location / {
    try_files $uri @proxy;
}
location @proxy {
    uwsgi_pass unix://srv/apps/_/server.sock;
    include uwsgi_params;
}

有关详细信息,请参阅this document