Nginx用户认证

针对整个站点的用户认证

1.创建一个虚拟主机
[root@antong vhost]# cd /usr/local/nginx/conf/vhost/
[root@antong vhost]# vim test.com.conf    //复制以下内容
server
{
    listen 80;
    server_name test.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    
location  /
    {
        auth_basic              "Auth";    //定义的用户名
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;   //指定密码文件
}
}
[root@antong vhost]# yum install -y httpd
[root@antong vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd test
New password:      //这里我设置的为123
Re-type new password: 
Adding password for user test
[root@antong vhost]# /usr/local/nginx/sbin/nginx -t
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
[root@antong vhost]# /usr/local/nginx/sbin/nginx -s reload
//重新加载的好处在于如果配置文件是错误的,重新加载不会生效,不会破坏原来的nginx服务,restart的话会起不来,停止工作。
2.创建网页文件
[root@antong vhost]# mkdir -p /data/wwwroot/test.com
[root@antong vhost]# echo "test.com" > /data/wwwroot/test.com/index.html
3.测试
[root@antong vhost]# curl -x127.0.0.1:80 test.com 
<html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.17.8</center>
</body>
</html>
[root@antong vhost]# curl -utest:123 -x127.0.0.1:80 test.com              test.com

针对目录的用户认证

1.修改虚拟主机文件
[root@antong vhost]# vim test.com.conf   //在location  /后面加admin
server
{
    listen 80;
    server_name test.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;

location  /admin/
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
}
[root@antong vhost]# /usr/local/nginx/sbin/nginx -t
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
[root@antong vhost]# /usr/local/nginx/sbin/nginx -s reload 
2.测试
[root@antong vhost]# curl -x127.0.0.1:80 test.com
test.com
[root@antong vhost]# curl -x127.0.0.1:80 test.com/admin/
<html>
<head><title>401 Authorization Required</title></head>   //401为访问失败
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.17.8</center>
</body>
</html>
[root@antong vhost]# curl -utest:123 -x127.0.0.1:80 test.com/admin/ 
<html>
<head><title>404 Not Found</title></head>    //404是因为没有创建网页文件
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.17.8</center>
</body>
</html>

针对url的用户认证

1.修改虚拟主机文件
[root@antong vhost]# vim test.com.conf   //在location后面加~ admin.php
server
{
    listen 80;
    server_name test.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;

location ~ admin.php
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
}
[root@antong vhost]# /usr/local/nginx/sbin/nginx -t
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
[root@antong vhost]# /usr/local/nginx/sbin/nginx -s reload
2.测试
[root@antong vhost]# curl -x127.0.0.1:80 test.com/admin/           
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.17.8</center>
</body>
</html>
[root@antong vhost]# curl -x127.0.0.1:80 test.com/admin.php
<html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.17.8</center>
</body>
</html>
[root@antong vhost]# curl -utest:123 -x127.0.0.1:80 test.com/admin.php
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.17.8</center>
</body>
</html>