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用户认证
- 首先切换到usr/local/nginx/conf/vhost/目录下
[root@hanfeng ~]# cd /usr/local/nginx/conf/vhost/
[root@hanfeng vhost]#
- 新建新建一个虚拟主机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; //用户名密码文件
}
}
保存退出
- 在配置完成后,需要生成密码文件
- 在生成密码文件,需要用到Apache生成密码文件的工具“ htpasswd ”
- 若本机已经安装过Apache,可以直接使用命令htpasswd进行生成
/usr/local/apache2.4/bin/htpasswd
- 若是本机未安装Apache,可直接 yum install -y httpd 进行安装,因为yum安装的,所以工具存放在/usr/bin/下,可以直接使用htpasswd
yum install -y httpd
- 这里由于未安装过Apache,所以先yum安装
[root@hanfeng vhost]# yum install -y httpd
在yum安装后,可以直接使用htpasswd命令
- 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]#
- 使用cat 命令查看/usr/local/nginx/conf/htpasswd 文件,会看到生成了一行字符串
[root@hanfeng vhost]# cat /usr/local/nginx/conf/htpasswd
hanfeng:$apr1$Vvig1g73$oHYs5Ng/ubqoYXzZT4TWP/
[root@hanfeng vhost]#
- 关于htpasswd -c 命令 第一次创建的时候因为没有htpasswd这个文件,需要-c创建,第二使用的时候因为已经有这个htpasswd文件了,将不再需要-c 选项,如果还继续使用-c 这个选项,将会重置 htpasswd里的东西
- 再来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]#
- 检查配置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]#
- 重新加载配置文件
- 在重新加载的时候,若配置文件中存在错误,配置文件将不会生效;
- 如果是直接使用restart,如果配置有错,将会直接影响到网站的运行
[root@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng vhost]#
- 测试
[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]#
- 会提示错误码401,就是需要用户,所以用curl指定用户
- 这时指定用户和密码再来访问,会提示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]#
- 创建目录,然后新建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]#
- 这时再来访问,会看到显示正常
[root@hanfeng vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com
“test.com”
[root@hanfeng vhost]#
- 这里的用户认证是针对整站
针对某一个目录下,才需要认证
- 比如访问admin的时候,才需要认证
- 首先访问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]#
- 然后在/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;
}
}
保存退出
- 检查配置文件是否存在语法错误
[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]#
- 重新加载配置文件
[root@hf-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hf-01 vhost]#
- 这时候再来访问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]#
- 这时创建一个测试页面
- 先新建目录
[root@hf-01 vhost]# mkdir /data/wwwroot/test.com/admin
[root@hf-01 vhost]#
- 然后在admin目录下新建index.html
[root@hf-01 vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/adm
in/index.html
[root@hf-01 vhost]#
- 这时再来访问 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
- 首先在配置文件/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;
}
}
保存退出
- 检查配置文件是否存在语法错误
[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]#
- 重新加载配置文件
[root@hf-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hf-01 vhost]#
- 这时候就可以直接访问 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]#
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 系统设计:如何让系统高可用?
- 什么是事务隔离
- Python|Pandas的常用操作
- C++设计模式笔记(01)-设计模式的介绍
- 【DB笔试面试842】在Oracle中,如何启动Oracle数据库的监听日志?
- 【揭秘】C语言类型转换时发生了什么?
- JUC学习之8锁现象
- TensorFlow惊现大bug?网友:这是逼着我们用PyTorch啊!
- 机器学习|用Q-Learning走迷宫
- C++设计模式笔记(03-02) - Template Method_模板方法(下)
- 从零开始学习华为路由交换 | OSPF修改计时器
- 【没落的985/211】Python爬取知乎8万字回答进行高校分析
- LinkedBlockingQueue源码学习
- 三歪吐血总结了各个中间件是如何实现持久化的
- ThreadPoolExecutor源码学习