What Is a 401 Unauthorized Error and How Do You Fix It?

A 401 Unauthorized error means the server didn’t accept the request because it lacks valid authentication credentials. The credentials are missing, expired or wrong, or they never reached the server. RFC 9110 defines the 401 status code, also written HTTP 401, and requires the response to include a WWW-Authenticate header that says how to sign in.
A mistyped URL rarely causes a 401. A typo usually returns a 404, unless the wrong path falls inside a protected area. To fix a 401, find out which credential the server wants, then check that your request sends it and that it arrives.
Key takeaways
- A 401 is about authentication. The server doesn’t know who you are yet, or it rejected the proof you sent.
- A 403 Forbidden is a refusal. The server understood the request and won’t fulfill it, and signing in again with the same account won’t help.
- Every 401 must carry a
WWW-Authenticateheader. Read it first, because it names the scheme the server expects and often the reason for the refusal. - Common causes are a missing or expired token, a wrong password, the wrong auth scheme, a server clock that is off, and a proxy, CDN or redirect that drops the
Authorizationheader. - Test the URL from outside your network, with and without credentials, to see which server answers 401.
What Does a 401 Status Code Mean?
The 401 status code is an HTTP client error. RFC 9110 says the request “has not been applied because it lacks valid authentication credentials for the target resource.”
But the name is misleading. “Unauthorized” sounds like a permission problem, and a 401 is about identity. If the request had no credentials, the server is asking for them. If it had credentials, the server rejected them.
The server must also say how to authenticate. RFC 9110 requires a WWW-Authenticate header with at least one challenge on every 401. A typical exchange looks like this:
GET /admin/ HTTP/1.1
Host: www.example.com
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="Admin area"
The client then repeats the request with an Authorization header. A browser shows its sign-in prompt for a Basic challenge, and an API client adds the header in code.
GET /admin/ HTTP/1.1
Host: www.example.com
Authorization: Basic dXNlcjpwYXNzd29yZA==
You’ll see the error under several names:
- 401 Unauthorized
- HTTP Error 401
- 401 Authorization Required (Nginx and older Apache versions)
- HTTP Error 401.1, 401.2 or 401.3 (IIS)
{"message":"Unauthorized"}(Amazon API Gateway and many JSON APIs)
What Causes a 401 Unauthorized Error?
The server either received no credentials or received credentials it won’t accept.
| Cause | What happens | Where to look |
|---|---|---|
| No credentials sent | The request has no Authorization header, cookie or API key. This is the normal first response from a protected URL. In a browser, a cross-origin fetch() leaves out cookies unless it sets credentials: "include" | The request headers your client sends |
| Wrong username, password or key | A typo, a rotated password, a key copied with a trailing space or line break, or a test key sent to the production API | The credential store, the server’s auth log |
| Expired or revoked token or session | An access token passed its expiry time, or the session cookie timed out. RFC 6750 names this case invalid_token | The token’s exp claim, the WWW-Authenticate error text |
| Wrong authentication scheme | The client sends Basic and the server wants Bearer, the word Bearer is missing before the token, or the key is in the wrong header or a query string | The scheme named in WWW-Authenticate, the API docs |
| Clock skew | The server’s clock, or the clock of the system that issued the token, is off. A token then looks expired or not yet valid | NTP status on both systems, the token’s exp and nbf claims |
Authorization header lost on the way | A redirect to another host, a CDN or a server module removes the header before your application sees it | The redirect chain, CDN header settings, web server config |
| Token that fails validation | The token was issued for another audience, issuer or environment, or the identity provider rotated its signing keys and the server still has the old ones | The token’s aud, iss and kid values, the server’s JWT settings |
| Auth configuration error on the server | A staging password rule went live, or an auth service behind the server rejects every request | Recent deploys, web server and auth service logs |
The lost header is easy to miss, because the client code looks correct. The vendors document these cases.
- Redirects. curl sends credentials only to the first host. Its man page says they aren’t passed on when a redirect goes to another host. Browsers do the same for
fetch(), because the Fetch standard removesAuthorizationwhen a redirect crosses origins. A redirect fromexample.comtowww.example.comis enough. - Amazon CloudFront. AWS documents that CloudFront removes the
Authorizationheader from requests to the origin by default. - Apache with CGI or FastCGI. Apache hides
Authorizationfrom scripts unlessCGIPassAuth Onis set.
What Is the Difference Between 401 and 403?
A 401 means the server doesn’t have valid proof of who you are. A 403 means it understood the request and refuses it, with or without credentials.
| 401 Unauthorized | 403 Forbidden | |
|---|---|---|
| What it means | Credentials are missing or invalid | The server understood the request and refuses to fulfill it |
| The question it answers | Who are you? | Are you allowed to do this? |
| Required response header | WWW-Authenticate with at least one challenge | None |
| Typical causes | No token, expired token, wrong password, wrong scheme | Missing role or scope, IP block, WAF rule, file permissions |
| Should the client retry? | Yes, with new or refreshed credentials | Not with the same credentials |
| What the user does | Sign in, or sign in again | Ask for access, or use an account that has it |
| OAuth bearer token error (RFC 6750) | invalid_token | insufficient_scope |
A 407 Proxy Authentication Required is the proxy version of a 401. A proxy between you and the site wants credentials, and it sends a Proxy-Authenticate header. And a 404 can stand in for a 403, because RFC 9110 lets a server answer 404 when it doesn’t want to reveal that a protected resource exists.
If you build APIs, keep the two codes apart. Many API clients refresh the token and retry when they get a 401. An API that returns 401 for a permission problem makes those clients refresh and retry for nothing. An API that returns 403 for an expired token gives them no reason to refresh.
How Do You Read the WWW-Authenticate Header?
The first word of each challenge is a scheme the server accepts. The parameters after it often state the reason for the refusal. If the header lists several challenges, use the strongest scheme your client supports.
| Header value | What the server wants |
|---|---|
Basic realm="Admin area" | A username and password, sent as Authorization: Basic plus the Base64 form of user:password |
Bearer | An access token, sent as Authorization: Bearer YOUR_TOKEN. No token was received |
Bearer error="invalid_token", error_description="The access token expired" | A new token. The one you sent is expired, revoked or malformed |
Digest realm="...", nonce="..." | A hashed response built from the nonce. Send it with a client that supports Digest, such as curl --digest |
Negotiate or NTLM | Windows integrated authentication (Kerberos or NTLM), common on IIS and intranet sites |
But some APIs skip the header, even though the RFC requires it. In that case, read the response body, where most APIs put an error code or message.
How Can Visitors Fix a 401 Error?
Sign in again. On a website, a 401 usually means your session expired or the password you entered didn’t match.
- Enter your username and password again. Check Caps Lock and any password your browser filled in for you.
- Sign out, then sign back in to replace an expired session.
- Clear the cookies for that site, or open the page in a private window. A stale cookie can keep sending an old session.
- Check the address. A shared link to a members-only or admin page needs an account.
Don’t keep guessing passwords, because many sites lock an account after repeated failures. If a correct password still returns a 401, the problem is on the site, so send the owner the URL and the time of the error.
How Do You Check a 401 From Outside Your Network?
Send the request from somewhere else, first without credentials and then with them. The results show what the server expects and which server in the path answers.
A 401 is a challenge. The second request answers it with an Authorization header.
Step 1: Request the URL Without Credentials
Open the free Web Server Test, enter the protected URL, pick the HTTP method your client uses and run it from three locations. No sign-up is needed. Each location reports the status code, the response headers and the redirect chain.
A 401 is the expected result here. Note the scheme in the WWW-Authenticate response header. The Server header often shows which layer answered.
Step 2: Send the Credentials Your Client Uses
Run the test again with the Basic Auth option, or add a custom header such as Authorization: Bearer YOUR_TOKEN. Use a test account or a short-lived token.
- 200 from every location: the server and the credentials are fine. Look at your own client’s stored credentials and clock.
- 401 from every location: the credential is wrong or expired, or the header doesn’t reach the application.
- 401 from some locations only: a CDN, WAF or regional server handles those requests differently.
- 403: the credentials were accepted, and the account lacks permission.
Step 3: Check the Redirect Chain
If the result shows a redirect to another host name, your API client may drop the Authorization header on the second request. Call the final URL directly. With curl, add -v to see the headers sent on each hop:
curl -v -L -H "Authorization: Bearer $TOKEN" https://example.com/api/orders
Step 4: Compare the Edge With the Origin
If a CDN sits in front, send the same request straight to the origin IP:
curl -i --resolve www.example.com:443:203.0.113.10 \
-H "Authorization: Bearer $TOKEN" https://www.example.com/api/orders
If the origin returns 200 and the public URL returns 401, the CDN or proxy removes or changes the header.
And if both return 401, the fix is on the web server or in the application.
How to Fix a 401 Error by Stack
Start with the log of the server that sent the 401. It usually states the reason, even when the response doesn’t.
How to Fix a 401 in Nginx
Nginx sends its own 401 when auth_basic protects a location. The error log names the cause:
no user/password was provided for basic authenticationmeans the request had no credentials.user "name" was not found in "/etc/nginx/.htpasswd"means the user isn’t in the password file.user "name": password mismatchmeans the password is wrong.
Check that auth_basic_user_file points to the file you edited. If a staging rule reached production, remove it or set auth_basic off; for that location.
With auth_request, Nginx asks another service to approve each request. If that subrequest answers 401, Nginx returns the 401 and its WWW-Authenticate header to the client. So the reason is in the auth service’s log.
If Nginx is a reverse proxy, add $upstream_status to your log_format to see if the application sent the 401. Nginx passes Authorization to the upstream by default, so look for a proxy_set_header Authorization ""; line that clears it.
How to Fix a 401 in Apache
Apache returns 401 for a path protected with AuthType and Require. The error log has a code for each failure:
AH01618: user name not foundmeans the user isn’t in the file thatAuthUserFilepoints to.AH01617: ... Password Mismatchmeans the password is wrong.AH01614: client used wrong authentication schememeans the client sent something other than Basic.
Add or update a user with htpasswd /path/to/passwords name. If you can’t find the rule, search the site’s .htaccess files for AuthType.
Or your application checks tokens itself and never sees them. Apache hides the header from CGI and FastCGI applications, PHP-FPM through mod_proxy_fcgi included. Set this in the directory block or .htaccess (Apache 2.4.13 and later):
CGIPassAuth On
How to Fix a 401 in IIS
IIS adds a substatus code that narrows the cause. Find it in the sc-substatus field of the IIS log.
| Code | Microsoft’s description | What to check |
|---|---|---|
| 401.1 | Logon failed | The username and password, and the account’s status |
| 401.2 | Logon failed due to server configuration | Which authentication methods are enabled for the site (Anonymous, Windows, Basic) |
| 401.3 | Unauthorized due to ACL on resource | NTFS permissions on the files for the account the site runs as |
How to Fix a 401 From an API With Bearer Tokens
Check the token before you check the server.
- Confirm the header format. It’s
Authorization: Bearer YOUR_TOKEN, with one space and no quotes. - Decode the token if it’s a JWT and read
exp,nbf,audandiss. An expired token needs a refresh. A wrong audience or issuer means the token belongs to another API or environment. If thekidin the token header isn’t in the server’s key set, the signing keys were rotated and the server needs to reload them. - Compare clocks. RFC 7519 lets servers allow a small leeway for clock skew, “usually no more than a few minutes.” A server that is further off rejects valid tokens. On Linux,
timedatectlshows if the clock is synchronized. - If only browsers fail, check the CORS preflight. Browsers send the
OPTIONSpreflight request without credentials, so an auth layer that demands them answers 401. Let preflight requests through without authentication. - Refresh once and retry once. If the new token also gets a 401, stop and log the response.
On the server side, return 401 with WWW-Authenticate: Bearer error="invalid_token" for a bad or expired token. Return 403 with insufficient_scope when the token is valid but lacks the scope.
How to Fix a 401 on Cloudflare
On a proxied site, Cloudflare’s docs say a 401 usually comes from the origin server. Run the origin test from Step 4 to confirm it.
Cloudflare Access is the exception. It normally redirects visitors to a login page, but the docs list cases where it returns 401. One is an AJAX request with an expired session. Another is a service token request when strict service token authentication is on. Check the Access policy and the service token’s expiry date.
How to Fix a 401 on AWS
With Amazon CloudFront, forward the header. CloudFront removes Authorization from origin requests by default, and the origin then answers 401. For responses that differ by user, add the header to the cache key with a cache policy. An origin request policy, such as the managed AllViewer policy, also forwards it. But AWS warns that when the header isn’t part of the cache key, CloudFront can serve the same cached response to authorized and unauthorized viewers. After the change, rerun the Web Server Test with the header and confirm a 200.
Amazon API Gateway returns 401 with the UNAUTHORIZED gateway response when a Lambda or Cognito authorizer fails to authenticate the caller, or when a JWT authorizer can’t validate the token. Check the token source header in the authorizer settings and the issuer and audience values. Other failures return 403 there, including a Lambda authorizer that denies access, a missing authentication token and an expired AWS token.
An Application Load Balancer returns 401 from its own authentication rules. The causes AWS lists include a rule that denies unauthenticated requests, an identity provider that denied access, a login that timed out, and a missing or invalid JWT.
How Do You Keep 401 Errors From Coming Back?
Repeat 401s usually come from credentials that expire on a schedule or from a change in front of the application.
- Refresh access tokens before they expire, and handle a 401 with one refresh and one retry.
- Rotate API keys with an overlap period when the old and new credentials both work.
- After any CDN, proxy or redirect change, test a signed-in request through the public URL.
Plus, check the signed-in path on a schedule. A basic uptime check requests the home page without credentials and keeps passing while signed-in users get a 401.
For 24/7 coverage, website monitoring from Dotcom-Monitor tests websites and APIs as often as every minute from 30+ locations and sends alerts by email, SMS or phone when a check fails.
For server errors, separate guides cover the 504 Gateway Timeout and the 501 Not Implemented error.
Frequently Asked Questions About 401 Errors
Is a 401 Error My Fault or the Website’s?
It can be either. If you typed a wrong password or your session expired, signing in again fixes it. If a correct password or a fresh token still gets a 401, the site or API has a configuration problem that only its owner can fix.
Why Do I Get a 401 With the Correct Password or Token?
The server probably isn’t receiving what you send. A redirect, a CDN or a web server setting can remove the Authorization header before it reaches the application. A token issued for a different environment and a wrong server clock are the other usual causes.
Does a 401 Error Hurt SEO?
Only on pages you want indexed. Google treats a 401 like other 4xx errors. It doesn’t index the URL, and it removes a URL that was indexed before. That’s the right result for an admin area. It hurts when a staging password rule reaches production and public pages start returning 401 to Googlebot. Google also says not to use 401 or 403 to slow crawling.
Which 401 Fix Should You Try First?
Start with the WWW-Authenticate header, then send the same request with credentials from outside your network. If it succeeds there, fix the client’s stored credentials, clock or redirect handling. If it fails everywhere, read the log of the server that answered. Renew the credential if the log says it’s wrong or expired. And if the log shows no credential at all, look for the redirect, CDN or server setting that drops the Authorization header.
The Web Server Test from Step 1 shows the status code, the response headers and the redirect chain from three locations in one run. A free Dotcom-Tools account adds more locations and 30 days of test history.
Sources
- RFC 9110, HTTP Semantics, section 15.5.2 (401 Unauthorized)
- RFC 9110, section 11.6.1 (WWW-Authenticate)
- RFC 9110, section 15.5.4 (403 Forbidden)
- RFC 6750, OAuth 2.0 Bearer Token Usage, section 3.1 (error codes)
- RFC 7519, JSON Web Token, section 4.1.4 (exp claim and clock skew)
- MDN Web Docs, 401 Unauthorized
- curl man page, —location (credentials and redirects)
- WHATWG Fetch Standard, HTTP-redirect fetch
- nginx, ngx_http_auth_basic_module
- nginx, ngx_http_auth_request_module
- nginx source, ngx_http_auth_basic_module.c (log messages)
- Apache HTTP Server 2.4, Authentication and Authorization
- Apache HTTP Server 2.4, CGIPassAuth
- Apache httpd source, mod_auth_basic.c (log messages)
- Microsoft, HTTP status codes in IIS
- Cloudflare, Error 401
- AWS, HTTP 401 status code (Unauthorized) in CloudFront
- AWS, Gateway response types for API Gateway
- AWS, Troubleshoot your Application Load Balancers
- Google, How HTTP status codes affect Google’s crawlers
Free HTTP/HTTPS test from several locations: status code, redirects, TTFB, DNS, connect and TLS timing.
Run a free server test

