我遵循this tutorial以便将我的Django应用与uWSGI和Nginx一起部署在DigitalOcean VPS上。静态文件运行良好,但是Django应用本身无法运行。 (404未找到-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;
    }
}

最佳答案

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

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


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

关于python - Nginx对Django应用程序响应404,但静态文件有效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34517746/

10-09 20:01