首页 > 解决方案 > nginx 页面重定向不正确,如何解决?

问题描述

我的 nginx conf,目标是让所有的东西都不是 https 到 https,从 www 到不是 www,通过几个教程,我一直得到这个

页面未正确重定向

连接到 example.com 期间发生错误。

此问题有时可能是由禁用或拒绝接受 cookie 引起的。

.

server {
    listen 80;
    server_name "~^www\.(.*)$" ;
    return 301 https://example.com$request_uri ;

}

server {
    listen 443 ssl;
    server_name www.example.com;
    ssl_certificate pathto/fullchain.pem;
    ssl_certificate_key pathto/privkey.pem;

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

server {

    listen 443 ssl;
    server_name example.com;
 
    ssl on;
    ssl_certificate pathto/fullchain.pem;
    ssl_certificate_key pathto/privkey.pem;
    ssl_trusted_certificate pathto/chain.pem;

    ssl_stapling on;
    ssl_stapling_verify on;

    add_header Strict-Transport-Security "max-age=31536000";

    add_header X-Frame-Options "SAMEORIGIN";
    
    client_max_body_size 4G;

    location /static/ {
        alias   /asd/ads/static/;
    }
    
    location /media/ {
        alias   /asd/ads/media/;
    }


    location / {

        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header X-Forwarded-Proto https;

        proxy_redirect off;


    }


}

看起来您的帖子主要是代码;请添加更多细节。

标签: nginx

解决方案


我从我的 django 设置中删除

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

它工作!


推荐阅读