Nginx 401 Unauthorized: auth_basic, auth_request, and Swallowed Headers

Nginx 401 Unauthorized: auth_basic, auth_request, and Swallowed Headers

Written by

in

Lab-tested: nginx 1.26.3 + Python 3.13.5 backend (lab) · 2 Dec 2026 · Reproduced and fixed on a real nginx

Fix Box — Try This First (10 Seconds)

An Nginx 401 comes from one of three places: auth_basic in Nginx itself, an auth_request subrequest, or your backend behind a proxy that ate the Authorization header. First, read the WWW-Authenticate realm — it names the layer asking.

Second, test the backend directly, bypassing Nginx; if it works direct and fails through Nginx, the proxy is swallowing a header. Third, check header names with underscores — Nginx drops them by default.

Still stuck? Walk the 401 Decision Tree or start from the pillar 401 Unauthorized: Causes and How to Fix It.

Table of Contents

Which Layer Answered? Read the Realm

Nginx sits in front of your app, so a 401 can be born in three different places. The WWW-Authenticate realm is the fastest tell.

A realm like Lab Area or your site name usually means Nginx auth_basic answered before your app ever saw the request. A realm from your API (api, backend) means the backend answered, possibly through the proxy.

No challenge at all points at a layer even further out, like a WAF or CDN.

This layer question decides everything else. Fixing your app does nothing when Nginx itself is rejecting the password.

Fixing auth_basic does nothing when the proxy strips the header your backend needs. Our lab reproduced all three layers on a real nginx 1.26.3 so you can compare your capture against known-good ones.

The Exact Responses (Verbatim from Our Lab)

These are real responses from nginx 1.26.3 in our lab on 2 Dec 2026, sanitized of lab credentials:

HTTP/1.1 401 Unauthorized
Server: nginx/1.26.3
WWW-Authenticate: Basic realm="Lab Area"
HTTP/1.1 401 Unauthorized
Server: nginx/1.26.3
WWW-Authenticate: Bearer realm="backend"
{"error": "missing_or_bad_credentials", "headers_seen": ["Accept", "Connection", "Host", "User-Agent"]}

Notice the second capture: our backend lists the headers it actually received, and Authorization is not among them. That one line proves the proxy ate the header.

It is the single most useful debugging trick in this article: make the backend echo header names (never values) and compare direct vs proxied.

Cause 1: auth_basic in Nginx

With auth_basic, Nginx itself checks the password against an htpasswd file and returns 401 with a Basic challenge. Our lab cases 1–3 cover the full cycle: no credentials → 401, wrong password → 401, correct password → 200.

The app behind never runs for the failures.

Two traps live here. First, the password file format: Nginx accepts crypt(), apr1, and {SHA}/{SSHA} hashes; a file generated with an unsupported scheme (like bcrypt $2y$ from newer htpasswd -B) fails every login.

Generate with openssl passwd -apr1 or htpasswd without -B. Second, inheritance: auth_basic set at server level applies to every location unless a location sets auth_basic off. A health-check location suddenly demanding a password is usually this.

Cause 2: auth_request Subrequest Rejects You

auth_request delegates the decision: Nginx fires a subrequest to your auth service and mirrors its verdict. A 2xx from the subrequest allows access; 401 or 403 denies with that code, and for 401 the client also receives the subrequest's WWW-Authenticate header.

Our lab cases 4–5 show both outcomes: no token → 401, valid Bearer token → 200.

The classic failure is the subrequest not receiving the credentials. The auth location must forward them explicitly with proxy_set_header Authorization $http_authorization;, plus proxy_pass_request_body off and an empty Content-Length.

Forget the header line and your auth service sees an anonymous request and correctly says no — forever, for everyone.

Cause 3: Proxy Swallows the Authorization Header

When Nginx proxies to a backend, some configurations explicitly clear the header: proxy_set_header Authorization ""; appears in hardening guides and in configs copied from setups where Nginx itself consumed the credentials. Our lab case 6 is exactly that: the client sent a valid Bearer token, the backend received no Authorization at all, and answered 401.

Case 7 adds proxy_set_header Authorization $http_authorization; and the same request returns 200.

Diagnose it with the echo trick: have the backend list received header names, then compare a direct request against a proxied one. If Authorization vanishes only through Nginx, grep your config for proxy_set_header Authorization.

Also remember proxy_set_header inheritance: defining any proxy_set_header in a location drops the ones inherited from server level. That silent reset causes exactly this class of bug.

Cause 4: Underscore Headers Dropped by Default

Nginx ignores headers containing underscores unless underscores_in_headers on; is set. An API key sent as X_Api_Key simply never reaches your backend.

