【问题标题】:400 Error on certain routes (Django + gunicorn + nginx)某些路由上的 400 错误(Django + gunicorn + nginx)
【发布时间】:2015-01-21 00:54:47
【问题描述】:

我有一个 django 站点,我正在尝试使用 gunicorn 和 nginx 进行部署。除带有连字符的路线外,所有路线都可以正常工作。这些返回 400 错误。

我将debug 设置为True,所以这不是我一直在阅读的allowed_hosts 问题。我的正则表达式也不是问题。该网站在 Apache 上的工作方式与原样。

这是我的 nginx 配置:

server {
    listen 80 default;
    client_max_body_size 4G;
    server_name mydomain.local

    keepalive_timeout 5;

    location / {
        try_files $uri @proxy_to_app;
    }

location /static/ {
   alias /path/to/project/static/;
}

location /uploads/ {
   alias /path/to/project/uploads/;
}

location @proxy_to_app {
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header Host $http_host;
   proxy_redirect off;
   proxy_pass   http://127.0.0.1:8002;
}

}

这是我的 urls.py

from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
import settings

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'Portfolio.views.get_home', name='home'),
    url(r'^contact/$', 'Portfolio.views.get_contact', name='contact'),
    url(r'^grappelli/', include('grappelli.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^(?P<slug>[\w-]+)/$', 'Portfolio.views.get_gallery', name='gallery')
)

正如我所说,只有包含连字符的 slug 才会导致问题。

【问题讨论】:

    标签: django nginx gunicorn


    【解决方案1】:

    server_name 指令后缺少分号。

    nginx 的 400 错误通常与配置错误有关,导致传递给应用程序的数据不完整或错误。您的陈述强化了这一假设,即在 apache 下它可以正常工作。

    因此,您应该以高日志级别 (--log-level debug) 运行 gunicorn,然后查看 Web 服务器传递的内容。查看运行 apache 和 nginx 之间的请求差异。这应该可以帮助您找到错误的原因,这本身就非常普遍。

    【讨论】:

    • 我在使用mydomain.local 替换我的实际域时犯了这个错误。
    猜你喜欢
    • 1970-01-01
    • 2015-06-17
    • 2014-02-19
    • 1970-01-01
    • 2014-11-02
    • 2014-07-31
    • 2020-10-20
    • 2017-12-31
    • 2017-10-01
    相关资源
    最近更新 更多