Django 管理员身份验证失败

发布于 2024-08-20 00:31:01 字数 5517 浏览 7 评论 0原文

首先按下“登录”按钮时登录 django admin 失败 时间,但再次按“返回”和“登录”-使用户登录 成功地。

我在这里使用 zc.buildout 部署 Django 应用程序,设置类似 此处描述的内容 http://www.meppum。 com/2009/jan/17/installing-django-ubuntu-intrepid/。 Nginx(监听 127.0.0.1:8003)运行于 Apache 之外(监听 在 127.0.0.1:8001 上),两台服务器的配置均在 帖子结束。我正在通过 127.0.0.1:8003/ 访问我的 django 应用程序。

因此,127.0.0.1:8003/admin/ 打开 django 管理员登录表单。 1)输入用户名/密码,按“登录”重定向到(出于某种未知原因?)127.0.0.1/ admin,因此最终什么也没有,因为没有服务器监听 在 80 端口上。 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)按浏览器的后退键,然后再次按“登录”按钮(或 按浏览器的“后退”和“刷新”)成功登录。 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

看起来这是一个与 cookie 相关的问题,但是,我猜,其根源是 问题也可能出在 apache 和/或 nginx 配置中。

有什么想法可以让管理员登录表单立即生效吗?

干杯!

P.S.configs

apache虚拟主机配置:

<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配置:

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

}
}

干杯, 到

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.

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/.
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. I'm accessing my django app as 127.0.0.1:8003/.

So, 127.0.0.1:8003/admin/ opens django admin login form.
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.
HTTP trace (after clicking login in: 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. HTTP trace:

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.

Any ideas how to make the admin log-in form work immediately?

Cheers!

to

P.S.configs:

apache vhost configuration:

<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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

从来不烧饼 2024-08-27 00:31:01

@Eugene Morozov 指出了问题的最终结果,但我认为解决方案更深层次。

返回的 Location 标头不正确:

Location: http://127.0.0.1/admin/

这显然缺少端口号。不幸的是,很难确切地说出这是从哪里来的。您可能会遇到 Nginx 或 Apache 自动更改 Location 标头的情况。我建议执行以下操作来查看问题出现的位置:

  • 每次登录之前清除 cookie,否则您可能会收到误报。
  • 直接转到您的 Apache 实例并查看问题是否仍然存在。
    • http://127.0.0.1:8001/admin/
    • 如果位置标头上没有端口:
      • 在某个随机端口(例如 8005)上启动 Django 开发服务器,看看问题是否仍然存在:
        • 如果问题仍然存在,则问题出在您的代码中。
        • 如果问题不再存在,则说明 Apache 正在执行一些奇怪的操作,或者 WSGI/Django 未获取确定端口号所需的所有信息。
          • 确保 wsgi 脚本和 django 应用都可以看到环境中的端口号。
    • 如果位置标头中有一个端口:
      • Nginx 由于某种原因正在剥离它。
      • 尝试将 Apache 排除在外,您可能不需要它。
        • 启动 Django 作为 FastCGI 服务器并将 Nginx 连接到该服务器而不是 Apache。
          • 这实际上会降低服务器的一些开销(减少请求传递、额外线程、内存使用等)。
          • 相当好的设置指南在这里。< /里>

忍者编辑!
不确定这是否有帮助,但 Nginx 中似乎有一个选项可以忽略重定向中的端口号。该网址已失效,因此我从此处粘贴此内容。

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

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

指令允许或阻止端口
重定向中的指示由处理
nginx。

@Eugene Morozov has pointed out the end result of the problem, but I think the solution lies deeper.

The Location header being returned is incorrect:

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. 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.
  • Go directly to your Apache instance and see if the problem persists.
    • 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:
        • 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.
          • Make sure that the wsgi script and the django app can both see the port number in the environment.
    • If the location header has a port in it:
      • Nginx is stripping it off for some reason.
      • Try cutting Apache out of the loop, you probably don't need it.
        • Fire up Django as a FastCGI server and connect Nginx to that instead of 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. The URL is dead, so I'm pasting this from over here.

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.

魔法唧唧 2024-08-27 00:31:01

您的设置不正确。 Django 相信它在端口 80 上运行。看看第一个 HTTP 响应中的这一行:

Location: http://127.0.0.1/admin/

不幸的是,我现在无法理解为什么会发生这种情况。在这种情况下,我更喜欢使用调试器单步调试相关的 Django 代码。

Your settings are incorrect. Django believes that it runs on port 80. Look at this line in the first HTTP response:

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.

无可置疑 2024-08-27 00:31:01

Euge、James,

再次非常感谢你们的帮助!

解决方案的描述如下:此处:更改 proxy_set_headerHost $host ;proxy_set_headerHost $host:$server_port; 成功了!

Euge, James,

thanks a lot for your help once again!

The solution turned out to be described here: changing proxy_set_headerHost $host; to proxy_set_headerHost $host:$server_port; did the trick!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文