Nginx Basic Auth not working

Posted March 26, 2020 3.6k views

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;

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.

Submit an Answer
1 answer

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.