Web / NGINX Interview questions
Why do we use try_files with a fallback to PHP-FPM in WordPress-style setups?
WordPress and similar PHP applications rely on a single front controller - typically index.php - to handle routing for URLs that don't correspond to a real file on disk, like /blog/my-post/.
location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { fastcgi_pass unix:/run/php/php-fpm.sock; fastcgi_index index.php; include fastcgi_params; }
try_files first checks whether the requested URI matches a real file, then a real directory, and only if neither exists falls back to passing the request to index.php with the original query string preserved. This lets NGINX serve actual static assets - images, CSS, uploaded media - directly and efficiently, while routing everything else through PHP-FPM, where WordPress's own router resolves the "pretty" URL internally.
Without this fallback, any URL that isn't a literal file path - which is most of WordPress's permalink structure - would return a 404 straight from NGINX before PHP ever got a chance to handle it.
More Related questions...