Django 仅提供索引页面,其他所有页面均为 404

Django only serves index page, 404 on all others

我有一个简单的 Django 应用程序,我正试图在数字海洋中抛出。我已将 NGINX 配置为代理我的端口并提供静态文件。但是,当我单击任何 link 转到另一个页面时,它对我来说是 404。它只正确提供索引页面,其他所有内容都是 404。

如果你们中的任何后端向导有任何其他的事情's/don不是我目前正在做的事情,请随时在您的回复中添加这些。

我是 NGINX 的新手,所以请把它简化 :) 谢谢。

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location /static {
            alias /home/rchampin/ryan_the_developer_django/static;
    }

    location / {
             # First attempt to serve request as file, then
             # as directory, then fall back to displaying a 404.
             try_files $uri $uri/ =404;
             proxy_set_header X-Real-IP  $remote_addr;
             proxy_set_header X-Forwarded-For $remote_addr;
             proxy_set_header Host $host;
             proxy_pass http://127.0.0.1:8080;
             # Uncomment to enable naxsi on this location
             # include /etc/nginx/naxsi.rules
    }

}

您已明确指示 nginx return 404 处理非文件请求。你不需要这样做。 Django 可以管理 Error 404s。您只需要在模板目录中添加一个 404.html,Django 就会在有 404 Not Found Error 时显示此页面。

此外,您不需要对索引页进行硬编码,使用 Django 有什么意义。 Michał Karzyński 有关于如何使用 Django 设置 Nginx 的非常好的文档:

Setting up Django with Nginx, Gunicorn, virtualenv, supervisor and PostgreSQL