nginx auth_request
Use this setup when nginx already serves your site, for example with static files, several virtual hosts or PHP-FPM. nginx asks Tamamo about each request. Tamamo answers allow, check or block, and nginx forwards the traffic to your app itself.
Reverse proxy or auth_request
In a reverse proxy setup, Tamamo forwards every request. In auth_request mode, it answers a small question per request.
| Reverse proxy | auth_request | |
|---|---|---|
| Who forwards the traffic | Tamamo | nginx |
server.target | Your app | Empty |
| Large downloads and streaming | Pass through Tamamo | Straight from nginx |
| Works with | Any proxy | nginx with ngx_http_auth_request_module. For Caddy and Traefik, see forward auth. |
Before you start
You need:
Tamamo running as in Installation.
A working nginx that already serves your site. It needs the
auth_requestmodule, which the official nginx packages and Docker image include. This command printshttp_auth_request_modulewhen it is there:bashnginx -V 2>&1 | grep -o http_auth_request_modulenginx able to reach Tamamo. The examples assume both run in Docker on one network, so nginx reaches Tamamo at
tamamo:8923. With nginx outside Docker, use127.0.0.1:8923and publish Tamamo's port to the server only, as in Keep Tamamo's port private.
How the pieces fit
Tamamo serves its own pages under /.tamamo/. nginx forwards that prefix to Tamamo.
| Endpoint | Used by | Answer |
|---|---|---|
GET /.tamamo/auth | nginx, for every request | 204 allow, 401 needs a check, 403 blocked |
/.tamamo/... with X-Tamamo-Render: 1 | nginx, after 401 or 403 | The checking page, or the denied page |
POST /.tamamo/api/verify | The checking page's script | Stores the pass cookie after a solved check |
/.tamamo/health | You, to test | ok |
On a 401, nginx sends the visitor to @tamamo, which renders the checking page. A visitor who passes gets a pass cookie, and the next request gets 204.
Steps
Set up Tamamo. In
data/tamamo.yaml, set these three things in theserversection:yamlserver: target: "" # empty: nginx does all the forwarding ip_source: x-real-ip # nginx sends the visitor's IP in this header trusted_proxies: - 172.18.0.0/16 # the Docker network nginx is onTo find the right value for
trusted_proxies, follow Tell Tamamo to trust the proxy. If your compose file setsTAMAMO_TARGETorTAMAMO_IP_SOURCE, remove those lines there: they override the file.Also remove the
portssection of thetamamoservice in your compose file. Visitors must only reach Tamamo through nginx.Add Tamamo to your nginx configuration. The example serves
example.comover plain HTTP to stay short. In your own file, keep yourlisten,server_nameand certificate lines, and add theupstreamblock, the five Tamamo locations, and the two marked lines in eachlocationthat serves your site.nginxupstream tamamo { server tamamo:8923; keepalive 16; } server { listen 80; server_name example.com www.example.com; # Tamamo's pages, scripts, images and the verify endpoint. location /.tamamo/ { proxy_pass http://tamamo; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $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; } # Only nginx itself may ask Tamamo for a verdict. location = /.tamamo/auth { return 404; } location = /.tamamo/forward-auth { return 404; } # Asked before every request. Internal: visitors can't call it. location = /.tamamo-auth { internal; proxy_pass http://tamamo/.tamamo/auth; proxy_http_version 1.1; proxy_pass_request_body off; proxy_set_header Content-Length ""; proxy_set_header Connection ""; proxy_set_header Host $host; proxy_set_header X-Original-URI $request_uri; proxy_set_header X-Original-Method $request_method; 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; } # Renders the checking page (401) or the denied page (403). location @tamamo { proxy_pass http://tamamo; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host; proxy_set_header X-Tamamo-Render 1; proxy_set_header X-Original-URI $request_uri; 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; } # Your site. Every request is checked first. location / { auth_request /.tamamo-auth; # add this error_page 401 403 = @tamamo; # and this proxy_pass http://your-site:80; # whatever serves your site today proxy_http_version 1.1; proxy_set_header Host $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; } }/.tamamo-authisinternal, and@tamamoruns only after a401or403. Visitors can't reach either one directly.Reload nginx. Tamamo reloads
tamamo.yamlon its own within a few seconds.
Check that it works
Run these on the server. Use the Host header if DNS isn't set up yet.
bash
# 1. Tamamo answers through nginx.
curl -s -H 'Host: example.com' http://localhost/.tamamo/health
# ok
# 2. A visitor without a pass gets the checking page.
curl -i -H 'Host: example.com' http://localhost/ | head -n 1
# HTTP/1.1 200 OK
# 3. A script request without a pass is refused with JSON.
curl -i -H 'Host: example.com' -H 'Sec-Fetch-Mode: cors' -H 'Sec-Fetch-Dest: empty' http://localhost/
# HTTP/1.1 403 Forbidden
# {"error":"challenge_required"}Then open http://example.com in a browser. After the check, your site loads.
Common problems
nginx -treportsunknown directive "auth_request". Your nginx lacks the module. Check with the command in Before you start.Every page returns
500, and the log saysauth request unexpected status: 502. nginx can't reachtamamo:8923. Check that Tamamo runs and that the address in theupstreamblock is right.Part of the site skips the check. A
locationblock withoutauth_requestis not checked. Add the two lines to every block that serves the site, including static files.The checking page loops after you pass. The pass cookie isn't stored. Test through the same host name you open in the browser, and make sure the browser accepts cookies. Tamamo marks the cookie
Secureonly when nginx tells it the visitor used HTTPS, which theX-Forwarded-Protolines do.Every visitor shows the same IP in Activity. nginx isn't in
trusted_proxies. See step 1.Every page returns
500, and the log saysauth request unexpected status: 404.targetisn't empty, so Tamamo keeps/.tamamo/authclosed. Empty it as in step 1, and checkTAMAMO_TARGETin your compose file. See auth mode.
Related
- Behind a reverse proxy: let Tamamo forward the traffic instead.
- Sites: one
serverblock per domain, each with its own site. - Caddy and Traefik forward auth: the same idea for Caddy and Traefik.
