Skip to content

HTTP 401 Unauthorized Error: What It Means and How to Fix It

What the HTTP 401 Unauthorized error means, how it differs from 403, and how to fix it with curl, API tokens, htpasswd, Apache and Nginx.

Category
Web Servers
Covers
Apache, curl, Nginx
On this page

An HTTP 401 Unauthorized error means the request needs authentication, and you either didn’t send any credentials or the ones you sent weren’t accepted. Despite its name, 401 is about authentication (who you are), not authorization (what you’re allowed to do). Logging in, or sending a valid password, token or API key, usually fixes it.

This guide explains what the 401 status code means, how it differs from 403, and how to fix it in browsers, with curl and APIs, and on Apache and Nginx servers that use password protection.

What Does 401 Unauthorized Mean?

The HTTP standard (RFC 9110) says a 401 means the request “has not been applied because it lacks valid authentication credentials for the target resource.” A server that sends 401 must also send a WWW-Authenticate header saying how to authenticate, for example:

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

Browsers react to Basic and Digest challenges by showing a login pop-up. APIs usually send WWW-Authenticate: Bearer, which means “send a valid token”.

You may see it as:

  • 401 Unauthorized or HTTP Error 401
  • 401 Authorization Required (Nginx)
  • A browser login box that keeps coming back
  • {"error": "unauthorized"} or invalid_token from an API
  • HTTP Error 401.1 - Unauthorized: Access is denied due to invalid credentials (IIS)

401 vs 403

401 Unauthorized403 Forbidden
MeaningWho are you? Prove it.I know the request, and the answer is no.
CauseMissing, wrong or expired credentialsPermissions, access rules, IP blocks
Logging in helps?YesUsually not
WWW-Authenticate headerRequiredNot used

If you’re logged in and still blocked, see our 403 Forbidden guide.

Common Causes of a 401 Error

  1. Wrong username or password, including typos, caps lock, or a password that was changed.
  2. Expired session, token or API key: JWT and OAuth access tokens expire, often after an hour or less.
  3. No credentials sent: the Authorization header is missing or misspelled.
  4. The wrong auth scheme: sending a token as Basic instead of Bearer, or the reverse.
  5. The Authorization header is dropped on the way, for example by Apache when PHP runs through PHP-FPM or CGI.
  6. Password-protected directories using .htpasswd, with the user missing from the file or the file unreadable.
  7. Clock skew: a server with the wrong time treats valid tokens as expired or not yet valid.

Quick Fixes If You’re Visiting the Site

  1. Check the URL. You may have opened a private area by mistake.
  2. Log in again, carefully re-typing the username and password.
  3. Clear the site’s cookies and cache, or try a private window, to remove an expired session.
  4. Reset your password if you aren’t sure it’s right.
  5. Contact the site owner if you were given access and it still fails.

How to Fix 401 Errors with curl and APIs

Send the credentials the API expects

Look at the WWW-Authenticate header first. It tells you which scheme the server wants:

curl -sI https://api.example.com/v1/items | grep -i www-authenticate

Then send the matching credentials:

curl -u admin:secret https://example.com/private/
curl -H "Authorization: Bearer $API_TOKEN" https://api.example.com/v1/items
curl -H "X-API-Key: $API_KEY" https://api.example.com/v1/items

-u sends HTTP Basic authentication. Bearer tokens go in the Authorization header. Some APIs use their own header such as X-API-Key; check the API’s documentation. Use curl -v to see exactly which headers are sent.

Refresh expired tokens

If a request that worked earlier now returns 401, the token has probably expired. OAuth clients should use their refresh token to get a new access token and retry once. For a JWT, you can read the expiry time (the exp claim, in Unix seconds) by decoding the token’s middle part. Most JWT libraries also allow a small leeway for clock differences.

Check the server clock

Token validation depends on the correct time. If a server’s clock is wrong, valid tokens are rejected as expired or not yet valid. Check that time sync is on:

timedatectl

The output should show System clock synchronized: yes.

How to Fix 401 Errors on Your Own Server

Step 1: Check the logs

Apache and Nginx log failed Basic authentication with the reason:

sudo tail -n 20 /var/log/apache2/error.log
sudo tail -n 20 /var/log/nginx/error.log

Typical messages are AH01617: user admin: authentication failure for "/private/": Password Mismatch and AH01618: user bob not found in Apache, and user "bob" was not found in "/etc/nginx/.htpasswd" or user "admin": password mismatch in Nginx.

Step 2: Fix the password file

Create or update users with htpasswd (from the apache2-utils package on Ubuntu and Debian, or httpd-tools on RHEL-based systems). Use -c only when creating a new file, because it overwrites an existing one:

sudo htpasswd -c /etc/nginx/.htpasswd admin
sudo htpasswd /etc/nginx/.htpasswd bob

The web server must be able to read the file. In Nginx, point to it from the protected location:

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

For Apache, see how to set up Basic authentication with .htaccess. Always use HTTPS with Basic authentication: the password is only Base64-encoded, not encrypted.

Step 3: Pass the Authorization header to PHP

When PHP runs through PHP-FPM or CGI under Apache, the Authorization header may not reach PHP. Your API then returns 401 even with a correct token, because the app never sees it. On Apache 2.4.13 and later, add this to the virtual host or .htaccess:

CGIPassAuth On

On older setups, the common workaround is a rewrite rule (WordPress includes it in its default .htaccess):

RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

Nginx passes the Authorization header to proxy_pass backends by default, but check that no proxy_set_header Authorization ""; line removes it.

Step 4: Check the application’s auth settings

For your own APIs and apps:

  • Make sure the client and server use the same secret or public key to sign and verify tokens. A key change on one side makes every token invalid.
  • Check that the token’s audience (aud) and issuer (iss) match what the API expects.
  • Return a clear error body (for example invalid_token or token_expired) along with the 401, so clients know whether to refresh or re-login.

For the full list of codes and what they mean, see our HTTP status codes list. Guides for other common errors:

Frequently Asked Questions

What does 401 Unauthorized mean?

It means the server needs you to authenticate, and you either didn’t send credentials or they were wrong or expired.

What is the difference between 401 and 403?

401 means “authenticate first”, so logging in or sending a valid token fixes it. 403 means the server refuses the request even if it knows who you are.

Why does my browser keep asking for a password?

The site uses HTTP Basic authentication, and the username or password is wrong, so the server keeps sending 401. Check the credentials, or ask the site owner to check the password file.

Why does my API return 401 with a valid token?

The token may have expired, the header may be in the wrong format (it should be Authorization: Bearer <token>), the server’s clock may be wrong, or Apache may be dropping the Authorization header before it reaches PHP.

How do I send credentials with curl?

Use curl -u user:password URL for Basic authentication, or curl -H "Authorization: Bearer TOKEN" URL for token-based APIs.

Conclusion

An HTTP 401 Unauthorized error means the server needs valid credentials. Visitors can usually fix it by logging in again or clearing cookies. Developers should check the WWW-Authenticate header, send the right credentials in the right format, and refresh expired tokens. Admins should check the auth log messages, the .htpasswd file, and that the Authorization header reaches the application.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

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


Back to top