Skip to main content

flask-nginx aka "footprint" 👣

I use this to generate config files for my Flask/Quart apps. Currently systemd and nginx. It only depends on jinja2 and click which are also dependencies of Flask/Quart (installing python-dotenv is encouraged).

Will also work with Starlette apps too e.g. Fastapi (but not fully tested!).

It is intended to be installed into the same virtual environment that the Flask app inhabits so it can introspect the app (for static folders and routes mainly).

export FLASK_APP=your_package.wsgi
footprint config nginx www.example.com > example.conf
footprint config nginx-install example.conf
# install in ~/.config/systemd/user
export FLASK_APP=your_package.wsgi
footprint config systemd --user  > example.service
footprint config systemd-install --user example.service

will install nginx and systemd files that will statically serve you 'static' assets and run the Flask app with gunicorn.

Mostly I've found that confectioning these files by hand are highly error prone. These commands will at least get the absolute pathnames correct :)

footprint will install a Quart or a starlette/fastapi using the --asgi flag.

Install with:

uv add flask-nginx
python -m pip install flask-nginx

If footprint finds a pyproject.toml file in the current directory if will try to load [tool.footprint] values into its global configuration object.

Unless you specify a configuration file yourself with footprint -c confg.toml ....

nginx, systemd and all that

Note that these configuration generating functions are not infallible. Please examine the generated configure files carefully! They are mainly useful for getting the directory names correct etc. So if you move your repo then you can easily regenerate and reinstall the files.

Test an nginx config with e.g.:

cd ~/Sites/websites/ppr
export FLASK_APP=ppr.wsgi
footprint config nginx  example.org | footprint config nginx-run -

This will run nginx at the terminal listening on port 5000 and run the backend website.

To install a website:

footprint config nginx example.org -o website.conf
footprint config systemd [--user] -o website.service
# nginx requires sudo (default) or su
footprint config nginx-install website.conf
# if you can install into ~/.config/systemd/user
footprint config systemd-install [--user] website.service

You can test this locally by editing /etc/hosts and adding a line:

127.0.0.1 example.org

to the file.

If you install as "user" (i.e. footprint config systemd --user ...) then to ensure that the user systemd starts at boot time use: sudo loginctl enable-linger <user>

See here:

But what’s the real reason for having user services? To answer that, we have to realize when the enabled service starts and stops. If we enable a user service, it starts on user login, and runs as long as there is a session open for that user. Once the last session dies, the service stops.

Installing flask-nginx as a tool

flask-nginx doesn't have to be installed into each project. You can install it once as a "global" tool with uv tool install flask-nginx and then use the --python-executable option to identify the "correct" virtual environment to use (similar to mypy).


See digitalocean.com here for a tutorial about serving flask from nginx.

bots, nginx and the --exclusive option

Much of the web traffic today is bots and scrapers. Most of the time your Flask/Quart/Starlette app will be processing 404s. To offload this to nginx you should (optionally) add a 404.html (not a template -- a full html page) file to your /static directory and use the --exclusive option for footprint config nginx: which checks the routes of the current app and gets nginx to generate a 404 if these route prefixes are not found.

The downside of this is that you now cannot add any new routes to your app without regenerating and reinstalling the nginx.conf file.

Metadata

Release files for flask-nginx 0.9.17

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-nginx 0.9.17
File Size Uploaded Provenance Actions
flask_nginx-0.9.17.tar.gz 42.7 kB Without provenance Details for flask_nginx-0.9.17.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flask-nginx 0.9.17
File Interpreter ABI Platform Actions
flask_nginx-0.9.17-py3-none-any.whl Python 3 none any Details for flask_nginx-0.9.17-py3-none-any.whl

Total release size: 100.4 kB

Release files / flask_nginx-0.9.17.tar.gz

Download URL flask_nginx-0.9.17.tar.gz
Size 42.7 kB
Tags Source
SHA-256 checksum
How to use checksums
188039f7e28fdeac79c3da8cc75e9c6ba7ab744b84581a816b272e49cf84eaf3
BLAKE2b-256 checksum
How to use checksums
9c915e973f17fcbd8a5483991cb83549a3f9043fecdf504c05c8748ca0677c2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.5

Release files / flask_nginx-0.9.17-py3-none-any.whl

Download URL flask_nginx-0.9.17-py3-none-any.whl
Size 57.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fbdb4283e10d6b3aea2ffefcccf1abd9b93666329dd481ff81b4ff7d31de57d8
BLAKE2b-256 checksum
How to use checksums
d51df6dac7fcd8e1d5246d4f47942c12a987914b0d7d390d54cc0f2efac35b53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.5

Security for flask-nginx 0.9.17

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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