Detailed Nginx actual combat to let users through the user name password authentication access to the Web site _nginx

Source: Internet
Author: User
Tags rsyslog

Sometimes we have a demand that your site does not want to provide a public access or that some pages do not want to be made public, and we want some specific clients to access it. Then we can ask for authentication when we visit, as if we were to add a lock to your own house to deny the uninvited guests.

Experimental environment:

Now the company opened an internal use of the site for, require all people to view the page information must be entered the default username and password before you can browse the Web page

Using Nginx to build a virtual host for testing.


First, view the current system parameters

[Root@rhel6u3-2 ~]# uname–r//View system kernel version number 
[root@rhel6u3-2 ~]# cat/etc/redhat-release// View System version number 

Second, edit the virtual host configuration file

[Add the following statement to the HTTP field in the root@rhel6u3-7 ~]# vim/usr/local/nginx/conf/nginx.conf//main configuration file to point the virtual host's profile to 
[root@rhel6u3-7 ~]# vim/usr/local/nginx/server/ 
server { 
   listen    80;//listening port is 
   server_name; Virtual host URL 
      root  sites/www1///virtual host Web site root 
      index index.html index.htm;//Virtual Host home page 
      auth_ Basic "secret"; Virtual Host authentication name 
      auth_basic_user_file/usr/local/nginx/passwd.db////Virtual host username password Authentication database 
    location/status { 
        stub_status on;//open website monitoring status 
        access_log/usr/local/nginx/logs/www1_status.log//monitor log 
        Auth_basic " Nginxstatus "; } 

Third, the user name and the corresponding password database file are generated by HTPASSWD command.

[Root@rhel6u3-7 server]# htpasswd-c/usr/local/nginx/passwd.db xiaonuo//Create authentication information, Xiaonuo authenticate username 
New Password: * * * * *//Enter the authentication password 
re-type new password: ********//re-enter the authentication password 
adding password for user xiaonuo 
[root@rhel6u3-7 server]# 
[root@rhel6u3-7 ~]# chmod 400/usr/local/nginx/passwd.db//Modify Web Site Certification database permissions 
[root@rhel6u3-7 ~]# chown Nginx. /USR/LOCAL/NGINX/PASSWD.DB//Modify website authentication database and group 
[root@rhel6u3-7 ~]# cat/usr/local/nginx/passwd.db// You can see that the password generated by HTPASSWD is a cryptographic format 

Four, smooth restart Nginx service

[Root@rhel6u3-7 ~]#/etc/rc.d/init.d/nginx reload//Smooth reboot Nginx service nginx:the configuration file/usr/local/nginx/ 
conf/nginx.conf syntax are OK 
nginx:configuration file/usr/local/nginx/conf/nginx.conf test is successful 
Reloading Nginx:                      

V. Add WWW1 A records on the DNS server


Six, through IE access to for testing.

Don't forget to point the network card preferred DNS to

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.