如何在 Django 中使用带有 https 的登录重定向?

2024-02-15

我正在使用 django-braces'LoginRequiredMixin对于我的观点之一。基本上,这会添加一个查询字符串?next=/my/desired/url to http://example.com/login/.

问题是,我在我的网站上使用 ssl 证书。我的nginx文件如下:

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}
#
# Redirect all www to non-www
#
server {
    server_name          www.example.com;
    ssl_certificate      /src/bin/ssl/ssl-bundle.crt;
    ssl_certificate_key  /etc/ssl/private/STAR_example_com.key;
    ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;
    listen               *:80;
    listen               *:443 ssl spdy;
    listen               [::]:80 ipv6only=on;
    listen               [::]:443 ssl spdy ipv6only=on;

    return 301 https://example.com$request_uri;
}

#
# Redirect all non-encrypted to encrypted
#
server {
    server_name          example.com;
    listen               *:80;
    listen               [::]:80;

    return 301 https://example.com$request_uri;
}

server {
    server_name          example.com;
    ssl_certificate      /src/bin/ssl/ssl-bundle.crt;
    ssl_certificate_key  /etc/ssl/private/STAR_example_com.key;
    ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;
    listen               *:443 ssl spdy;
    listen               [::]:443 ssl spdy;

    # rest goes here...

    root /usr/share/nginx/html;
    index base.html index.html index.htm;

    client_max_body_size 4G; 

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /src/example/media;
    expires 1y; 
    add_header Cache-Control "public";
    }   

    # your Django project's static files - amend as required
    location /static {
        alias /src/static;
    expires 1y; 
    add_header Cache-Control "public";
    } 

    location / { 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-Ssl on; 
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }   
}

我有一个 AJAX 搜索提交,我已将其 url 设置为 https。不幸的是,我仍然收到此错误:

Mixed Content: The page at 'https://example.com/my/url/' was loaded 
over HTTPS, but requested an insecure XMLHttpRequest endpoint 
'http://example.com/login/?next=/amazon/%3FsearchTerms%3DmySearchTerms'. 
This request has been blocked; the content must be served over HTTPS.

我需要在 nginx 文件中更改哪些内容才能使此请求正常工作?这基本上是因为未使用 https 设置登录重定向。

我尝试过添加SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')每回复Django @login_required 删除 https https://stackoverflow.com/q/11571616/2532070,以及文档在这里 https://docs.djangoproject.com/en/1.7/ref/settings/#secure-proxy-ssl-header但无济于事。谢谢你的帮助!


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Django 中使用带有 https 的登录重定向? 的相关文章

随机推荐