9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ELB+nginxでhttpsリダイレクトを実現する為のnginx設定

Last updated at Posted at 2018-01-29

何がやりたかったか

AWS上のサーバにhttpでアクセスしてきた人を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
http → 80 ELB 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リダイレクトするように返してあげましょう。

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?