Django应用程序在运行服务器下运行,但在nginx下运行404

在“ python manage.py runserver …..”下运行时,我的Django应用在服务器上运行良好,但是在nginx / fastcgi下运行时,得到了404.

这是错误消息:

Page not found (404)
Request Method: GET
Request URL: blah.youtrain.me/admin/ (I removed the http due to StackOverflow constraints for new account)

Using the URLconf defined in youtrainme.urls, Django tried these URL patterns, in this order:

^admin/doc/
^admin/

The current URL, , didn’t match any of these.

无论我在nginx下进入应用程序的哪个页面,它始终显示相同的错误消息:“当前URL,与任何这些都不匹配.”

在nginx下,如果我只是转到blah.youtrain.me,则上面的请求URL更改为:blah.youtrain.me//< ---注意结尾的斜杠.但是,在runserver下,“请求URL”行中没有结尾斜杠.这可能会提供一些见解? 任何帮助将不胜感激! 🙂 这是此站点的我的nginx配置:

server
{
listen 80;
server_name blah.youtrain.me;
access_log /home/ytmadmin/public_html/blah.youtrain.me/log/access.log;
error_log /home/ytmadmin/public_html/blah.youtrain.me/log/error.log debug;
# rewrite rule – files
location ~* .+.>(xml|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|swf)
{
access_log off;
expires 30d;
break;
}
# python requests
location /
{
fastcgi_pass 127.0.0.1:8010;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
include /usr/local/nginx/conf/fastcgi_params;
}
}

还有我的urls.py

from django.conf.urls.defaults import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns(”,
(r’^$’, ‘main.views.index’),
(r’^admin/doc/’, include(‘django.contrib.admindocs.urls’)),

# Uncomment the next line to enable the admin:
(r’^admin/’, include(admin.site.urls)),
)

最佳答案
我在这里找到解决方案:https://serverfault.com/questions/134863/nginx-fastcgi-problems-with-django-double-slashes-in-url

在nginx下,我必须从nginx / conf / fastcgi_params文件中删除以下行,一切正常:

fastcgi_param SCRIPT_NAME $fastcgi_script_name;

转载注明原文:Django应用程序在运行服务器下运行,但在nginx下运行404 - 代码日志