How we can set up HTTP Authentication with Nginx on Ubuntu
How we can set up HTTP Authentication with Nginx on Ubuntu
Nginx is a server that is HTTP and reverse proxy server which is very light in weight as compare to Apache. When we need HTTP authentication to secure our site admin login then we need to setup HTTP Authentication with our server. So,Please watch the given below steps that define how we can setup HTTP Authentication with Nginx on Ubuntu Server.
1.Step to setup HTTP Authentication with Nginx
-Install Apache Utils
First step is to install Apache Utils which we require for generate encrypted password for the user authentication as basic .Install apache 20-utils and use the following command:-
solutionclub:~# apt-get install apache2-utils
Then we need to create user name and password
You create a .htpasswd file that will be under your web root directory and is being serverd by Nginx virtual host.Command for it are given followed to use
htpasswd file:-
solutionclub:~# htpasswd -c /home/solutionclub/public_html/solutionclub.in/.htpasswd solutionclub
Create New password :
You Re-type New Password :
Add password for the user solutionclub
Please check the file htpasswd that will appear as given below :-
solutionclub:~# cat /home/solutionclub/public_html/solutionclub.in/.htpasswd
solutionclub:encryptedpassword
Nginx Configuration
server { listen portnumber; server_name ip_address; location / { root /home/solutionclub/public_html/solutionclub.in/; index index.html index.htm; auth_basic "Restricted"; #For Basic Auth auth_basic_user_file /home/solutionclub/public_html/solutionclub.in/.htpasswd; #For Basic Auth } }
The above given code is normal and we have not php file. When you have the php file and using fastcgi as backend proxy then we need to configure vhost in an other way to secure any page.
server { listen 80; server_name solutionclub.in; root /home/solutionclub/public_html/solutionclub.in; index index.php index.html index.htm; ######### Whatever code configured in your nginx vhost###### ###### line of code ####### # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ .php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php7.1-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~* /wp-login.php { auth_basic "Restricted Area"; auth_basic_user_file /home/solutionclub/public_html/solutionclub.in/.htpasswd; try_files $uri =404; fastcgi_pass unix:/var/run/php7.1-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
The given above line of the code is secure WordPress admin login page with the help of pre-created user in .htpasswd.
Note:- We should use php scripts code to define location to the PHP file for the security and after when you login successfully then your file will download as a bin file and do not excuted as PHP script
Reload Nginx
Relaod/Restart nginx server to have the effect of your configuration.
solutionclub:~# systemctl reload nginx
Now the time to check that if your site/selected page is fully secure through http authentication.
To learn more about HTTP Authentication please click here basic authenticationfor the basic authentication
Well Congratulation you have done all !!!