Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

What 401 Unauthorized Means

Despite the name, 401 is about authentication: the server does not know who you are, or the credentials sent were wrong or expired. A 401 response comes with a WWW-Authenticate header that names the method the server expects:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="Restricted"

A browser that sees Basic opens a login box. An API usually names Bearer, meaning it expects a token. If the server knows who you are and still refuses, the answer is 403; the difference is in 401 vs 403.

What Visitors Can Check

  • Credentials: retype the user name and password; a password manager may fill in an old one.
  • Session: log out and in again, or clear the site's cookies.
  • Address: a staging or test address often has its own login.
  • Private window: rules out an extension that changes requests.

Test from the Command Line

curl -i https://example.com/private/
curl -i -u user https://example.com/private/
curl -i -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/v1/items

With -u user, curl asks for the password, so it does not end up in your shell history. -i prints the headers, including WWW-Authenticate.

Basic Authentication on Nginx and Apache

Both read users from a password file created with htpasswd (package apache2-utils on Debian and Ubuntu, httpd-tools on AlmaLinux and Rocky Linux):

sudo htpasswd -c /etc/nginx/.htpasswd user

Nginx:

location /private/ {
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

Apache, in the site configuration or an .htaccess file:

AuthType Basic
AuthName "Restricted"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user

If correct credentials keep failing, the web server user may not be able to read the password file, or the path in the configuration is wrong; the error log says which. The full setup is in Nginx basic authentication. Basic authentication sends the password with every request, so use it only over HTTPS.

An Authorization Header That Never Arrives

The client sends a token, yet the application reports none. Common reasons:

  • Apache with PHP-FPM or CGI: Apache hides the Authorization header from the application by default. CGIPassAuth On in the site's directory block or .htaccess passes it.
  • Header names with underscores: Nginx ignores request headers such as X_API_KEY unless underscores_in_headers on; is set. Hyphens are the safer choice.
  • Redirects: curl does not send credentials on to another host after a redirect, and browsers drop the Authorization header when a redirect leads to another site. Call the final address directly.
  • A proxy or CDN in between that removes the header.

Log the received headers in the application for one test request to see what arrives.

Signed Tokens and the Server Clock

Tokens with an expiry time, such as JSON Web Tokens, and one-time codes are checked against the server's clock. A clock that is a few minutes off makes valid tokens look expired or not yet valid. Check with timedatectl and keep the clock in sync as described in chrony time sync.

401 or 403?

CodeMeaning
401Who are you? Send credentials.
403I know who you are; no.

A 403 is covered in 403 Forbidden.

Was this answer helpful?
Loading...