Nginx Basic Auth not working

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

This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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