Skip to main content

Provide a middleware for Flask application, to fix redirection issue when the application runs behind a reverse proxy, like Nginx.

The redirection issue is that, when your website is HTTPS and a view returns a 301/302 reponse, the new URL mistakenly becomes HTTP.

This resolution requires you to configure Nginx so that it passes a custom header, to inform the scheme (HTTPS) of the original request. To do that, you just need to setup like this in your Nginx virtualhost config:

location / {
    include         proxy_params;
    proxy_pass      http://localhost:8000;
}

This is the common setup for Nginx on Debian/Ubuntu. Nginx on other distros may not have proxy_params file, you can add configuration like this:

location / {
    proxy_pass       http://localhost:8000;
    proxy_set_header Host              $http_host;
    proxy_set_header X-Real-IP         $remote_addr;
    proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

Currently, Flask Behind Proxy is only tested with Nginx web server.

Install

pip install flask-behind-proxy

Usage

from flask import Flask
from flask_behind_proxy import FlaskBehindProxy

app = Flask(__name__)
proxied = FlaskBehindProxy(app)

Other implementation

Other implementation is Flask Reverse Proxy. It is based on a header name that is not “standard” (in my terms, it is the name chosen by Debian/Ubuntu maintainer). I chose to make a new software myself, instead of contributing to Flask Reverse Proxy, because wilbertom seems not to be very reactive, and I want to have a clean code, without Python 2 backward compatibility, and newer tool (like pyproject.toml file).

Release files for flask-behind-proxy 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-behind-proxy 0.1.1
File Size Uploaded
flask-behind-proxy-0.1.1.tar.gz 3.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-behind-proxy 0.1.1
File Interpreter ABI Platform
flask_behind_proxy-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 7.1 kB

Release files / flask-behind-proxy-0.1.1.tar.gz

Download URL flask-behind-proxy-0.1.1.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ca82756f78ec4c23420e47148fb2d853b4b9a7a9b9324eb94bb9bbdc44a4a676
BLAKE2b-256 checksum
How to use checksums
a1c5609a63f32a08751dfa16fc352bc06aaa51fbab74a1515532872efbf64fa3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.0a2 CPython/3.6.7 Linux/4.18.0-15-generic

Release files / flask_behind_proxy-0.1.1-py3-none-any.whl

Download URL flask_behind_proxy-0.1.1-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a2bb35eb1e0e7f8425c50b8dc7a5139ae41dab49dab9c344a2e1c9dfe31c09a7
BLAKE2b-256 checksum
How to use checksums
0ce6f6e97c5ee4b308ca59ea4322915bebd0efa9fb2972d27347e204d70825f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.0a2 CPython/3.6.7 Linux/4.18.0-15-generic

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page