LoginSignup
173
173

More than 5 years have passed since last update.

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

Posted at

フロントでSSL解いてリバースプロキシってのは割と良く作るのだけど、あれれ...?

nginx, ときどき httpd で出来てたことが出来なくて「えっ?」てなる。今後のためにメモ。

nginx.conf
server {
       listen         80;
       server_name    www.example.com;
       rewrite        ^ https://$http_host$request_uri? permanent;
}

server {
       listen         443;
       server_name    www.example.com;

       ssl on;
       ssl_certificate ...
       ssl_certificate_key ...

       :
       location / {
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-Proto https;
             proxy_set_header X-Forwarded-Host $host;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

             proxy_pass http://backend.example.com/;
             proxy_redirect http:// https://;
       }
}

ポイントは proxy_redirect http:// https://; の行。後ろが頭のいい奴(例:Rails)だと、X-Forwarded-Proto を見てよしなにヘッダを吐いてくれるんだけど、X-Forwarded-Proto 見てくれない奴が裏側にいる場合には、これで Location ヘッダなんかを強制的に https:// に書き換える必要がある。

apache httpd で言うところの ProxyPassReverse ですな。こっちの方が融通利くみたい。

ちなみに nginx の $http_host と $host は同じようでいて微妙に違うらしい > ネタ元

173
173
2

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
173
173