Skip to content
Log In Sign Up info@muvcloud.com

The Short Answer

401 Unauthorized403 Forbidden
QuestionWho are you?Are you allowed?
ConceptAuthenticationAuthorization
Challenge headerRequiredNot required
Logging in helpsYesUsually not
Typical causeNo or bad loginRule or permission

Despite its name, 401 Unauthorized is about missing or wrong credentials. Both codes are listed with the others in HTTP status codes explained.

401 Unauthorized

A server sends 401 when a resource needs authentication and the request has none, or has credentials that failed: a wrong password, an expired token, or a revoked API key. Sending correct credentials with the same request should succeed. APIs use 401 for a missing or invalid bearer token in the Authorization header.

The WWW-Authenticate Header

RFC 9110 requires every 401 response to include a WWW-Authenticate header that names the scheme the client should use, such as Basic or Bearer. For Basic authentication, browsers react to it by showing a login box. Look at the header with curl:

curl -I https://example.com/admin/

A line such as WWW-Authenticate: Basic realm="Restricted" under the 401 status tells you the area is protected by Basic authentication.

A Basic Authentication Example in Nginx

The htpasswd tool is in the apache2-utils package on Debian and Ubuntu and in httpd-tools on AlmaLinux and Rocky Linux. Create the file with the first user; -c creates a new file and overwrites an existing one, so leave it out when you add more users:

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

Then protect a location:

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

Run sudo nginx -t, reload, and test with curl -I -u deploy https://example.com/admin/, which asks for the password. Serve the area over HTTPS only: Basic authentication sends the password encoded, not encrypted.

403 Forbidden

A server sends 403 when it knows enough to refuse: the user is logged in but lacks the role, the client address is blocked, or the web server cannot read the file. Sending the same credentials again does not change the answer, because the rule or the permission is the problem; an account with the right role, or a corrected rule, does.

Nginx Deny Rules

location /internal/ {
    allow 203.0.113.0/24;
    deny all;
}

Requests from outside the allowed range get 403. Rules are checked in order and the first match wins.

File Permissions

When Nginx cannot read a file or enter a folder above it, it answers 403 and logs "Permission denied" in its error log. The fix is the right owner and modes, never 777; see 403 Forbidden in Nginx for the full checklist, including the SELinux label on AlmaLinux and Rocky Linux.

When 404 Is Used Instead

A server may answer 404 Not Found for a resource that exists but is off limits, so that outsiders cannot even learn it is there; RFC 9110 allows this. GitHub's API does this for private repositories when a request is not properly authenticated, and many sites do it for admin paths and hidden files. In Nginx, return 404; in a location gives that effect. Use 401 or 403 when users need to know they should log in or ask for access, and 404 when hiding the resource matters more. For pages that are really missing, see 404 Not Found.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations