[PYTHON] I got stuck in a flask application redirect with a reverse proxy in between

phenomenon

Reverse proxy with SSL (apache mod_proxy) Backend wsgi server (apache mod_wsgi + flask)

A configuration in which a reverse proxy receives a request via https and proxies to the backend wsgi server via http.

At this time, when redirecting with flask, the url of the redirect destination in the client browser became http.

Reverse proxy settings

<VirtualHost *:443>

    ...

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

    ...

</VirtualHost>

mod_proxy should be set according to the documentation.

Location

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

Then, the absolute path with http: // was specified for Location. Apparently this is the problem. If you specify an absolute path, ProxyPassReverse doesn't seem to work properly (the scheme doesn't rewrite).

In flask ...

You could use url_for (..., _scheme = "https") to make the scheme https. However, since the changes are large and the backend wsgi server alone will not work, I will not do it.

In RFC?

It seems that relative paths are allowed in RFC7321. https://triple-underscore.github.io/RFC7231-ja.html#section-7.1.2 Rewrite the Location header.

Rewrite Location on the wsgi server side

<VirtualHost *:80>

    ...

    Header edit Location ^http:// //

    ...

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

The scheme has disappeared from the location and the redirect via the reverse proxy is now working properly.

Recommended Posts

I got stuck in a flask application redirect with a reverse proxy in between
I want to transition with a button in flask
I got stuck when trying to specify a relative path with relative_to () in python
I made a WEB application with Django
[ES Lab] I tried to develop a WEB application with Python and Flask ②
What I did when I got stuck in the time limit with lambda python
I made a Mattermost bot with Python (+ Flask)
I made a web application that maps IT event information with Vue and Flask
I want to work with a robot in python.
I can't manipulate iframes in a page with Selenium
Work memo that I tried i18n with Flask app
I got a sqlite3.OperationalError
I wrote python3.4 in .envrc with direnv and allowed it, but I got a syntax error
I got a Value Error when using JUMAN ++ with PyKNP
When I made CaboCha usable with python3, I got stuck (Windows 10)
I made a simple typing game with tkinter in Python
Mezzanine introduction memo that I got stuck in the flow
What I got stuck around GUI in WSL python environment
Until I return something with a line bot in Django!
I made a puzzle game (like) with Tkinter in Python
Flask-Create a Todo list with CSRF measures in Flask with WTF
I get an OS Error: [Errno 8] Exec format error when running a Flask application with a python command
When I cut the directory for UNIX Socket under / var / run with systemd, I got stuck in a pitfall and what to do
Web application with Python + Flask ② ③
A little stuck with chainer
Web application with Python + Flask ④
Let's make a WEB application for phone book with flask Part 1
Build a Flask / Bottle-like web application on AWS Lambda with Chalice
Let's make a WEB application for phone book with flask Part 2
I made a Nyanko tweet form with Python, Flask and Heroku
I tried to make a 2channel post notification application with Python
Let's make a WEB application for phone book with flask Part 3
I tried to make a todo application using bottle with python
Let's make a WEB application for phone book with flask Part 4
I got stuck trying to install various things with Mac El captain pip, so make a note