为什么 Django 不在 Varnish 代理后面生成 CSRF 或会话 Cookie?

发布于 2024-11-08 17:28:13 字数 2105 浏览 2 评论 0原文

在带有 Apache2 的 Linux 服务器上运行 Django 1.2.5,由于某种原因,Django 似乎无法存储 CSRF 或会话 cookie。因此,当我尝试登录 Django 管理员时,它会在提交登录表单时显示 CSRF 验证错误。有没有人遇到过这个问题并找到解决方案?

当我在主机提供的 VPS 网址上尝试此操作时,我能够发布有效的帖子。示例:vps123.hostdomain.com/admin/ 并且对于该域,确实设置了 cookie。但是,当我访问 www.sitedomain.com/admin/ 并尝试登录时,出现 CSRF 403 错误,指出 cookie 不存在,并且当我检查浏览器时,cookie 未设置。

我已尝试在我的设置文件中设置以下内容:

SESSION_COOKIE_DOMAIN = 'www.sitedomain.com'
CSRF_COOKIE_DOMAIN = 'www.sitedomain.com'

还尝试过:

SESSION_COOKIE_DOMAIN = 'vps123.hostdomain.com'
CSRF_COOKIE_DOMAIN = 'vps123.hostdomain.com'

我已将“django.middleware.csrf.CsrfViewMiddleware”添加到settings.py中的MIDDLEWARE_CLASSES中,并且表单中有一个CSRF令牌,它显示在POST中。

我启用了cookie。我已经在多个浏览器和机器上尝试过这个。

www.sitedomain.com 前面有一个清漆代理服务器,我认为这可能是问题的一部分。任何有使用代理服务器和 Django 经验的人都可以对此有所了解。

我的apache2配置:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName www.sitedomain.com
    ServerAlias www.sitedomain.com
    <Location "/">
        Options FollowSymLinks
        SetHandler python-program
        PythonInterpreter nzsite
        PythonHandler django.core.handlers.modpython
        PythonDebug On
        PythonPath "['/var/www/django_projects', '/var/www', '/usr/lib/python2.6/dist-packages'] + sys.path"
        SetEnv DJANGO_SETTINGS_MODULE project_one.settings
    </Location>
    <location "/phpmyadmin">
        SetHandler None
    </location>
</VirtualHost>

<VirtualHost *:80>
    ServerName othersite.sitedomain.com
    ServerAlias othersite.sitedomain.com
    <Location "/">
        Options FollowSymLinks
        SetHandler python-program
        PythonInterpreter ausite
        PythonHandler django.core.handlers.modpython
        PythonDebug On
        PythonPath "['/var/www/django_projects', '/var/www', '/usr/lib/python2.6/dist-packages'] + sys.path"
        SetEnv DJANGO_SETTINGS_MODULE project_two.settings
    </Location>
    <location "/phpmyadmin">
        SetHandler None
    </location>
</VirtualHost>

Running Django 1.2.5 on a Linux server with Apache2 and for some reason Django seems like it cannot store CSRF or Session cookies. Therefore when I try to login to the Django admin it gives me a CSRF verification error upon submitting the login form. Has anyone come up against this and found a solution?

I AM able to make a valid post when i try this at the url of my VPS that was provided by my host. Example: vps123.hostdomain.com/admin/ and for that domain the cookies DO get set. However, when I go to www.sitedomain.com/admin/ and try to login I get a CSRF 403 error saying the cookie is not there and when I check in my browsers cookies they are not set.

I have tried setting the following in my settings file:

SESSION_COOKIE_DOMAIN = 'www.sitedomain.com'
CSRF_COOKIE_DOMAIN = 'www.sitedomain.com'

Also tried:

SESSION_COOKIE_DOMAIN = 'vps123.hostdomain.com'
CSRF_COOKIE_DOMAIN = 'vps123.hostdomain.com'

I have 'django.middleware.csrf.CsrfViewMiddleware' added to my MIDDLEWARE_CLASSES in settings.py and there is a CSRF token in the form and it shows up in the POST.

I have cookies enabled. I have tried this on multiple browsers and machines.

There is a varnish proxy server sitting in front of www.sitedomain.com that I think may be part of the problem. Anyone with experience using proxy servers and Django may be able to shed some light on that.

