LoginSignup
8
8

More than 5 years have passed since last update.

nginx+ELBでhttps/http毎に行う処理を変えたい

Posted at

ELBでSSLの設定をしている場合には、ぶら下がってるサーバーへは80番で常に来ます。なのでこれだとSSLでアクセスされているかどうかわかりません。

ですが、httpsかどうかは$http_x_forwarded_protoという変数に入ってるので、それで判断しましょうという話です。

例では、TOPにhttpsで来た時だけhttpにrewriteし、それ以外のページにhttpで来た時にはhttpsにrewriteするという処理を書いてます。

/etc/nginx/conf.d/sample.conf
     location = / {
+        if ($http_x_forwarded_proto = "https") {
+            rewrite ^(.*) http://$server_name$1
+            break;
+        }
         ...
     }

     location / {
+        if ($http_x_forwarded_proto != "https") {
+            rewrite ^(.*) https://$server_name$1
+            break;
+        }
8
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
8
8