Harden WordPress on Nginx: Server Block Rules That Matter

By Information Security Asia · Updated December 3, 2026 · 6 min read

Harden WordPress on Nginx: Server Block Rules That Matter

Short answer: To harden WordPress on Nginx, put security rules directly in the site’s server block, because Nginx does not read .htaccess files. The rules that matter most are: deny PHP execution inside wp-content/uploads, deny access to hidden files such as .git and .env and to backup copies of wp-config.php, route requests with try_files so missing PHP files never reach PHP-FPM, rate-limit wp-login.php with limit_req, block or restrict xmlrpc.php if you do not need it, and hide the Nginx version with server_tokens off. Set cgi.fix_pathinfo to 0 in php.ini, run PHP-FPM as a user that cannot write to core files, and test every rule by requesting a blocked URL after each reload. One typo in a location block can silently reopen a hole.

Why Nginx needs its own hardening

Most WordPress security tutorials assume Apache and rely on .htaccess rules that plugins write automatically. On Nginx those files are ignored. A security plugin may report that it has protected wp-config.php or blocked PHP in uploads while the server happily serves those requests. If you moved from shared hosting to a VPS, check this first. Our Apache hardening guide covers the equivalent rules for Apache.

Harden WordPress on Nginx: Server Block Rules That Matter

The WordPress developer documentation for Nginx provides a reference configuration and a pointed warning: a typo in your restrictions can create loopholes, so test them by uploading a PHP file to the uploads directory and trying to run it.

The rules at a glance

Rule What it stops Risk if missing
Deny PHP in uploads Running a web shell uploaded through a vulnerable plugin Full site takeover
Deny hidden files Reading .git, .env, .htaccess leftovers Source code and secrets leak
Deny backup and config copies Downloading wp-config.php.bak or .sql dumps Database password leak
try_files and 404 for missing PHP Path-info tricks that run non-PHP files as PHP Code execution
limit_req on wp-login.php Password guessing at high speed Account compromise, server load
Restrict xmlrpc.php Amplified login attempts and pingback abuse Brute force, DDoS participation
server_tokens off Advertising the exact Nginx version Easier targeting

1. Block PHP execution in uploads

The uploads folder must be writable by WordPress, which makes it the favorite landing spot for malicious PHP files. Images, PDFs and videos never need to execute, so tell Nginx to refuse any PHP request there. Place this before your general PHP location so it matches first:

location ~* /(?:uploads|files)/.*\.php$ {
    deny all;
}

The WordPress documentation notes that this pattern also covers subdirectories and multisite file paths. Test it by placing a harmless test.php in uploads and requesting it in a browser; you should get a 403 error, never output.

2. Deny hidden files and leftovers

Hidden files often hold secrets: a .git directory exposes your entire source history, and .env files may contain API keys. Block them all, with an exception for the ACME challenge path if you use Let’s Encrypt:

location ^~ /.well-known/acme-challenge/ {
    allow all;
}
location ~ /\. {
    deny all;
}

Then block common backup and dump extensions that editors and careless migrations leave in the web root:

location ~* \.(?:bak|old|orig|save|swp|sql|tar|gz|zip|log)$ {
    deny all;
}
location ~* wp-config {
    deny all;
}

If your site legitimately offers ZIP or GZ downloads, for example from a downloads plugin, remove those extensions from the list or scope the rule to the web root only. A copy named wp-config.php.bak is served as plain text by default, which hands out your database password. Our wp-config.php hardening guide explains why this backup-copy leak is so common.

3. Route requests safely to PHP

Use the standard WordPress routing so static files are served directly and everything else goes to index.php. In the PHP location, return 404 when the requested script does not exist, which blocks a family of path-info tricks:

location / {
    try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/run/php/php-fpm.sock;
}

Adjust the socket path to match your PHP-FPM pool. The WordPress documentation also recommends setting cgi.fix_pathinfo to 0 in php.ini so PHP does not guess at script names.

Harden WordPress on Nginx: Server Block Rules That Matter

4. Rate-limit the login page

Nginx’s limit_req module can slow password guessing to a crawl without a plugin. Define a zone in the http block, then apply it only to wp-login.php:

limit_req_zone $binary_remote_addr zone=wplogin:10m rate=5r/m;

location = /wp-login.php {
    limit_req zone=wplogin burst=5 nodelay;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/run/php/php-fpm.sock;
}

Because this exact-match location bypasses your general PHP block, it must include the FastCGI lines itself. If your site sits behind Cloudflare or another proxy, configure the real_ip module first, otherwise every visitor shares the proxy’s address and you will rate-limit everyone together. Our guide to stopping WordPress brute force attacks shows how to confirm an attack in the logs before you tune these numbers.

5. Decide what to do with xmlrpc.php

XML-RPC lets old apps and some plugins talk to WordPress, but attackers use it to try many passwords in a single request. If nothing on your site needs it, block it at the server, which is cheaper than letting PHP handle the request:

location = /xmlrpc.php {
    deny all;
}

If you use Jetpack or the mobile app, restrict it to the services that need it instead. Our XML-RPC guide lists who still depends on it and how to test that it is really off.

6. Hide versions and add headers

Add server_tokens off in the http block so error pages and headers stop advertising the exact Nginx version. Then send security headers such as Strict-Transport-Security, X-Content-Type-Options and a Content-Security-Policy. Remember the Nginx add_header trap: a location that sets any header discards headers inherited from the server block. Our security headers guide has tested values.

7. Lock down the filesystem and PHP-FPM

Server rules only go so far if PHP can rewrite everything. Run each site’s PHP-FPM pool as its own user, keep core files owned by a different user where your update workflow allows, and give the PHP user write access only to wp-content/uploads and any cache directories. Disable the built-in theme and plugin editor in wp-config.php with DISALLOW_FILE_EDIT. Keep Nginx, PHP and WordPress updated, and remove the default server block that answers requests for unknown hostnames.

Testing checklist

  1. Run nginx -t before every reload; never reload a failing config.
  2. Request a test PHP file in uploads: expect 403.
  3. Request /.git/config and /.env: expect 403 or 404.
  4. Request /wp-config.php.bak after creating an empty test file: expect 403.
  5. Request /xmlrpc.php if blocked: expect 403.
  6. Log in normally to confirm the rate limit does not lock out real users.
  7. Check response headers with your browser’s developer tools.

Delete your test files when you finish.

Frequently asked questions

Do security plugins work on Nginx?

Their PHP-level features, such as login limits and scanning, work. Features that write .htaccess rules do nothing on Nginx, so add the equivalent rules to the server block yourself.

Where do these rules go?

Inside the site’s server block, typically in a file under sites-available or conf.d. limit_req_zone and server_tokens belong in the http block.

Will blocking PHP in uploads break plugins?

Well-written plugins do not execute PHP from uploads. If something breaks, treat it as a warning sign about that plugin.

Is Nginx more secure than Apache for WordPress?

Neither is secure by default. Both are safe when configured carefully, and both are dangerous when rules are missing.

Sources

Keep reading