Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django@login\u需要使用nginx和fastcgi时导致重定向循环_Python_Django_Nginx_Fastcgi - Fatal编程技术网

Python Django@login\u需要使用nginx和fastcgi时导致重定向循环

Python Django@login\u需要使用nginx和fastcgi时导致重定向循环,python,django,nginx,fastcgi,Python,Django,Nginx,Fastcgi,我在settings.py: 在urls.py I和这个URLConf中: agent.index.redirect视图如下所示: 我的Django网站是这样运行的: nginx.conf如下所示: 当我访问[http://localhost],有一个重定向循环。nginx的access.log如下: 有人能帮忙解决这个问题吗?这通常表示您有另一个与请求的URL(可能是所有URL)匹配的URL模式,并重定向到登录页面。例如: (r'^', 'agent.index.redirect'),

我在settings.py:

在urls.py I和这个URLConf中:

agent.index.redirect视图如下所示:

我的Django网站是这样运行的:

nginx.conf如下所示:

当我访问[http://localhost],有一个重定向循环。nginx的access.log如下:


有人能帮忙解决这个问题吗?

这通常表示您有另一个与请求的URL(可能是所有URL)匹配的URL模式,并重定向到登录页面。例如:

(r'^', 'agent.index.redirect'),
    ^ left out the end-of-string $
这将导致一个重定向循环,就像您正在经历的那样。除了问题中列出的url模式之外,您还有其他url模式吗?

啊哈,原因是:

Django使用路径信息来匹配URL模式。Nginx的fastcgi_参数include没有设置这一点。它确实设置了脚本名称。如果PATH_INFO和SCRIPT_NAME都设置为$fastcgi_SCRIPT_NAME,Django似乎会为所有请求获取一个空路径。只需设置路径信息

更多信息请访问

所以解决方案就是删除这一行:
fastcgi\u参数SCRIPT\u NAME$fastcgi\u SCRIPT\u NAME

感谢@Alasdair very mach.

这个问题只有在我使用nginx和fastcgi时才会出现。如果使用python manage.py runserver运行站点,则不会出现重定向循环。您的问题提醒我。尝试删除
fastcgi\u参数SCRIPT\u NAME$fastcgi\u SCRIPT\u NAME@Alasdair它可以工作。谢谢!但我不知道为什么。我很高兴它成功了:)
(r'^$', 'agent.index.redirect'),
(r'^login/$', 'django.contrib.auth.views.login', {'template_name':'login.html'}),
@login_required
def redirect(request):
    ...
python manage.py runfcgi host=127.0.0.1 port=8090 --settings=settings
user nobody nobody;
worker_processes  5;
#error_log /var/log/nginx/error_log info;

events {
    worker_connections  1024;
    use epoll;
}

http {
    include         mime.types;
    default_type    application/octet-stream;

    log_format main
        '$remote_addr - $remote_user [$time_local] '
        '"$request" $status $bytes_sent '
        '"$http_referer" "$http_user_agent" '
        '"$gzip_ratio"';

    client_header_timeout   10m;
    client_body_timeout     10m;
    send_timeout            10m;

    connection_pool_size            256;
    client_header_buffer_size       1k;
    large_client_header_buffers     4 2k;
    request_pool_size               4k;

    gzip on;
    gzip_min_length 1100;
    gzip_buffers    4 8k;
    gzip_types      text/plain;

    output_buffers  1 32k;
    postpone_output 1460;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;

    keepalive_timeout       75 20;

    ignore_invalid_headers  on;
    index index.html;

    server {
        listen 80;
        server_name localhost;
        root /web/agent;

        location /static  {
            alias /web/agent/media;
            access_log   off;
            expires      30d;
        }

        location /media  {
            alias /web/agent/admin_media;
            access_log   off;
            expires      30d;
        }

        location / {
            # host and port to fastcgi server
            fastcgi_pass 127.0.0.1:8090;
            fastcgi_param PATH_INFO $fastcgi_script_name;
            fastcgi_param REQUEST_METHOD $request_method;
            fastcgi_param QUERY_STRING $query_string;
            fastcgi_param CONTENT_TYPE $content_type;
            fastcgi_param CONTENT_LENGTH $content_length;
            fastcgi_pass_header Authorization;
            fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
            fastcgi_param  REQUEST_URI        $request_uri;
            fastcgi_param  DOCUMENT_URI       $document_uri;
            fastcgi_param  DOCUMENT_ROOT      $document_root;
            fastcgi_param  SERVER_PROTOCOL    $server_protocol;
            fastcgi_param  HTTPS              $https if_not_empty;
            fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
            fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
            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_connect_timeout 30s;
            fastcgi_send_timeout 30s;
            fastcgi_read_timeout 30s;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 8 128k;#8 128
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
            fastcgi_intercept_errors on;
        }
        #access_log     /usr/local/nginx/logs/access_log main;
        #error_log      /usr/local/nginx/logs/error_log;
    }
}
127.0.0.1 - - [28/Feb/2013:18:13:51 +0800] "GET / HTTP/1.1" 302 5 "-" "Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130220 Firefox/17.0"
127.0.0.1 - - [28/Feb/2013:18:13:51 +0800] "GET /login/?next=// HTTP/1.1" 302 5 "-" "Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130220 Firefox/17.0"
127.0.0.1 - - [28/Feb/2013:18:13:51 +0800] "GET /login/?next=/login//%3Fnext%3D// HTTP/1.1" 302 5 "-" "Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130220 Firefox/17.0"
127.0.0.1 - - [28/Feb/2013:18:13:51 +0800] "GET /login/?next=/login//%3Fnext%3D/login//%253Fnext%253D// HTTP/1.1" 302 5 "-" "Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130220 Firefox/17.0"
127.0.0.1 - - [28/Feb/2013:18:13:51 +0800] "GET /login/?next=/login//%3Fnext%3D/login//%253Fnext%253D/login//%25253Fnext%25253D// HTTP/1.1" 302 5 "-" "Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130220 Firefox/17.0"
...and so on.
(r'^', 'agent.index.redirect'),
    ^ left out the end-of-string $