Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

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!
Does not prompt for password if I specify a specific php file. For instance… somesite.com/somefile.php
It does prompt for password if I specify the root of the server or any subdirectory. For instance… somesite.com Or somesite.com/somedirectory
If I use this method and type in a wrong password once, it will always show me the forbidden page instead of asking again. Is there a solution for that?
It’s working but a giving a prompt to download the wp-login.php page or else downloading the login page automatically. spend hrs figuring out suitable way to protect login page. Also is it possible to change the name of wp-login.php page to any other name in lemp config’n.
I got this working but now I think it would be cool to have a nicely designed login page for my site rather than the default browser dialogue. Is there a way to change the username and password prompt from a browser dialogue to a web form?
You can generate a password without a prompt by piping text into openssl and passing a new flag. For example:
echo "password" | openssl passwd -apr1 -stdin
This will echo to stdout. This way you can write a script or something instead of having to use the prompt to type in the password.
If you still wanted to append the output to the /etc/nginx/.htpasswd file, then you would do the following:
echo "password" | openssl passwd -apr1 -stdin >> /etc/nginx/.htpasswd
As others have mentioned, the config (if done incorrectly) can lead to php files being accessible after trying to protect a directory. This is due to the way nginx config prioritises each part of the config.
As most servers have a single php section within their server block, when the request comes in for a php file nginx uses that config, skipping the folder / directory protection section, and serves the resulting html. Bad times.
The correct way to do this is to include a copy of the php section within your password protected section, as follows:
location /admin/ {
auth_basic "Admin-Section";
auth_basic_user_file /myfolder/.htpasswd;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Make a copy of the php section i.e. don’t delete the block from your server config, else you will lose php from the rest of your site.
Credit: http://stackoverflow.com/users/1476414/fisharebest Original Answer: http://stackoverflow.com/questions/4697010/nginx-auth-basic-and-php
You could reduce this article content by 95%. I don’t know why there is a need to explain this for each platform where it isn’t specific to a platform but rather having openssl and nginx installed
Step 1:
Method 1: openssl passwd -apr1
Method 2: htpasswd -c /etc/nginx/.htpasswd sammy
Step 2:
Put it in your nginx file, use the auth_basic directive into the location block.
i am having a small problem.
i added a password before using openssl, but i forgot it. i know i could add a new user, but i want to edit password for my old user or at least delete old one and create a new one.
also there is one password without any user in my htpasswd file
username@host:~$ cat /etc/nginx/.htpasswd
user$apr1$146S4Nar$Rm8HTS/13VbjMI61ovBnl1
$apr1$sbP3SD30$roRPzaPfYA8B.nbnEce.x0
how can i delete that password without user.
another issue is, i am trying to protect adminer.php. but unfortunately, when i protect that file using http authentication, after giving the password im prompted to download the file. how can i work around this?
this is my server block
location = /adminer.php {
try_files $uri $uri/ =404;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}
This guide needs updating; You need to chown the .htpasswd file to www-data, otherwise it cannot read it
chown www-data:www-data /etc/nginx/.htpasswd