12.8 Nginx用户认证

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/目录php

[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]#
相关文章
相关标签/搜索