使用HTTP和HTTPS设置Ghost + Nginx时的ERR_TOO_MANY_REDIRECTS

时间:2016-02-12 04:53:23

标签: http nginx https reverse-proxy

我使用Nginx(1.8.1)在VPS中部署Ghost(0.7.6)。为了使仪表板和登录页面安全,我强制任何请求在访问此类页面时使用HTTPS(例如/ghost页面)。但是,对于任何其他页面的任何请求(例如访问Ghost博客本身),我想强制它使用HTTP。 Ghost正在聆听127.0.0.1:2368

奇怪的是,结果并不像我预期的那样:每当我访问我的博客时(让我们说网址是a.b),它说我的网站有ERR_TOO_MANY_REDIRECTS并且它在http://a.bhttps://a.b之间(或http://a.b/signinhttps://a.b/signin之间)重定向。但是,当我访问管理仪表板(https://a.b/ghosthttp://a.b/ghost)时,它会按预期运行(没有错误,正确重定向以使用HTTPS)。

任何帮助?

我的Nginx配置:

# Configuration for http://a.b
server {
    listen 80;
    server_name a.b;
    location ^~ /ghost { # /ghost should be accessed securely
        return 301 https://$host$request_uri;
    }
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $host;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:2368;
    }
}

# Configuration for http://a.b
server {
    listen 443 ssl;
    server_name a.b;
    ssl_certificate ...;
    ssl_certificate_key ...;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers '...';

    location ^~ /ghost { # /ghost should be accessed securely
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $host;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:2368;
    }
    location / { # Force to use HTTP
        return 301 http://$host$request_uri;
    }
}

任何形式的帮助都将受到赞赏:')

1 个答案:

答案 0 :(得分:1)

https://github.com/TryGhost/Ghost/issues/2796

location ^~ /ghost { # /ghost should be accessed securely
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header HOST $host;
    proxy_set_header X-NginX-Proxy true;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:2368;
}