Django总是使用nginx返回错误404

时间:2013-03-22 06:19:34

标签: django nginx fastcgi

我有Django的问题。应用程序的url配置如下所示:

urlpatterns = patterns(
    '',
    url(r'^api/', include(api_urls.getUrls())),
    url(r'^api/vk.com/buy.coins/$', VkPaymentsView.as_view()),
    url(r'^api/admin/', include(admin.site.urls)),
    url(r'^api-testing/((?P<method_code>[\w\./-]+?)/)?$', TestingView.as_view()),
)

我尝试请求正确的网址:

http://example.com/api-testing/

但我有404错误:

Page not found (404)
Request Method: GET
Request URL:    http://example.com/api-testing//

我看到一个额外的尾随斜线,可能是问题的原因吗?

应用程序使用nginx:

以FastCGI模式启动
python manage.py runfcgi method=threaded host=localhost port=9001 pidfile=$RUNFILES_PATH/$SITE.pid

这是nginx中的fastcgi参数:

fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /var/www/$domain/$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $document_root;

fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param REQUEST_URI $uri;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PROTOCOL $server_protocol;

proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;

fastcgi_param REDIRECT_STATUS 200;

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。问题出在FastCGI param SCRIPT_NAME中。我在Django位置指令fastcgi_param SCRIPT_NAME ''中添加了。问题已经消失了。

location @django {
    fastcgi_pass localhost:9001;
    fastcgi_read_timeout 600;
    fastcgi_send_timeout 600;
    include fastcgi_params;
    fastcgi_param SCRIPT_NAME '';
}