LoginSignup
69
55

More than 5 years have passed since last update.

ELB+nginx構成でhttpをhttpsにリダイレクトする

Last updated at Posted at 2015-12-29

http_x_forwarded_prot を見てhttpならhttpsにリダイレクトする方法が一般的なのかな?
nginxのリッスンポートをもう一つ開けてELBのリスナーの設定変更でもう少し簡単に出来ないかと試してみた。

ELBのリスナーの設定

  • 80を81に流す
  • インスタンスの80ポートへはELBからヘルスチェックが来るのでいじらないようにした
ロードバランサーのプロトコル ロードバランサーのポート インスタンスのプロトコル インスタンスのポート
HTTP 80 HTTP 81
HTTPS 443 HTTP 80

nginxの設定

  • /etc/nginx/conf.d/virtual.conf に以下を追加
  • 81に来たリクエストはhttpsに問答無用でリダイレクトする
  • if ($http_x_forwarded_proto != 'https') {...の判定が無いのですっきり
server {
  listen 81;
  rewrite ^ https://$host$request_uri permanent;
}

apacheの場合

  • apacheでも必要になったので追記
Listen 81
<VirtualHost *:81>
  <ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
  </ifModule>
</VirtualHost>
69
55
4

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
69
55