Nginx Basic Auth not working

  • Posted March 26, 2020
  • Nginx

I’m trying to add basic auth for wp-login but it is not working

Here is the code I have added to /etc/nginx/sites-available/default file

        location ^~ /wp-login.php {
          auth_basic  "Admin Login";
          auth_basic_user_file /var/www/html/.htpasswd;
          fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
          fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
          location /wp-admin/admin-ajax.php {
           allow all;

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi @bepari662,

I’m not sure if the location is actually correct. Can you try using :

 location ~ ^/(wp-admin|wp-login\.php)

instead of

 location ^~ /wp-login.php

The reason behind the non working basic auth is most probably as it doesn’t actually gets to the specified location.

Regards, KDSys