12.8 Nginx用户认证

时间:2022-04-27
本文章向大家介绍12.8 Nginx用户认证,主要内容包括Nginx用户认证目录概要、Nginx用户认证、针对URL、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

Nginx用户认证目录概要

  • vim /usr/local/nginx/conf/vhost/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;
    }
}
  • yum install -y httpd
  • htpasswd -c /usr/local/nginx/conf/htpasswd aming
  • -t && -s reload //测试配置并重新加载
  • mkdir /data/wwwroot/test.com
  • echo “test.com”>/data/wwwroot/test.com/index.html
  • curl -x127.0.0.1:80 test.com -I//状态码为401说明需要验证
  • curl -uaming:passwd 访问状态码变为200
  • 编辑windows的hosts文件,然后在浏览器中访问test.com会有输入用户、密码的弹窗
  • 针对目录的用户认证
location  /admin/
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}

Nginx用户认证

  1. 首先切换到usr/local/nginx/conf/vhost/目录下
[root@hanfeng ~]# cd /usr/local/nginx/conf/vhost/
[root@hanfeng vhost]# 
  1. 新建新建一个虚拟主机test.com.conf,并编辑
[root@hanfeng vhost]# ls
aaa.com.conf
[root@hanfeng 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  /                    //表示全站,都需要进行用户认证
    #location  /admin                                     // 这个地方只要加上” /admin ” 就变成 针对这个站点的“admin” 这个目录需要用户认证
    #location  ~ admin.php                          //如果把这行这样写,就会变成,匹配 “ admin.php ”这个页面的时候才需要用户认证
    {
        auth_basic              "Auth";            //定义用户认证的名字
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;         //用户名密码文件
    }
}

保存退出
  1. 在配置完成后,需要生成密码文件
  2. 在生成密码文件,需要用到Apache生成密码文件的工具“ htpasswd ”
  • 若本机已经安装过Apache,可以直接使用命令htpasswd进行生成
/usr/local/apache2.4/bin/htpasswd
  • 若是本机未安装Apache,可直接 yum install -y httpd 进行安装,因为yum安装的,所以工具存放在/usr/bin/下,可以直接使用htpasswd
yum install -y httpd 
  1. 这里由于未安装过Apache,所以先yum安装
[root@hanfeng vhost]# yum install -y httpd 

在yum安装后,可以直接使用htpasswd命令
  1. htpasswd指定文件,生成用户
[root@hanfeng vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd hanfeng
New password:                     //密码hanfeng
Re-type new password: 
Adding password for user hanfeng
[root@hanfeng vhost]# 
  1. 使用cat 命令查看/usr/local/nginx/conf/htpasswd 文件,会看到生成了一行字符串
[root@hanfeng vhost]# cat /usr/local/nginx/conf/htpasswd 
hanfeng:$apr1$Vvig1g73$oHYs5Ng/ubqoYXzZT4TWP/
[root@hanfeng vhost]# 
  1. 关于htpasswd -c 命令 第一次创建的时候因为没有htpasswd这个文件,需要-c创建,第二使用的时候因为已经有这个htpasswd文件了,将不再需要-c 选项,如果还继续使用-c 这个选项,将会重置 htpasswd里的东西
  2. 再来htpasswd指定文件,生成另一个用户
[root@hanfeng vhost]# htpasswd /usr/local/nginx/conf/htpasswd gurui
New password: 
Re-type new password: 
Adding password for user gurui
[root@hanfeng vhost]# cat /usr/local/nginx/conf/htpasswd 
hanfeng:$apr1$Vvig1g73$oHYs5Ng/ubqoYXzZT4TWP/
gurui:$apr1$mqc2Dgwa$qVvurqGN6gj8hX3tEpQ6j/
[root@hanfeng vhost]# 
  1. 检查配置nginx文件是否存在语法错误
[root@hanfeng 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@hanfeng vhost]# 
  1. 重新加载配置文件
  • 在重新加载的时候,若配置文件中存在错误,配置文件将不会生效;
  • 如果是直接使用restart,如果配置有错,将会直接影响到网站的运行
[root@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng vhost]# 
  1. 测试
[root@hanfeng vhost]# curl -x127.0.0.1:80 test.com
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@hanfeng vhost]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.1
Date: Wed, 03 Jan 2018 16:52:23 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"

[root@hanfeng vhost]# 
  1. 会提示错误码401,就是需要用户,所以用curl指定用户
  2. 这时指定用户和密码再来访问,会提示404,这是因为去访问index.html,但是还未创建
[root@hanfeng vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@hanfeng vhost]# 
  1. 创建目录,然后新建index.html
[root@hanfeng vhost]# mkdir /data/wwwroot/test.com
[root@hanfeng vhost]# echo “test.com”>/data/wwwroot/test.com/index.html
[root@hanfeng vhost]#
  1. 这时再来访问,会看到显示正常
[root@hanfeng vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com
“test.com”
[root@hanfeng vhost]# 
  1. 这里的用户认证是针对整站

针对某一个目录下,才需要认证

  • 比如访问admin的时候,才需要认证
  1. 首先访问admin尝试下
[root@hf-01 vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com/admin/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@hf-01 vhost]# 
  1. 然后在/usr/local/nginx/conf/vhost/test.com.conf配置文件中定义,只需要在location / 后加上admin/ 目录即可
[root@hf-01 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;
    }
}
保存退出
  1. 检查配置文件是否存在语法错误
[root@hf-01 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@hf-01 vhost]# 
  1. 重新加载配置文件
[root@hf-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hf-01 vhost]# 
  1. 这时候再来访问test.com,就不需要指定用户名和密码了
[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com
“test.com”
[root@hf-01 vhost]#

6.访问test.com/admin/目录

[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com/admin/
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@hf-01 vhost]# 
  1. 这时创建一个测试页面
  2. 先新建目录
[root@hf-01 vhost]# mkdir /data/wwwroot/test.com/admin
[root@hf-01 vhost]# 
  1. 然后在admin目录下新建index.html
[root@hf-01 vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/adm
in/index.html
[root@hf-01 vhost]#
  1. 这时再来访问 test.com/admin/ 会显示401,但是指定用户名和密码后就会正常显示
[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com/admin/
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@hf-01 vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com/admin/
test.com admin dir
[root@hf-01 vhost]# 

针对URL

  • 比如针对admin.php
  1. 首先在配置文件/usr/local/nginx/conf/vhost/test.com.conf下定义,在 location 后加~ admin.php即可
[root@hf-01 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;
    }
}
保存退出
  1. 检查配置文件是否存在语法错误
[root@hf-01 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@hf-01 vhost]# 
  1. 重新加载配置文件
[root@hf-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hf-01 vhost]# 
  1. 这时候就可以直接访问 test.com/admin/,不需要指定用户名和密码了,但是在访问admin.php的时候,则会显示401——>状态码为401说明需要验证
[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com/admin/
test.com admin dir
[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com/admin.php
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@hf-01 vhost]#