Protect WordPress login with Nginx Basic Auth without breaking AJAX

HTTP Basic Authentication can add a separate password prompt before the WordPress login page. It can reduce direct automated login attempts, but it does not remove malware or fix vulnerable plugins. Use HTTPS, maintain the application and enable a suitable second factor for administrator accounts.

Reviewed: 27 October 2026. This replaces the old broad Nginx rule. Protecting every URL beginning with /wp-admin without reviewing public endpoints can break forms and other front-end features.

Decide what needs protection

For a small site where only staff log in, protecting /wp-login.php may be useful. A shop or membership site has customer login, password recovery and integrations to consider. A separate prompt may be unsuitable for those users. Basic Auth is an additional password layer, not WordPress two-factor authentication.

Keep a copy of the active virtual host and an existing administrative session before making changes. If Nginx is a reverse proxy, inspect both the public proxy and the application server: a rule at one layer must not accidentally replace the working PHP or proxy handler.

Create the password file safely

The Apache htpasswd utility can maintain a file for Nginx without installing an Apache web server. Keep it outside the document root. For a new dedicated file, use an interactive password prompt:

Use -c only for first creation: it overwrites an existing file. To update or add an account, omit it:

Do not put the password in shell arguments or publish the hash. Set ownership and permissions for the actual Nginx worker account so it can read the file; do not assume that all distributions use the same account. Verify bcrypt support on the target system.

Add authentication to the existing handler

These are the two relevant directives. They are a fragment for the reviewed handler, not a complete virtual host or PHP location:

For a login-only design, the intended URL match is location = /wp-login.php. An exact location must also contain the correct existing FastCGI or proxy handling. Do not paste an otherwise empty location: it can change PHP execution or file serving. Subdirectory installations require their real path.

If you also protect the admin area, account for /wp-admin/admin-ajax.php and any public admin-post.php actions. WordPress plugins may use these for visitors who are not logged in. A reviewed exception can use auth_basic off; with the correct handler, while WordPress still performs its own authorization. Avoid broad exceptions or a global server-level gate on a public shop.

Test before and after reload

Check the login page in a private browser window: the gate should reject unauthenticated requests and permit valid credentials through to WordPress. Then test administrator login/logout, password reset, public pages, search, AJAX and contact forms. For a shop, include customer login and checkout on a test environment without real charges. Do not assume every protected URL should return 401.

If behavior breaks, restore only the changed configuration, run nginx -t and reload again. Do not weaken unrelated controls to make a form work.

When the site is already compromised

A new login prompt is insufficient. Preserve evidence, review the scope, recover trusted files and address the entry point before returning the site to normal service. For help with access rules or a recurring infection, contact Soban with the symptoms and recent changes; never send passwords in the initial message.

Sources