简体   繁体   English

Django管理员身份验证失败

[英]Django admin authentication failure

logging into django admin fails when 'log in' button is pressed first time, but pressing "back" and "log in' again - logs the user in successfully. 首次按下“登录”按钮时,登录django admin失败,但是再次按下“返回”和“登录”-成功登录用户。

I am deploying Django app with zc.buildout here, with a setup similar to what is described here http://www.meppum.com/2009/jan/17/installing-django-ubuntu-intrepid/ . 我将在此处使用zc.buildout部署Django应用,其设置类似于http://www.meppum.com/2009/jan/17/installing-django-ubuntu-intrepid/ Nginx (listening on 127.0.0.1:8003) is running beyond Apache(listening on 127.0.0.1:8001), configurations of both servers are provided at the end of the post. Nginx(在127.0.0.1:8003上监听)的运行超出了Apache(在127.0.0.1:8001上监听)的运行范围,文章结尾处提供了这两个服务器的配置。 I'm accessing my django app as 127.0.0.1:8003/. 我正在以127.0.0.1:8003/的身份访问我的Django应用程序。

So, 127.0.0.1:8003/admin/ opens django admin login form. 因此,127.0.0.1:8003/admin/将打开django管理员登录表单。 1)Entering username/pass, pressing "log in" redirects to (for some unknown reason?) 127.0.0.1/ admin, therefore ends up with nothing, as there is no server listening on 80 port. 1)输入用户名/密码,然后按“登录”重定向到(出于某些未知原因?)127.0.0.1/ admin,因此没有任何结果,因为没有服务器在80端口上侦听。 HTTP trace (after clicking login in: http://127.0.0.1:8003/admin/ ) HTTP跟踪(单击登录后: http : //127.0.0.1 : 8003/admin/

POST /admin/ HTTP/1.1
Host: 127.0.0.1:8003
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.17) Gecko/2010010604 Ubuntu/9.04 (jaunty) Firefox/3.0.17
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://127.0.0.1:8003/admin/
Cookie: JSESSIONID=0D287C17FBA82223AB7D5884EA534967; sessionid=c65c7af8ebde9e6bdf9c739a731d376b
Content-Type: application/x-www-form-urlencoded
Content-Length: 55
username=to&password=password&this_is_the_login_form=1

HTTP/1.x 302 FOUND
Server: nginx/0.8.29
Date: Mon, 01 Feb 2010 10:27:44 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Expires: Mon, 01 Feb 2010 10:27:44 GMT
Vary: Cookie
Etag: "d41d8cd98f00b204e9800998ecf8427e"
Cache-Control: max-age=0
Set-Cookie: sessionid=9db437da69c359136786696f90ba89f2; expires=Mon, 15-Feb-2010 10:27:44 GMT; Max-Age=1209600; Path=/
Last-Modified: Mon, 01 Feb 2010 10:27:44 GMT
Location: http://127.0.0.1/admin/
Content-Length: 0

2)Pressing browser's back , and 'log in' button once again (or pressing browser's 'back' and 'refresh') logs in succesfully. 2)再次按浏览器的后退和“登录”按钮(或按浏览器的“后退”和“刷新”)成功登录。 HTTP trace: HTTP跟踪:

POST /admin/ HTTP/1.1
Host: 127.0.0.1:8003
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.17) Gecko/2010010604 Ubuntu/9.04 (jaunty) Firefox/3.0.17
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://127.0.0.1:8003/admin/
Cookie: JSESSIONID=0D287C17FBA82223AB7D5884EA534967; sessionid=9db437da69c359136786696f90ba89f2
Content-Type: application/x-www-form-urlencoded
Content-Length: 55
username=to&password=password&this_is_the_login_form=1

HTTP/1.x 200 OK
Server: nginx/0.8.29
Date: Mon, 01 Feb 2010 10:52:02 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Expires: Mon, 01 Feb 2010 10:52:02 GMT
Vary: Cookie
Etag: "3355a46fb32d7cb6ab21262c359d9eec"
Cache-Control: max-age=0
Last-Modified: Mon, 01 Feb 2010 10:52:02 GMT

It looks like that's a cookie-related issue, but, i guess, the root of the problem might as well lie in the apache and/or nginx config as well. 看起来这是一个与Cookie相关的问题,但是,我想,问题的根源可能还在于apache和/或nginx配置。

Any ideas how to make the admin log-in form work immediately? 任何想法如何使管理员登录表单立即工作?

Cheers! 干杯!

to

PSconfigs: PSconfigs:

apache vhost configuration: apache vhost配置:

<VirtualHost *:8001>

    <Directory /home/toinbis/Desktop/programming/project/runtime/
htdocs/django_wsgi/>
        Order deny,allow
        Allow from all
    </Directory>

    WSGIDaemonProcess tsd user=www-data group=www-data threads=25
    WSGIProcessGroup tsd

    WSGIScriptAlias / /home/toinbis/Desktop/programming/project/
runtime/htdocs/django_wsgi/djproject.wsgi

    LogLevel warn
    ErrorLog  /home/toinbis/Desktop/programming/project/runtime/logs/
apache_tsd_error.log
    CustomLog /home/toinbis/Desktop/programming/project/runtime/logs/
apache_tsd_access.log combined

    ServerName localhost
</VirtualHost>

nginx configuration: nginx配置:

daemon off;
#user www-data;
worker_processes  2;

error_log  /home/toinbis/Desktop/programming/project/runtime/logs/
nginx_general_error.log;
pid        /home/toinbis/Desktop/programming/project/runtime/var/pids/
nginx.pid;

