19
18

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配下のApacheで外部はHTTPSにリダイレクトし、内部のサーバのみHTTPで通信させる

Last updated at Posted at 2016-02-08

やりたい事

server_pptx.png

  • ApacheにELB経由でHTTPでアクセス来たらHTTPSでアクセスするようにクライアントに要求する
  • ApacheにELB経由でHTTPSアクセス来たらELBでHTTPのポートのApacheにアクセスする
  • 他の内部(10.X.X.X)のEC2からHTTPにアクセスが来た場合はそのまま通す

設定

<VirtualHost *:80>
  ServerName xxx

  # http -> https rewrite
  RewriteEngine On
  RewriteCond %{HTTP:X-Forwarded-Port} !^443$
  RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker
  RewriteCond %{REQUEST_URI} !=/server-status
  RewriteCond %{HTTP:X-FORWARDED-FOR} !^$
  RewriteRule ^(.*)?$ https://%{HTTP_HOST}$1 [R=301,L]
  # http -> https rewrite [end]

  ...
</VirtualHost>

上記でいけます。ELB経由でアクセスされる場合は、 %{HTTP:X-FORWARDED-FOR} のHTTPヘッダにクライアントのGlobal IPがついているので、それがない場合は内部と判定しています。

その他の設定は、ヘルスチェックなどを通すためのものです

追記

CloudFrontの場合は以下を参照

19
18
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
19
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?