If you’ve enabled basic authentication in Nginx and now get a 404 Not Found instead of a password prompt, the problem is almost always a misconfigured location block or a missing auth_basic directive in the right context. This guide walks you through the exact steps to diagnose and fix it, so your protected directory prompts for credentials and loads correctly.
What You’ll Need
- Access to your server’s terminal (SSH) with sudo privileges
- The path to your Nginx configuration file (usually
/etc/nginx/nginx.confor a site file in/etc/nginx/sites-available/) - A text editor like
nanoorvim - Your password file (e.g.,
/etc/nginx/.htpasswd) already created withhtpasswdor similar
Step-by-Step
- Identify the configuration file – List your enabled sites with
ls /etc/nginx/sites-enabled/. Open the relevant file, oftendefaultor your domain’s config, withsudo nano /etc/nginx/sites-enabled/your-site. - Check where the 404 originates – A 404 with basic auth usually means Nginx is not even processing the
auth_basicdirective; it’s falling through to a default location that doesn’t exist. Look for anylocation /orlocation /pathblocks that might be overriding yours. - Place auth directives correctly – The
auth_basicandauth_basic_user_filemust be inside thelocationblock that handles the URL you’re protecting. For example, to protect the whole site, put them insidelocation / { ... }. For a subdirectory, uselocation /admin { ... }. - Verify the password file path – Ensure the
auth_basic_user_filepoints to an existing file. Test withsudo ls -l /etc/nginx/.htpasswd. If the file is missing, create it withsudo htpasswd -c /etc/nginx/.htpasswd username(installapache2-utilsif needed). - Check for conflicting location blocks – Nginx uses the most specific prefix match. If you have
location /adminand alsolocation ~ \.php$that matches, the PHP block might take precedence, bypassing auth. Uselocation ^~ /adminto force prefix matching and ignore regex blocks. - Test the configuration – Run
sudo nginx -tto check for syntax errors. If it says “syntax is ok,” reload withsudo systemctl reload nginx. - Clear your browser cache – Sometimes the 404 is cached. Try an incognito window or hard refresh (Ctrl+Shift+R).
- Check file permissions – The password file must be readable by the Nginx worker process (usually user
www-data). Runsudo chown www-data:www-data /etc/nginx/.htpasswdandsudo chmod 640 /etc/nginx/.htpasswd.
Common Mistakes
- Putting auth in the wrong context – Placing
auth_basicinsideserver { }instead oflocation { }can cause it to be ignored or applied unexpectedly. - Using a relative path for the user file – Always use an absolute path like
/etc/nginx/.htpasswd. Relative paths are interpreted relative to the Nginx prefix, not your current directory. - Forgetting to reload after changes – Editing the config without reloading means the old settings remain active.
- Password file with wrong format – The file must contain lines like
username:hashedpassword. If you used plain text, Nginx will fail to authenticate, but that usually gives a 500, not 404. - Location block with a trailing slash mismatch – If you protect
location /adminbut the URL is/admin/, Nginx may treat them differently. Uselocation /admin/orlocation ^~ /admin.
Tips
- Use
location ^~ /protectedto make Nginx skip regex checks for that path, avoiding conflicts with PHP or other handlers. - If you want to protect only certain files, use a regex like
location ~ ^/files/.*\.pdf$and put auth inside. - Test with
curl -I http://yourdomain.com/protected– you should see401 Authorization Requiredinstead of 404. - Consider using
auth_basic "Restricted Area";to set a custom message in the login prompt. - Keep the password file outside the web root (e.g.,
/etc/nginx/) to prevent it being downloaded.
Troubleshooting
If you still get a 404 after following the steps:
- Check the error log – Run
sudo tail -f /var/log/nginx/error.logand try accessing the URL. Look for “directory index of … is forbidden” or “open() … failed (2: No such file or directory)”. - Verify the document root – The 404 might be because the actual file or directory doesn’t exist. Ensure the protected path has an index file (like
index.html) or a valid script. - Try a minimal test – Create a temporary
location /test { auth_basic ...; root /var/www/html; }and see if that prompts. If it does, the issue is in your original location. - Check for try_files – If your location has
try_files $uri $uri/ =404, it might be returning 404 before auth is evaluated. Remove or adjust it. - Look for duplicate server blocks – A second server block for the same domain might be catching the request. Use
sudo nginx -Tto see all loaded configs.
Frequently Asked Questions
Why does basic auth work on one path but not another?
Because each location block is independent. You must add auth_basic and auth_basic_user_file to every location you want protected, or use a location / block and then include auth in nested blocks.
Can I protect the entire site except one file?
Yes. Put auth in location /, then add a more specific location for the public file (e.g., location = /public.html { auth_basic off; }). The exact match takes precedence.
What if I get a 500 Internal Server Error instead?
That usually means the password file is unreadable or malformed. Check permissions and that each line is in the correct user:hash format. Use htpasswd to regenerate.
How do I create the password file on Ubuntu?
Install apache2-utils with sudo apt install apache2-utils, then run sudo htpasswd -c /etc/nginx/.htpasswd username. For additional users, omit the -c flag.
Does the order of directives matter?
No, within a block the order doesn’t matter, but the location matching order does. Prefix locations are checked first, then regex in order of appearance. Use ^~ to skip regex.
Why does the 404 appear only after entering credentials?
That means auth succeeded, but the requested file doesn’t exist. Check your root and index directives inside that location. The path is relative to the root, not the server’s filesystem root.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It