LoginSignup
3
1

More than 5 years have passed since last update.

リバースプロキシを間に挟むflaskアプリケーションのリダイレクトではまった

Last updated at Posted at 2017-02-22

現象

SSLを導入したリバースプロキシ―(apache mod_proxy)
バックエンドのwsgiサーバー(apache mod_wsgi+flask)

リバースプロキシ―がhttpsでリクエストを受け、httpでバックエンドのwsgiサーバーにプロキシーする構成。

この時、flaskでリダイレクトするとクライアントブラウザでのリダイレクト先のurlがhttpになってしまった。

リバースプロキシ―の設定

<VirtualHost *:443>

    ...

    ProxyPass / http://my.wsgi.server/
    ProxyPassReverse / http://my.wsgi.server/

    ...

</VirtualHost>

mod_proxyはドキュメントに従って設定しているはず。

Location

curl -v http://my.wsgi.server/

するとLocationにhttp://付きの絶対パスが指定されていた。
どうやらこれが問題らしい。絶対パスを指定すると、ProxyPassReverseは正常に動作しない(スキームが書き換わらない)ようだ。

flaskでは...

url_for(..., _scheme="https") を使用すれば、スキームをhttpsにできそう。
しかし、変更箇所が大きいのと、バックエンドのwsgiサーバー単独で動かなくなるので見送り。

RFCでは?

RFC7321では相対パスが認められている模様。
https://triple-underscore.github.io/RFC7231-ja.html#section-7.1.2
Locationヘッダを書き換えることにする。

wsgiサーバー側でLocationを書き換える

<VirtualHost *:80>

    ...

    Header edit Location ^http:// //

    ...

</VirtualHost>
curl -v http://my.wsgi.server/

Locationからスキームが消え、リバースプロキシー経由でのリダイレクトが正常に動作するようになった。

3
1
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
3
1