Skip to main content
https://travis-ci.org/wosc/nginx-db-auth.png

This packages answers an nginx auth subrequest by looking up the user/password in a database (mysql, postgresql, whatever sqlalchemy supports).

Usage

Configure database access

You’ll need to provide the DSN and the query using a configuration file:

[default]
dsn = postgresql://localhost/myusers
query = SELECT password FROM users WHERE username = :username AND role = :x_required_role
password_hash = bcrypt

See the sqlalchemy documentation for supported DSNs. Note that you have to install the respective driver python package (mysql-python, psycopg2, etc.) yourself.

Settings beginning with sqlalchemy. are stripped of that prefix and passed through to sqlalchemy.create_engine(). Settings ending in __literal__ are stripped of that suffixed and passed through ast.literal_eval(), for example sqlalchemy.pool_recycle__literal__=3600 calls create_engine(dsn, pool_recycle=3600) (and not ‘3600’).

The query gets passed as SQL parameters the basic auth username and password as well as any request headers (lowercase, and - replaced with _). (If that is not flexible enough for your usecase, you’ll have to run separate instances with specialized queries, for the time being).

The query must return the hashed password of the user. Since for simple cases you might get away with using the SQL functions provided by your database to hash the password (e.g. mysql WHERE password=encrypt(:password, password)), but you can also specify any hash supported by passlib as the password_hash to perform the comparison in Python (that’s why we need the stored password hash from the database). You need to pip install passlib to use this feature.

Set up HTTP service

Then you need to set up an HTTP server, either with a dedicated process:

$ nginx-db-auth-serve --host localhost --port 8899 --config /path/to/config

or as a CGI script, if you have infrastructure for that set up anyway. Here’s an example apache configuration snippet to do this:

ScriptAlias /nginx-auth /path/to/nginxdbauth/nginx-db-auth-cgi
<Location /nginx-auth>
  SetEnv NGINXDBAUTH_CONFIG /path/to/config
  CGIPassAuth On  # apache >= 2.4.13
  # SetEnv NGINXDBAUTH_LOGFILE /path/to/log  # optional, for debugging
</Location>

Configure nginx

Now you can set up a protected nginx location like this:

location /private/ {
    auth_request /auth;
    # ... define rest of location ...
}

location = /auth {
    proxy_pass http://localhost:8899;  # or http://mycgi/nginx-auth
    proxy_pass_request_body off;
    proxy_set_header Content-Length "";
    proxy_set_header X-Required-Role "superuser";
    proxy_set_header WWW-Authenticate "Basic realm=\"my realm\"";
}

The WWW-Authenticate header sent by nginx will simply be echoed back on 401 requests (since as far as I can tell, nginx does not provide another way to set the realm string in the auth_request case).

CHANGES

ws.nginxdbauth 1.2.0 (2026-08-31)

Changes

  • Support non-str values for sqlalchemy configuration (literal)

1.1.1 (2024-06-10)

Changes

  • Work around passlib/bcrypt bug (passlib)

1.1.0 (2023-03-19)

Changes

  • Make sqlalchemy options configurable (dbconfig)

  • Connect to database once on startup, not for each request (dbconnect)

  • Drop Python-2 support (py2)

1.0.4 (2023-02-07)

  • Update to sqlalchemy-2

1.0.3 (2022-05-04)

  • Fix passlib import error

1.0.2 (2018-03-28)

  • Echo back the WWW-Authenticate header for 401 responses.

1.0.1 (2018-03-28)

  • Enable logging errors to a file for debugging the CGI script.

1.0.0 (2018-03-27)

  • First release.

Release files for ws.nginxdbauth 1.2.0

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

Source distribution (sdist)

Source distribution for ws.nginxdbauth 1.2.0
File Size Uploaded
ws_nginxdbauth-1.2.0.tar.gz 6.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ws.nginxdbauth 1.2.0
File Interpreter ABI Platform
ws_nginxdbauth-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.5 kB

Release files / ws_nginxdbauth-1.2.0.tar.gz

Download URL ws_nginxdbauth-1.2.0.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
22371c6d981ce574d8123286da934906f37cbe3a534e8824caa93a942ffc331d
BLAKE2b-256 checksum
How to use checksums
e8cd16eeb060520da204f473cfcaf4faac00c24439b32374efe62292896b07af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Hatch/1.18.0 {"ci":null,"cpu":"x86_64","distro":{"id":"noble","libc":{"lib":"glibc","version":"2.39"},"name":"Ubuntu","version":"24.04"},"implementation":{"name":"CPython","version":"3.12.3"},"installer":{"name":"hatch","version":"1.18.0"},"openssl_version":"OpenSSL 3.0.13 30 Jan 2024","python":"3.12.3","system":{"name":"Linux","release":"6.8.0-138-generic"}} HTTPX2/2.5.0

Release files / ws_nginxdbauth-1.2.0-py3-none-any.whl

Download URL ws_nginxdbauth-1.2.0-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2a93ae20ebb902d7a005301c26180ad8415833a89d66b2f722abb96da3631469
BLAKE2b-256 checksum
How to use checksums
afdfc22e26bc38c371b3211497ac34a6bbaad561bda02fd5a7cdf1d6b5ba0873
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Hatch/1.18.0 {"ci":null,"cpu":"x86_64","distro":{"id":"noble","libc":{"lib":"glibc","version":"2.39"},"name":"Ubuntu","version":"24.04"},"implementation":{"name":"CPython","version":"3.12.3"},"installer":{"name":"hatch","version":"1.18.0"},"openssl_version":"OpenSSL 3.0.13 30 Jan 2024","python":"3.12.3","system":{"name":"Linux","release":"6.8.0-138-generic"}} HTTPX2/2.5.0

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

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