2

我对 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;
4

1 回答 1

2

我找到了解决方案。问题出在 FastCGI 参数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 '';
}
于 2013-03-22T07:42:39.787 回答