LoginSignup
24
27

More than 5 years have passed since last update.

apache(htaccess)でリバースプロキシー経由のHTTP/HTTPS転送

Last updated at Posted at 2014-07-22

よくある話なのでメモ。

htaccessを使ってhttpからhttpsに転送する設定です。

RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

だけど例外として、リバースプロキシーを経由してhttpsじゃなくなっている場合は上記では動作しません。httpになった状態でリクエストが送られてきますので、この場合だとhttpsへ延々と転送し続けるリダイレクトループが発生します。

その場合は以下のように追加したヘッダーを元に転送する設定にします。httpで/form/から始まるURLならhttpsに転送します。/form/以外はhttpに転送する設定も書きます。

  RewriteEngine on
  RewriteCond %{HTTP:X-Forwarded-Proto} ^http$
  RewriteCond %{REQUEST_URI} ^/form/
  RewriteRule (.*) https://www.hogehoge.jp%{REQUEST_URI} [R=301,L]

  RewriteCond %{HTTP:X-Forwarded-Proto} ^https$
  RewriteCond %{REQUEST_URI} !^/form/
  RewriteRule (.*) http://www.hogehoge.jp%{REQUEST_URI} [R=301,L]

転送元のリバースプロキシー設定はこんな感じです。poundの設定ですが分かると思います。ヘッダの追加がポイントです。

ListenHTTP
  Address xxx.xxx.xxx.xxx
  Port    80
  AddHeader  "X-Forwarded-Proto: http"
  RewriteLocation 0
  Service
    BackEnd
      Address www.hogehoge.jpx
      Port    80
      TimeOut 30
    End
  End
End

ListenHTTPS
  Address xxx.xxx.xxx.xxx
  Port    443
  Cert "/usr/local/etc/pound/ssl/www.hogehoge.jp/ssl.globalsign.com.key_crt_cer"
  xHTTP      0
  AddHeader  "X-Forwarded-Proto: https"
  RewriteLocation 0
  Service
    BackEnd
      Address www.hogehoge.jpx
      port 80
      TimeOut 30
    End
  End
End

すっきり。

24
27
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
24
27