4

I set LOGIN_URL like this in settings.py:

LOGIN_URL = '/login/'

and in urls.py I and this URLConf:

(r'^$', 'agent.index.redirect'),
(r'^login/$', 'django.contrib.auth.views.login', {'template_name':'login.html'}),

and the agent.index.redirect view is like this:

@login_required
def redirect(request):
    ...

and I run my Django site like this:

python manage.py runfcgi host=127.0.0.1 port=8090 --settings=settings

the nginx.conf is like this:

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;
    }
}

When I access [http://localhost], there's a redirect loop. And the access.log of nginx is like this:

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.

Can anyone help to solve this problem?

2
  • Your problem reminds me of this question. Try removing fastcgi_param SCRIPT_NAME $fastcgi_script_name;. Commented Feb 28, 2013 at 14:32
  • @Alasdair It works. Thanks a lot! But I don't know why. Commented Mar 1, 2013 at 4:02

2 Answers 2

2

Aha, the reason is:

Django uses PATH_INFO to match against urlpatterns. Nginx’s fastcgi_params include doesn’t set that. It does set SCRIPT_NAME. If both PATH_INFO and SCRIPT_NAME are set to $fastcgi_script_name, Django seems to get an empty path for all requests. Just set PATH_INFO!

See more at http://aftnn.org/2009/jan/23/nginx-django-fastcgi/.

So solution is just removing this line:fastcgi_param SCRIPT_NAME $fastcgi_script_name;
Thank @Alasdair very mach.

Sign up to request clarification or add additional context in comments.

Comments

1

This usually indicates that you have another URL pattern that matches the requested URL (probably all urls) and redirects to the login page. For example:

(r'^', 'agent.index.redirect'),
    ^ left out the end-of-string $

This would cause a redirect loop like you're experiencing. Do you have any other url patterns other than those listed in your question?

1 Comment

This problem only occurs when I use nginx and fastcgi. If run site using python manage.py runserver, there's no redirect loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.