Home > Operation and Maintenance > Nginx > How does Nginx allow users to access web sites through username and password authentication?

How does Nginx allow users to access web sites through username and password authentication?

WBOY
Release: 2023-05-11 17:19:15
forward
1674 people have browsed it

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)
Copy after login

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"; } 
  }
Copy after login

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 ~]#
Copy after login

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 ]
Copy after login

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.102How does Nginx allow users to access web sites through username and password authentication?How does Nginx allow users to access web sites through username and password authentication?

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template