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 a 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.

REMEMBER: Unix file permissions mean that you should edit /etc/nginx/nginx.conf and change user www-data; to user {you}; Or (recursively) change the owner on all the repo directories to www-data.

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.


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

.flaskenv

If a .flaskenv is found in the repo directory then nginx and systemd will read paramters from that file. The keywords should be uppercase version of the known parameters. Unknown parameters will be ignored.

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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_nginx-0.9.12.tar.gz (39.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_nginx-0.9.12-py3-none-any.whl (54.9 kB view details)

Uploaded Python 3

File details

Details for the file flask_nginx-0.9.12.tar.gz.

File metadata

  • Download URL: flask_nginx-0.9.12.tar.gz
  • Upload date:
  • Size: 39.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.5

File hashes

Hashes for flask_nginx-0.9.12.tar.gz
Algorithm Hash digest
SHA256 3d0abf82480e3b2c7a4a7fd919342533b647b877b1f49c929b0d5e824278a950
MD5 4f769402839ebb2eeae34723b84e5bdb
BLAKE2b-256 1f9011ce164e2c8dfc6c5ed51d1da82e55aeda2c0dd378274b34989a652bc6f5

See more details on using hashes here.

File details

Details for the file flask_nginx-0.9.12-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_nginx-0.9.12-py3-none-any.whl
Algorithm Hash digest
SHA256 a789463ff1987d22a1f8f5ef8f94a118e5b5689cec6a7f94fd191b8ff97c5eb7
MD5 c43ca9d7196aa2cd365e08520e56fa57
BLAKE2b-256 0d2e10870eda00c9c87d6deaa1d0bcd6082b277430e9bb415db059da33c1c26e

See more details on using hashes here.

Release history Release notifications | RSS feed

0.9.16

2 files

0.9.15

2 files

0.9.14

2 files

This release

0.9.12 This release

2 files

0.9.11

2 files

0.9.10

2 files

0.9.9

2 files

0.9.8

2 files

0.9.7

2 files

0.9.6

2 files

0.9.5

2 files

0.9.4

2 files

0.9.3

2 files

0.9.2

2 files

0.9.1

2 files

0.9.0

2 files

0.8.18

2 files

0.8.17

2 files

0.8.16

2 files

0.8.15

2 files

0.8.5

2 files

0.8.4

2 files

0.8.3

2 files

0.8.2

2 files

0.8.0

2 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