为了实现ELB+nginx的https重定向,需要进行以下nginx设置

想要做什么?

希望将通过HTTP访问AWS上的服务器的人重定向到HTTPS。使用Nginx。

顺便提一下,在Apache上有很多相关的文章。
https://qiita.com/ruddy95/items/377428f4d8219608bdbd
https://qiita.com/michimani/items/88973c5e2ae76a8e84aa
https://qiita.com/tihimsm/items/2c58a3425e446261940b

祖先的智慧

如果在ELB的设置中将端口(80|443)重定向到80,可能会导致重定向循环。

https → 443 ELB 80 → nginx
https → 443 负载均衡 80 → nginx

http → 80 ELB 80 → nginx
http → 80 负载均衡 80 → nginx

重定向的对象只能是“来自ELB的通过http(80)访问的请求”。

鉴别方式

使用Nginx时,可以通过以下变量来获取向ELB发送请求的格式。

$http_x_forwarded_proto

设定

location / {
    if ($http_x_forwarded_proto = http) {
        return 301 https://$host$request_uri;
    }
}

只需要一个选项:
按照上述要求,当$http_x_forwarded_proto为http时,进行301重定向。