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

时间:2015-04-19 23:25:34

标签: django ssl nginx https mixed-content

我正在使用django-braces' LoginRequiredMixin我的一个观点。基本上,这会将?next=/my/desired/url的查询字符串添加到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搜索提交,为此我已将网址设置为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设置的。

我已尝试在Django @login_required dropping httpsdocs here的每个回复中添加SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https'),但无济于事。谢谢你的帮助!

0 个答案:

没有答案