关于nginx:Digital Ocean Load Balancer从301到301的HTTP到https

Digital Ocean Load Balancer http to https with 301 instead of 307

在我的设置中,我有一个DigitalOcean负载平衡器,该负载平衡器仅与一个运行nginx的液滴连接(现在)。我通过SSL终止使用负载平衡器管理SSL证书。负载均衡器已设置以下转发规则:

端口80上的HTTP->端口80上的HTTP

端口443上的HTTP2->端口80上的HTTP

负载均衡器有一个选项可以将HTTP重定向到HTTPS。但是,如果使用此选项,它将使用307重定向而不是301重定向。根据DigitalOcean的说法,这是有意的。出于Seo的原因,我被告知应该使用301。我试图禁用该选项,并使用nginx configs进行了重定向,但最终陷入了无限循环。这是我用过的片段:

1
2
3
4
5
6
7
server {
        listen 80;
        server_name _;
        # $scheme will get the http protocol
        # and 301 is best practice for tablet, phone, desktop and seo
        return 301 https://$host$request_uri;
}

有人知道如何正确处理这种情况吗?任何帮助将不胜感激。

干杯
Raph


我实际上已经弄清楚了,我可以通过添加

来防止循环

1
2
3
if ($http_x_forwarded_proto ="http") {
  return 301 https://$host$request_uri;
}

如这篇文章中所建议