python - 带有gunicorn和nginx的Django不会返回404或500

标签 python django nginx http-status-code-404 gunicorn

如果我使用 get_object_or_404(),Django 会显示正确的错误页面,但 NGINX 仍然返回:

HTTP/1.1 200 OK
Server: nginx

tl;tr

每个页面都会返回 200 或(如果是 500)超时。这里出了什么问题?

NGINX 配置:

location / {
        proxy_pass http://unix:/opt/repo/page.sock;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
}

url.py

from django.conf import settings

handler404 = 'my_app.views.page_not_found'

最佳答案

您应该设置处理程序,可以通过以下方式完成:

from django.utils.functional import curry

handler404 = curry(page_not_found, template_name='404.html')
handler500 = curry(page_not_found, template_name='500.html')
handler403 = curry(page_not_found, template_name='403.html')

我已经在 urls.py 中完成了此操作,但也可以在 s 中完成

关于python - 带有gunicorn和nginx的Django不会返回404或500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38405430/

相关文章:

python - 属性错误: module 'plotly.tools' has no attribute 'set_credentials_file'

python - Python 枚举的 Matlab 等价物

javascript - Django提交表单->重定向到新页面->自动更新新页面

javascript - 从 Javascript 接收数据后重定向到新页面

symfony - 具有多个 symfony2 应用程序的 nginx

python - 在类中声明装饰器

python - Django form.errors 没有出现在模板中

python - 一个 CreateView 中的两个 ModelForm

nginx - 如何在从 PuPHPet 生成的配置文件中设置 Nginx 重写规则?

javascript - nginx反向代理后面的create-react-app代码更改未在浏览器中重新加载