Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Nginx Location Block 404: How to Fix It

· 5 min read · Home Tech

In this article
  1. Understanding Nginx Location Blocks
  2. Common Causes of 404 Errors in Location Blocks
  3. Step-by-Step Fixes
  4. Common Misconceptions: root vs alias
  5. When to Use try_files and proxy_pass
  6. Frequently Asked Questions
  7. Conclusion

If your Nginx location block is returning a 404 error, the fix usually involves correcting the root or alias directive, ensuring the file path resolves correctly, or adjusting the try_files directive. This article explains the common causes and provides step-by-step solutions to resolve the issue.

Understanding Nginx Location Blocks

Nginx uses location blocks to define how to handle requests for different URIs. A location block can be nested within a server block and specifies a pattern that matches the request URI. When a request comes in, Nginx checks the location blocks in order and uses the first match (or the most specific match, depending on the modifier) to process the request. The directives inside the location block, such as root, alias, try_files, and proxy_pass, determine what content is served or what action is taken.

Common Causes of 404 Errors in Location Blocks

A 404 error from a location block typically means Nginx cannot find the file or resource that the location is supposed to serve. The causes are often:

  • Incorrect root or alias directive: The path specified does not match the actual file location on the server.
  • Missing try_files directive: For single-page applications or pretty URLs, without try_files, Nginx may try to serve a non-existent file directly.
  • File permissions: Nginx may not have read access to the file or directory.
  • Wrong location modifier: Using = for exact match when you need a prefix match, or vice versa.
  • Symlinks and path resolution: If the file is accessed via a symlink, Nginx may not follow it unless disable_symlinks is set appropriately.

Step-by-Step Fixes

Here are the most effective steps to resolve a 404 in a location block:

  1. Check the root directive: Ensure the root directive points to the directory that contains the file. For example, if your file is at /var/www/html/index.html and your location is /, the root should be /var/www/html.
  2. Verify the file path: Use ls -l to confirm the file exists and that Nginx's user (usually www-data or nginx) has read permission (at least r-- for files and r-x for directories).
  3. Use alias correctly: If you use alias, it replaces the matched part of the URI. For example, location /images/ { alias /data/images/; } means a request to /images/logo.png will look for /data/images/logo.png. Do not include the location path in the alias.
  4. Add a try_files directive: For a location that should serve static files or a fallback, use try_files $uri $uri/ /index.html; to serve the requested file or fall back to a default.
  5. Test the configuration: Run nginx -t to check for syntax errors, then reload with systemctl reload nginx or nginx -s reload.
  6. Check for regex precedence: If you have both prefix and regex locations, Nginx evaluates regex locations after prefix locations unless the prefix uses ^~. Ensure your regex location is not being ignored.

Common Misconceptions: root vs alias

One of the most frequent mistakes is confusing root and alias. The root directive appends the full request URI to the root path. For example, with root /var/www; and a request to /data/info.txt, Nginx looks for /var/www/data/info.txt. The alias directive replaces the matched location part. With location /data/ { alias /files/; }, a request to /data/info.txt looks for /files/info.txt. Using alias with a root style path often leads to 404s.

When to Use try_files and proxy_pass

If your location block is meant to serve a single-page application (like React or Vue), you need try_files to fall back to index.html for client-side routing. For example:

location / {
    root /var/www/app;
    try_files $uri $uri/ /index.html;
}

If you are proxying to a backend server, ensure proxy_pass is set correctly and that the backend is responding. A 404 from Nginx might actually come from the backend if the proxy passes the request incorrectly. Check your backend logs to distinguish.

Frequently Asked Questions

Why does my location block return 404 even though the file exists?

This usually happens because the root or alias path is wrong, or because the file permissions do not allow Nginx to read it. Double-check the absolute path and run ls -l to see the permissions. Also, ensure there is no a trailing slash mismatch between the location and the file path.

What is the difference between root and alias?

root appends the full URI to the specified directory. alias replaces the matched part of the URI with the specified directory. For example, location /static/ { root /var/www; } serves /var/www/static/file.css for a request to /static/file.css. With alias /var/www/files/; the same request would look for /var/www/files/file.css.

How do I fix a 404 for pretty URLs (like /page/about)?

Add a try_files directive to your location block. For example: try_files $uri $uri/ /index.php?$query_string; if you are using PHP, or try_files $uri $uri/ /index.html; for a static SPA. This tells Nginx to fall back to a single entry point when the exact file is not found.

Does the order of location blocks matter?

Yes. Nginx first checks prefix locations (longest match wins), then regex locations in order. If you use ^~ on a prefix location, regex locations are not checked. If you use = for an exact match, it takes precedence over everything. Make sure your most specific location is defined correctly to avoid unexpected 404s.

Can a 404 be caused by a misconfigured index directive?

Yes. If a request ends with a slash (e.g., /directory/), Nginx tries to serve the index file defined by the index directive. If the index file does not exist, Nginx returns 404. Ensure the index directive points to a file that exists in the root or alias directory.

Why does my location block work in one server block but not another?

This often happens because the server block has a different root, different server_name, or the location block is nested inside another location. Check the server block's root and ensure there are no conflicting location blocks. Also verify that the correct server block is being used by checking the server_name and the request's Host header.

Conclusion

Fixing a 404 in an Nginx location block is usually a matter of verifying the file path, permissions, and the directives used. Start by testing your configuration with nginx -t and checking the error log at /var/log/nginx/error.log for detailed messages. With the steps above, you can resolve most 404 issues quickly.