Configuring WordPress Permalinks with Nginx: A Complete Guide to Pretty URLs
This article explains how to configure WordPress pretty permalinks (often called pseudo-static URLs) on Nginx. It covers what pseudo-static routing actually does, how to verify you are editing the correct configuration file, the standard `try_files` rule, common conflicts with caching plugins and reverse proxies, subdirectory installs, security hardening, and a step-by-step checklist for validation and troubleshooting.
1. What “Pretty Permalinks” Actually Do
Many developers and system administrators run into the same problem when setting up WordPress: after changing the permalink structure in the admin dashboard to “Post name” or “Numeric”, the homepage loads fine, but clicking any post or category page returns a 404. The cause is not WordPress itself, but the fact that the web server does not know which handler should process these paths that look like static files.
The essence of pretty permalinks is reassigning a request: when the server receives a request like /archives/128.html, it first tries to treat it as a real file or directory. If it does not exist, the rules hand the request off to a single entry script (usually index.php), passing the original path through as a parameter so the application layer can route it to the content that actually needs to be rendered. So pretty permalinks are not about “turning dynamic pages into static files”, but about “making dynamic routes masquerade as static paths”, while gaining a friendlier URL structure and better crawlability.
In Nginx, there are two main types of directives for this:
try_files: probes for a real resource from a candidate list in order, falling back to a specified target when nothing matches. The logic is clear with no extra conditional checks, and it is the currently recommended approach.rewrite: rewrites and forwards the URI based on regular expressions, suitable for complex scenarios with fixed path formats that need precise parameter extraction.
The difference between the two is not just stylistic; it directly affects performance and maintainability. The official recommendation is to avoid using if inside location for file-existence checks, because the behavior of if during configuration parsing can easily diverge from expectations and interrupts Nginx’s directive processing flow.
2. Prerequisites: Edit the Right File, Point to the Right Directory, Make It Take Effect
1. Confirm Which Config File You Are Editing
A single server often has multiple configuration directories, such as the site directory shipped with the OS, the virtual host directory of a compiled installation, or the website settings generated by a graphical control panel. The way to tell is not memory but running nginx -T, which prints the main configuration and every included sub-configuration in full. Search for your domain to locate the file that is actually in effect.
2. The Site Root Must Point to the WordPress Directory
If you create the site through a control panel, the root is usually already pointed to the correct location. For manual configuration, confirm that root points to the level containing index.php and wp-config.php, not a subdirectory of it. When the root is wrong, the typical symptom is that the homepage opens (because a default index file exists) while all inner pages return 404.
3. Check and Reload
Every change must be syntax-checked before reloading, so that a single bad configuration does not take the whole site down:
nginx -tnginx -s reload
For containerized deployments, also confirm that the configuration takes effect via a read-only mount or a copy inside the image, and that the reload runs inside the container’s main process.
3. The Standard WordPress Pretty Permalink Rule
WordPress uses a single-entry model: every request that does not match a real file should go to index.php, with the query string passed through unchanged. The simplest and most reliable form is:
location / {try_files $uri $uri/ /index.php?$args;}
The three candidate values each have a specific meaning:
$uri: first matched as a real file; on a hit it is returned directly, so static asset performance is unaffected;$uri/: then matched as a directory, used for paths like/category/;/index.php?$args: the fallback to the entry script, where$argspreserves the original query parameters so pagination, search, and filtering do not lose parameters.
A special note: do not mix or duplicate /index.php?$query_string with $args, and do not write /index.php and drop the parameters, otherwise requests like /page/2 or ?s=keyword will return abnormal content.
The permalink settings in the admin dashboard must work with the rule: enable a format such as “Post name” in the settings. The hint about writing to .htaccess can be ignored, because that file only applies to Apache.
4. Common Conflicts and Advanced Scenarios
1. Conflict with a Caching Plugin’s location /
After enabling a page-cache plugin, the plugin often asks you to define a custom location / with cache-hit checks. If you then add a separate WordPress fallback rule, you get duplicate definitions or one rule overriding the other. The correct approach is to keep only one location /, merge the cache check and the try_files fallback into the same block, and make sure that a cache miss still falls back to the entry script.
2. Execution Permissions for Static Assets and PHP
Pretty permalinks only handle the handoff, not the parsing. There must be a PHP processing block that passes .php requests to the application server (usually a local address or socket listening with the FastCGI protocol) and sets SCRIPT_FILENAME to the real script path. Without this block, requesting the entry script returns a downloaded file or a blank page.
3. Behind a Reverse Proxy or Load Balancer
If the site sits behind a proxy, the application layer may receive the proxy address instead of the real client address. You need to pass the real source information in the proxy block and configure the backend to parse it accordingly; otherwise admin login, rate limiting, and analytics will all be distorted. Also, the proxy layer and the backend should not each maintain their own set of rewrite rules; keep rewriting only in the layer closest to the application.
4. Subdirectory Installation
When WordPress is installed in a subdirectory such as /blog/, the rule must be written in the location for that subdirectory, and the fallback target must include the subdirectory prefix:
location /blog/ {try_files $uri $uri/ /blog/index.php?$args;}
5. Security Hardening Recommendations
- Block direct execution of scripts in the upload directory to prevent upload points from being exploited;
- Deny access to sensitive files (configuration files, version control directories, backup files);
- Add request rate limiting for the admin and login endpoints as needed;
- Enable logging of slow requests and 404s to make misconfigured rules easier to spot.
5. Verification and Troubleshooting Checklist
After configuring, verify in the following order to narrow down the problem quickly:
- Visit the homepage to confirm the site root is correct;
- Visit a post with a pretty permalink format and check whether it 404s;
- If it 404s, look at the “file not found” path recorded in the site error log to determine whether the root is wrong or the rule did not match;
- If PHP source code or a download is returned, the script parsing block is missing;
- If the permalink settings cannot be saved, check directory write permissions and plugin caching;
- If only pagination misbehaves, focus on whether the query parameters are being dropped.
For cloud deployments, you can also use log services and monitoring alerts to set thresholds for the 404 ratio and the 5xx ratio, catching configuration mismatches before users complain. Overall, the pretty permalink configuration for WordPress is only a few lines; what really determines success is a complete understanding of these four areas: file-probing order, parameter pass-through, script parsing, and proxy layers.
