Install Apache-utils
Unlike Apache, Nginx uses HttpAuthBasic module to enable Password Protected Directories but doesn’t provide any tools to create an encrypted .htpasswd file. We need htpasswd to create and generate an encrypted for the user using Basic Authentication. Install apache2-utils using the command below.
sudo apt-get install apache2-utils
Create User and Password
Create a .htpasswd file under your website directory being served by nginx. The following command would create the file and also add the user and an encrypted password to it.
$ sudo mkdir /etc/nginx/passwd
$ sudo htpasswd -c /etc/nginx/passwd/.htpasswd first_user
$ sudo htpasswd /etc/nginx/passwd/.htpasswd second_user
$ sudo htpasswd -c /etc/nginx/passwd/.htpasswd first_user
$ sudo htpasswd /etc/nginx/passwd/.htpasswd second_user
Eg:- htpasswd -c /etc/nginx/passwd/.htpasswd test1
After you have installed Apache create a new directory under /etc/nginx/ named intuitively passwd where .htpasswd file will be stored and use htpasswd command with –c switch on first added user to generate file, then if you want to add more users use htpasswd without –c switch.
For protect name-ip-ssl Virtual Host root /srv/http/ served path with all its subfolders and files beneath it add the following instructions inside your Virtual Host server block under root directive and point it to absolute .htpasswd file path.
auth_basic "Restricted Website";
auth_basic_user_file /etc/nginx/passwd/.htpasswd;
auth_basic_user_file /etc/nginx/passwd/.htpasswd;
Example :-
Here i have already a Virtual Sites running on Port 8080 | etc/nginx/sites-enabled
server {
listen 8080; ## listen for ipv4; this line is default and implied
root /var/www;
location ~ \.php$ {
root /var/www;
auth_basic "Restricted Website";
auth_basic_user_file /etc/nginx/passwd/.htpasswd;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
No comments:
Post a Comment