WordPress routing and static assets
Trace the front controller
WordPress is a PHP application. Nginx handles the public HTTP connection and can deliver files itself; PHP-FPM runs the selected script; the application talks to the database. A permalink such as /news/ need not correspond to a directory. The front controller receives that request and decides whether it represents a post, an archive or a missing resource. This is different from redirecting the browser: the URL stays /news/. Our lab is an original miniature PHP app, not an installed WordPress instance.
Browser → Nginx existing asset → file response virtual page → index.php → PHP-FPM → application data application missing page → HTTP 404Route paths without losing arguments
In a conventional WordPress installation a general location checks existing files and directories, then internally routes other requests to the front controller. $args carries current query arguments; $request_uri preserves the original request including its query. Real directories can trigger index handling, so check both / and directory URLs. The fragment below assumes a separately configured PHP location and index handling; it does not install WordPress. Nginx never interprets Apache .htaccess files. Translate the intent of rewrite and access rules into reviewed Nginx locations, not literal Apache syntax.
index index.php;location / { try_files $uri $uri/ /index.php?$args;}Limit executable scripts
For the minimal fixture, only /index.php may execute; PHP-FPM receives a fixed script filename within the deployed document root. Reject other PHP URLs, including missing files, rather than passing arbitrary paths. In a real WordPress installation, selected core entry points such as login, cron and administration need their own verified routing policy; the fixture deliberately omits them. Do not copy this restricted policy into production unchanged. Keep application code deployment-owned and allow writes only to specific application data directories. Nginx and PHP-FPM must agree on filesystem paths.
location = /index.php { try_files $uri =404; include fcgi-params.conf; fastcgi_pass 127.0.0.1:19450;}location ~* \.php(?:/|$) { return 404; }Keep uploads as data
Uploaded data is not trusted application code. The fixture restricts the upload prefix to a flat list of known media names with allowed extensions; it rejects everything else, including PHP files and dotfiles. ^~ prevents top-level regex locations from taking over this prefix, while its nested regex locations select the allowed files. In production, validate actual content and filenames, control SVG/HTML and active content, and consider a separate media origin. Extension filtering is not complete upload security. Test an existing asset, a missing asset, a forbidden file, and a virtual route; do not make a missing asset look like a successful page.
location ^~ /wp-content/uploads/ { location ~ ^/wp-content/uploads/[a-zA-Z0-9_-]+\.(?:png|css)$ { try_files $uri =404; } location ~ . { return 404; } return 404;}Use the accompanying Chapter 47 manual practice and lab. Configuration snippets are excerpts, not complete production WordPress configurations. The complete fixture and expected results are provided separately.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
WordPress routing and static assets
Assess decisions from the lesson and the original lab.
What handles a virtual permalink after an internal redirect?
Why is a fixed script filename useful in this fixture?
What happens to Apache .htaccess rules under Nginx?
What should a missing static upload return in this fixture?
What must be checked before using the lab policy for real WordPress?