【问题标题】:Django only serves index page, 404 on all othersDjango 仅提供索引页面,其他所有页面为 404
【发布时间】:2016-09-26 20:58:47
【问题描述】:

我有一个简单的 django 应用程序,我正试图在数字海洋上呕吐。我已将 NGINX 配置为代理我的端口并提供静态文件。但是,当我单击任何链接转到另一个页面时,它会在我身上显示 404。它只正确服务于索引页面,其他一切都是 404。

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

我对 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
    }

}

【问题讨论】:

    标签: django postgresql nginx server digital-ocean


    【解决方案1】:

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

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

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

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多