# Fix Nginx try_files Configuration
You're configuring Nginx to serve static files or a single-page application, but try_files isn't working as expected. Files exist on disk, but Nginx returns 404 errors or serves the wrong content.
The try_files directive checks for file existence in order and serves the first match. Understanding its behavior is critical for proper static file serving and SPA routing.
Introduction
You are configuring Nginx to serve static files or a single-page application, but try_files is not working as expected. Files exist on disk, but Nginx returns 404 errors or serves the wrong content. The try_files directive checks for file existence in order and serves the first match. Understanding its behavior is critical for proper static file serving and SPA routing.
Symptoms
Nginx try_files issues present with: - 404 errors despite files existing on disk - Incorrect file being served - SPA routes not working (falling back incorrectly) - $request_filename vs $uri confusion - Nested location blocks not working - Index files not being found
Understanding try_files syntax and variables:
The basic syntax:
try_files file1 file2 ... last_argument;Each argument is checked in order. The last argument can be:
- A URI (internal redirect) - starts with /
- A named location - starts with @
- A file path - returns 404 if not found
Common Causes
- Configuration misconfiguration
- Missing or incorrect credentials
- Network connectivity issues
- Version compatibility problems
- Resource exhaustion or limits
- Permission or access denied
Step-by-Step Fix
- 1.Check logs for specific error messages
- 2.Verify configuration settings
- 3.Test network connectivity
- 4.Review recent changes
- 5.Apply corrective action
- 6.Verify the fix
Common Issues and Solutions
Issue 1: try_files with $request_filename
The $request_filename variable contains the full path to the file corresponding to the request URI.
```nginx # For request: http://example.com/images/logo.png # $request_filename = /var/www/html/images/logo.png # $uri = /images/logo.png
location / { root /var/www/html; try_files $request_filename $uri/ /index.html; } ```
- 1.This checks:
- 2.The exact file path
/var/www/html/images/logo.png - 3.The directory
/var/www/html/images/logo.png/ - 4.Falls back to
/index.html
Issue 2: Serving Single-Page Applications
For SPAs (React, Vue, Angular), all routes should serve index.html:
location / {
root /var/www/html;
try_files $uri $uri/ /index.html;
}Common mistake - using $request_filename incorrectly:
```nginx # WRONG - $request_filename is the full path, not a URI try_files $request_filename /index.html;
# CORRECT - $uri is the request URI try_files $uri $uri/ /index.html; ```
Issue 3: 404 Errors Despite File Existing
Check the root directive matches your file structure:
```nginx server { listen 80; server_name example.com;
# Make sure root points to the directory containing your files root /var/www/html;
location / { try_files $uri $uri/ =404; } } ```
Debug by checking what Nginx is looking for:
location / {
root /var/www/html;
# Log what file Nginx is trying to serve
add_header X-File-Path $request_filename always;
try_files $uri $uri/ =404;
}Check the response headers:
curl -I http://example.com/style.css
# Look for X-File-Path headerIssue 4: try_files in location Blocks
try_files must be in a location context, not server context:
```nginx # WRONG - try_files in server context server { listen 80; try_files $uri $uri/ =404; # Error! }
# CORRECT - try_files in location context server { listen 80; location / { try_files $uri $uri/ =404; } } ```
Issue 5: Named Location Fallback
Use named locations for complex fallback logic:
```nginx location / { root /var/www/html; try_files $uri $uri/ @fallback; }
location @fallback { proxy_pass http://backend; } ```
Issue 6: Multiple try_files Directives
You cannot have multiple try_files in the same location:
```nginx # WRONG - multiple try_files location / { try_files $uri =404; try_files $uri/ =404; # Error! }
# CORRECT - single try_files with multiple checks location / { try_files $uri $uri/ =404; } ```
Issue 7: try_files with alias
When using alias, the path construction is different:
```nginx # With alias, $request_filename = alias_path + $uri (without location prefix) location /static/ { alias /var/www/static/; try_files $uri =404; # WRONG - $uri includes /static/ }
# CORRECT - use $request_filename or adjust the path location /static/ { alias /var/www/static/; try_files $request_filename =404; }
# Or use internal path location /static/ { alias /var/www/static/; try_files $uri $uri/ =404; # For /static/style.css, this checks /var/www/static/style.css } ```
Issue 8: Permission Denied Errors
Even if the file exists, Nginx needs permission to read it:
```bash # Check file permissions ls -la /var/www/html/
# Fix permissions chmod 644 /var/www/html/*.html chmod 755 /var/www/html/
# Check Nginx user ps aux | grep nginx
# Ensure Nginx user owns or can read files chown -R www-data:www-data /var/www/html/ ```
Issue 9: Index File Not Found
For directory requests, ensure index files are configured:
location / {
root /var/www/html;
index index.html index.htm;
try_files $uri $uri/ =404;
}Issue 10: Encoding and Special Characters
URL-encoded characters can cause issues:
```nginx # Use $uri for decoded path location / { try_files $uri $uri/ /index.html; }
# For files with spaces or special characters location /files/ { root /var/www; try_files $request_filename =404; } ```
Verification
Test your configuration:
```bash # Test Nginx configuration nginx -t
# Reload Nginx nginx -s reload
# Test specific file curl -I http://localhost/style.css
# Test directory curl -I http://localhost/images/
# Test SPA route curl -I http://localhost/dashboard ```
Enable debug logging for troubleshooting:
error_log /var/log/nginx/error.log debug;Check error logs:
tail -f /var/log/nginx/error.logComplete Working Examples
Static File Server
```nginx server { listen 80; server_name example.com; root /var/www/html; index index.html;
location / { try_files $uri $uri/ =404; }
# Cache static assets location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ { expires 1y; add_header Cache-Control "public, immutable"; try_files $uri =404; } } ```
Single-Page Application
```nginx server { listen 80; server_name example.com; root /var/www/html; index index.html;
location / { try_files $uri $uri/ /index.html; }
# Don't cache index.html location = /index.html { add_header Cache-Control "no-cache"; } } ```
API Backend with Static Files
```nginx server { listen 80; server_name example.com; root /var/www/html;
# Serve static files location /static/ { alias /var/www/static/; try_files $uri =404; }
# Proxy API requests location /api/ { proxy_pass http://backend:3000; }
# SPA fallback location / { try_files $uri $uri/ /index.html; } } ```
Related Articles
- [Nginx troubleshooting: Fix Lambda Permission Denied - Complete ](fix-lambda-permission-denied)
- [Nginx web server troubleshooting: Fix Client Max Body Size Large Upload Nginx Issue ](client-max-body-size-large-upload-nginx)
- [Fix Apache 502 Proxy Error](fix-apache-502-proxy-error)
- [Fix Apache LogLevel Core Debug Configuration](fix-apache-loglevel-core-debug)
- [Fix Cloudflare 502 Bad Gateway Error](fix-cloudflare-502-bad-gateway)
<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "Fix Nginx try_files Configuration", "description": "Step-by-step guide to fix Nginx try_files issues. Learn correct syntax, $request_filename vs $uri, and resolve 404 errors in static file serving and SPA deployments.", "url": "https://www.fixwikihub.com/fix-nginx-try-files-configuration", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2026-04-27T10:01:00.000Z", "dateModified": "2026-04-27T10:01:00.000Z" } </script>