Our lab case 8 shows the 401 with the header missing from the backend's list; case 9 enables underscores_in_headers on; and the header arrives, returning 200. Case 10 is the control: the hyphenated X-Api-Key passes by default.

The cleanest fix is renaming the header to use hyphens, which is also the HTTP convention. Enable underscores_in_headers only when you cannot change the client.

Either way, this failure is invisible in most logs: the client sent the header, Nginx silently dropped it, and the backend honestly reports it missing.

Our Lab Reproduction

Stack: nginx 1.26.3 (real binary, nginx -t clean) plus a Python 3.13.5 backend on localhost, tested 2 Dec 2026 on our lab machine. The backend echoes received header names and requires a Bearer token or API key.

Ten cases cover auth_basic, auth_request, header stripping, and underscore handling. Full captured output, including the Nginx access log, is stored with this package as lab evidence.

The before/after pair below is verbatim lab output, with only lab credential values removed:

# BEFORE (proxy strips Authorization)
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="backend"
{"error": "missing_or_bad_credentials", "headers_seen": ["Accept", "Connection", "Host", "User-Agent"]}

# AFTER (proxy_set_header Authorization $http_authorization)
HTTP/1.1 200 OK
{"ok": true, "headers_seen": ["Accept", "Authorization", "Connection", "Host", "User-Agent"]}

Honest lab note: the backend is a small Python service, not your production app. What is real is Nginx itself — same binary, same directives, same phase behavior. The header-echo method transfers directly to any backend framework.

The Fix, Step by Step

  1. Identify the layer from the realm, then test the backend directly:
curl -si https://your-site.example/protected
curl -si http://127.0.0.1:BACKEND_PORT/ -H "Authorization: Bearer YOUR_TOKEN"
  1. For auth_basic, verify the password file scheme and test with known credentials:
openssl passwd -apr1 'your-password'
curl -si -u user:password https://your-site.example/protected
  1. For proxy problems, forward the header explicitly and reload:
location /api/ {
    proxy_pass http://127.0.0.1:8000;
    proxy_set_header Authorization $http_authorization;
}
  1. For auth_request, forward credentials in the auth location:
location = /authcheck {
    internal;
    proxy_pass http://127.0.0.1:8000/auth;
    proxy_set_header Authorization $http_authorization;
    proxy_pass_request_body off;
    proxy_set_header Content-Length "";
}
  1. For underscore headers, rename to hyphens, or as a last resort add underscores_in_headers on; in the server block. Then run nginx -t and reload.

What Did Not Work

Our first lab attempt put return 200 in the same location as auth_basic — and every request returned 200, password or not. The return directive runs in the rewrite phase, before the access phase where auth_basic lives.

If your protected location seems to ignore passwords entirely, check for a return or rewrite that answers first. Serving a real file or proxying fixes the phase order.

Retrying with the same credentials through the stripping proxy also failed identically. The backend never saw the header, so no credential could ever work. When the backend's header list lacks Authorization, stop debugging passwords and fix the proxy.

Enabling underscores_in_headers on; alone was not enough in our first pass: the header arrived, but the backend read the hyphenated name and still returned 401. The app must read the exact header name that arrives. Rename to hyphens end-to-end where you can.

Prevention

Add a backend endpoint (internal only) that echoes received header names, and smoke-test it through Nginx after every config change. Grep configs for proxy_set_header Authorization "" during reviews.

Keep API keys in hyphenated headers. Log the realm on every 401 so the answering layer is obvious.

For the full cross-layer flow see the pillar 401 Unauthorized: Causes and How to Fix It and the 401 Checker. Related: 401 Basic Auth Failures and WordPress 401 Errors.

FAQ

How do I know if Nginx or my app sent the 401? Read the realm in WWW-Authenticate and test the backend directly. If the backend answers 200 direct but 401 through Nginx, Nginx or the proxy path is the problem.

Why does auth_basic accept every password? Probably phase order: a return or rewrite in the location answers before the access phase. Serve a file or proxy instead of returning directly.

Why does my API key work locally but not through Nginx? Check the header name.

Underscores are dropped by default. Rename X_Api_Key to X-Api-Key, or enable underscores_in_headers on; and make the app read the underscore name.

Does Nginx forward Authorization by default? Normally yes, but explicit proxy_set_header rules can clear it, and defining any proxy_set_header in a location resets inheritance. Set proxy_set_header Authorization $http_authorization; explicitly to be safe.

auth_request returns 401 for everyone. Where do I look? The auth location.

It must forward Authorization to the auth service. Without that line the subrequest is anonymous and always denied.

Sources and Date

Lab tests run 2 Dec 2026 on nginx 1.26.3 with a Python 3.13.5 backend (localhost). Header captures and access log lines in this article are from that run, sanitized of lab credential values.

Documentation references verified live on 2 Dec 2026.

References

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


More posts