events {
    worker_connections  1024;
    #server_names_hash_bucket_size: 128;

}

http {
    include       /home/toinbis/Desktop/programming/project/runtime/
etc/nginx_conf/mime.types;
    default_type  application/octet-stream;

    access_log  /home/toinbis/Desktop/programming/project/runtime/logs/
nginx_access.log;
    error_log   /home/toinbis/Desktop/programming/project/runtime/logs/
nginx_error.log;

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  2;
    tcp_nodelay        on;

    server_names_hash_bucket_size 128;
    #server_name_hashes 128;
    gzip  on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)"; #iš ubuntu
    gzip_types text/plain text/css application/x-javascript text/xml
application/xml application/xml+rss text/javascript;

upstream backend {
  server 127.0.0.1:8001;

}

server {
            listen   8003;
            server_name localhost;

            error_log /home/toinbis/Desktop/programming/project/
runtime/logs/project_nginx_error.log;
            access_log /home/toinbis/Desktop/programming/project/
runtime/logs/project_nginx_access.log;

            location / {
                        proxy_pass  http://backend/;
                        include     /home/toinbis/Desktop/programming/
project/runtime/etc/nginx_conf/nginx_proxy_1.conf;
            }

            location  /media/ {
                        root /home/toinbis/Desktop/programming/project/
src/tsd/core/;
            }

}
}

cheers, to 欢呼

Your settings are incorrect. 您的设置不正确。 Django believes that it runs on port 80. Look at this line in the first HTTP response: Django认为它运行在端口80上。在第一个HTTP响应中查看以下行:

Location: http://127.0.0.1/admin/

Unfortunately, I can't understand right now why this happens. 不幸的是,我现在不明白为什么会这样。 I prefer just to step throught the relevant Django code with a debugger in such cases. 在这种情况下,我更喜欢使用调试器逐步调试相关的Django代码。

@Eugene Morozov has pointed out the end result of the problem, but I think the solution lies deeper. @Eugene Morozov指出了问题的最终结果,但我认为解决方案更深入。

The Location header being returned is incorrect: 返回的Location标头不正确:

Location: http://127.0.0.1/admin/

This is clearly missing the port number. 这显然缺少端口号。 Unfortunately, it is hard to tell exactly where this is coming from. 不幸的是,很难确切地说出它的来源。 You may be running into either Nginx or Apache automagically altering the Location header. 您可能会碰巧通过Nginx或Apache来自动更改Location标头。 I'd suggest doing the following to see where the problem crops up: 我建议执行以下操作以查看问题的根源:

  • Clear your cookies before each time you log in, or you may get a false positive. 每次登录前请清除您的cookie,否则您可能会得到误报。
  • Go directly to your Apache instance and see if the problem persists. 直接转到您的Apache实例,看看问题是否仍然存在。
    • http://127.0.0.1:8001/admin/ http://127.0.0.1:8001/admin/
    • If the location header does not have a port on it: 如果位置头上没有端口:
      • Fire up the Django development server on some random port (8005, for example) and see if the problem persists: 在某个随机端口(例如8005)上启动Django开发服务器,然后查看问题是否仍然存在:
        • If the problem persists, it is in your code. 如果问题仍然存在,则在您的代码中。
        • If the problem does not persist, Apache is either doing something weird or WSGI/Django isn't getting all the information it needs to determine the port number. 如果问题仍然存在,则说明Apache正在做一些奇怪的事情,或者WSGI / Django无法获得确定端口号所需的所有信息。
          • Make sure that the wsgi script and the django app can both see the port number in the environment. 确保wsgi脚本和django应用程序都可以在环境中看到端口号。
    • If the location header has a port in it: 如果位置标头中包含端口:
      • Nginx is stripping it off for some reason. Nginx正在出于某种原因将其剥离。
      • Try cutting Apache out of the loop, you probably don't need it. 尝试将Apache切入循环,您可能不需要它。
        • Fire up Django as a FastCGI server and connect Nginx to that instead of Apache. 将Django启动为FastCGI服务器,并将Nginx连接到该服务器而不是Apache。
          • This will actually lower some of the overhead on your server (less request passing, extra threads, memory use, etc). 实际上,这将减少服务器上的一些开销(较少的请求传递,额外的线程,内存使用等)。
          • Fairly good setup guide over here . 相当不错的设置指南在这里

Ninja Edit! 忍者编辑!
Not sure if this will help, but it seems there is an option in Nginx for ignoring the port number in redirects. 不知道这是否有帮助,但是Nginx中似乎有一个选项可以忽略重定向中的端口号。 The URL is dead, so I'm pasting this from over here . URL已死,所以我要从这里粘贴。

http://wiki.codemongers.com/NginxHttpCoreModule#port_in_redirect http://wiki.codemongers.com/NginxHttpCoreModule#port_in_redirect

syntax: port_in_redirect [ on|off ]
default: port_in_redirect on
context: http, server, location

Directive allows or prevents port indication in redirects handled by nginx. 指令允许或阻止nginx处理的重定向中的端口指示。

Euge, James, 尤格,詹姆斯,

thanks a lot for your help once again! 非常感谢您的帮助!

The solution turned out to be described here : changing proxy_set_headerHost $host; 原来在这里描述该解决方案:更改proxy_set_headerHost $host; to proxy_set_headerHost $host:$server_port; proxy_set_headerHost $host:$server_port; did the trick! 做到了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM