Experimental environment:
Now the company has opened an internal website www1.rsyslog.org, which requires everyone to enter the default user name and password before viewing the web page information. The webpage
uses nginx to build a virtual host
www1.rsyslog.org for testing.
www1.rsyslog.org 192.168.100.107
dns1 192.168.100.102
1. Check the current system parameters
[root@rhel6u3-2 ~]# uname –r //查看系统内核版本号 2.6.32-279.el6.i686 [root@rhel6u3-2 ~]# cat /etc/redhat-release //查看系统版本号 red hat enterprise linux server release 6.3 (santiago)
2. Edit the virtual host configuration file
[root@rhel6u3-7 ~]# vim /usr/local/nginx/conf/nginx.conf //主配置文件中http字段中添加以下语句,将虚拟主机的配置文件指向www1.rsyslog.org include /usr/local/nginx/server/www1.rsyslog.org; [root@rhel6u3-7 ~]# vim /usr/local/nginx/server/www1.rsyslog.org server { listen 80; //监听端口为80 server_name www1.rsyslog.org; //虚拟主机网址 location / { root sites/www1; //虚拟主机网站根目录 index index.html index.htm; //虚拟主机首页 auth_basic "secret"; //虚拟主机认证命名 auth_basic_user_file /usr/local/nginx/passwd.db; //虚拟主机用户名密码认证数据库 } location /status { stub_status on; //开启网站监控状态 access_log /usr/local/nginx/logs/www1_status.log; //监控日志 auth_basic "nginxstatus"; } }
3. Generate the user name and corresponding password database file through the htpasswd command.
[root@rhel6u3-7 server]# htpasswd -c /usr/local/nginx/passwd.db xiaonuo //创建认证信息,xiaonuo 为认证用户名 new password: ******* //输入认证密码 re-type new password: ******** //再次输入认证密码 adding password for user xiaonuo [root@rhel6u3-7 server]# [root@rhel6u3-7 ~]# chmod 400 /usr/local/nginx/passwd.db //修改网站认证数据库权限 [root@rhel6u3-7 ~]# chown nginx. /usr/local/nginx/passwd.db //修改网站认证数据库属主和属组 [root@rhel6u3-7 ~]# cat /usr/local/nginx/passwd.db //可以看到通过htpasswd生成的密码为加密格式 xiaonuo:8ezaz7bqcrxmy [root@rhel6u3-7 ~]#
4. Smoothly restart the nginx service
[root@rhel6u3-7 ~]# /etc/rc.d/init.d/nginx reload //平滑重启nginx服务 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful reloading nginx: [ ok ]
5. Add www1 a record on the dns server
www1 a 192.168.100.107
6. Visit www1.rsyslog.org through ie for testing.
Don’t forget to point the network card’s preferred dns to 192.168.100.102
The above is the detailed content of How does Nginx allow users to access web sites through username and password authentication?. For more information, please follow other related articles on the PHP Chinese website!