My apache2 config:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName www.sitedomain.com
    ServerAlias www.sitedomain.com
    <Location "/">
        Options FollowSymLinks
        SetHandler python-program
        PythonInterpreter nzsite
        PythonHandler django.core.handlers.modpython
        PythonDebug On
        PythonPath "['/var/www/django_projects', '/var/www', '/usr/lib/python2.6/dist-packages'] + sys.path"
        SetEnv DJANGO_SETTINGS_MODULE project_one.settings
    </Location>
    <location "/phpmyadmin">
        SetHandler None
    </location>
</VirtualHost>

<VirtualHost *:80>
    ServerName othersite.sitedomain.com
    ServerAlias othersite.sitedomain.com
    <Location "/">
        Options FollowSymLinks
        SetHandler python-program
        PythonInterpreter ausite
        PythonHandler django.core.handlers.modpython
        PythonDebug On
        PythonPath "['/var/www/django_projects', '/var/www', '/usr/lib/python2.6/dist-packages'] + sys.path"
        SetEnv DJANGO_SETTINGS_MODULE project_two.settings
    </Location>
    <location "/phpmyadmin">
        SetHandler None
    </location>
</VirtualHost>

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

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

发布评论

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

评论(3

捶死心动 2024-11-15 17:28:13

问题是我的站点前面有一个 Varnish 代理服务器。 Varnish 正在接受请求并从中剥离 cookie。为了解决这个问题,我必须让管理 Varnish 服务器的公司将“/admin”添加到例外列表中,以便可以传递 cookie。抱歉,我无法详细说明 Varnish 流程的工作原理。

The problem was that I have a Varnish Proxy server in front of my site. Varnish was taking requests and stripping cookies from them. To fix this I had to have the company that is managing the Varnish Server add '/admin' to a list of exceptions so that cookies could be passed. Sorry I can't shed more light on how the Varnish process works.

风渺 2024-11-15 17:28:13

您是否在表单模板中包含 {{csrf_token}}

<form autocomplete="off" method="post" action="{% url auth_login %}">{% csrf_token %}
    {{form|as_p}}
    <input type='submit' />
</form>

包括中间件?

    'django.middleware.csrf.CsrfViewMiddleware',

根据您的编辑,猜测可能与 Apache 中的 VirtualHost 配置有关(如果您的提供商使用的是 apache)。这是我的 apache 配置之一的编辑版本。

<VirtualHost *:80>
ServerName www.domain.com

WSGIProcessGroup my-django-site
WSGIScriptAlias / /path-to-my-django-site/wsgi/production.wsgi
Alias /media /path-to-my-django-site/media
</VirtualHost> 

可能是这样,apache 中的服务器名称必须与您点击框的域名以及 Django 配置中的 *_COOKIE_DOMAIN 设置相匹配。我不确定你是否能够改变这一点。如果没有其他答案可以获胜,可能值得与您的提供商交谈。

Are you including the {{csrf_token}} in your form template?

<form autocomplete="off" method="post" action="{% url auth_login %}">{% csrf_token %}
    {{form|as_p}}
    <input type='submit' />
</form>

And including the middleware?

    'django.middleware.csrf.CsrfViewMiddleware',

From your edit, at a guess, it might have something to do with the VirtualHost configuration in Apache (if your provider is using apache). Here is an edited version of one of my apache configurations.

<VirtualHost *:80>
ServerName www.domain.com

WSGIProcessGroup my-django-site
WSGIScriptAlias / /path-to-my-django-site/wsgi/production.wsgi
Alias /media /path-to-my-django-site/media
</VirtualHost> 

It may be the case that the server name within apache has to match the domain name you are hitting the box at, along with the *_COOKIE_DOMAIN settings in your Django configuration. I'm not sure if you'll be able to change this though. Might be worth speaking to your provider if no other answers yield a win.

宫墨修音 2024-11-15 17:28:13

您是否使用 csrf 信息更新模板数据?

from django.core.context_processors import csrf

def index(request)
    data = {"listitems": items}
    data.updates(csrf(request))

    return render_to_response('template.html', data)

Are you updating your template data with the csrf info?

from django.core.context_processors import csrf

def index(request)
    data = {"listitems": items}
    data.updates(csrf(request))

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