LoginSignup
64
63

More than 5 years have passed since last update.

Apach, NginxをプロキシにしたRailsアプリでhttpsがリダイレクト時にhttpになる問題

Last updated at Posted at 2013-04-13

HTTPSオンリーのRailsのアプリ(Redminなど)でApacheをリバースプロキシにした場合、リダイレクトがHTTPになってしまう。。。

これを対処するにはRequestHeaderディレクティブでApacheからバックエンドのアプリケーションサーバへのX_FORWARDED_PROTOヘッダを付与させるようにする。

Apacheの場合

RequestHeader set X_FORWARDED_PROTO 'https'

設定例)

ApacheでRedminのリバースプロキシHTTPSオンリーにする
<VirtualHost *:443>
    # SSL の細かい設定(^^;
    RequestHeader set X_FORWARDED_PROTO 'https'
    ProxyPass /rails http://localhost:3000/rails
    ProxyPassReverse /rails http://localhost:3000/rails
</VirtualHost>

参考
http://webos-goodies.jp/archives/51261261.html

Nginxの場合

proxy_set_header X-Forwarded-Proto https;

設定例)

upstream backend {
  server unix:/data/apps/current/tmp/sockets/unicorn.sock max_fails=3 fail_timeout=10s;
}
 location / {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    if (!-f $request_filename) { proxy_pass http://backend; }
  }

参考
nginx で SSL解きリバースプロキシな設定のお作法 - Qiita

64
63
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
64
63