【发布时间】: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 才会导致问题。
【问题讨论】: