前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >12.8 Nginx用户认证

12.8 Nginx用户认证

作者头像
运维小白
发布2018-02-06 15:57:20
1.1K0
发布2018-02-06 15:57:20
举报
文章被收录于专栏:运维小白运维小白

Nginx用户认证目录概要

  • vim /usr/local/nginx/conf/vhost/test.com.conf//写入如下内容
代码语言:javascript
复制
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会有输入用户、密码的弹窗
  • 针对目录的用户认证
代码语言:javascript
复制
location  /admin/
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}

Nginx用户认证

  1. 首先切换到usr/local/nginx/conf/vhost/目录下
代码语言:javascript
复制
[root@hanfeng ~]# cd /usr/local/nginx/conf/vhost/
[root@hanfeng vhost]# 
  1. 新建新建一个虚拟主机test.com.conf,并编辑
代码语言:javascript
复制
[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进行生成
代码语言:javascript
复制
/usr/local/apache2.4/bin/htpasswd
  • 若是本机未安装Apache,可直接 yum install -y httpd 进行安装,因为yum安装的,所以工具存放在/usr/bin/下,可以直接使用htpasswd
代码语言:javascript
复制
yum install -y httpd 
  1. 这里由于未安装过Apache,所以先yum安装
代码语言:javascript
复制
[root@hanfeng vhost]# yum install -y httpd 

在yum安装后,可以直接使用htpasswd命令
  1. htpasswd指定文件,生成用户
代码语言:javascript
复制
[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 文件,会看到生成了一行字符串
代码语言:javascript
复制
[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指定文件,生成另一个用户
代码语言:javascript
复制
[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文件是否存在语法错误
代码语言:javascript
复制
[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,如果配置有错,将会直接影响到网站的运行
代码语言:javascript
复制
[root@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng vhost]# 
  1. 测试
代码语言:javascript
复制
[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,但是还未创建
代码语言:javascript
复制
[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
代码语言:javascript
复制
[root@hanfeng vhost]# mkdir /data/wwwroot/test.com
[root@hanfeng vhost]# echo “test.com”>/data/wwwroot/test.com/index.html
[root@hanfeng vhost]#
  1. 这时再来访问,会看到显示正常
代码语言:javascript
复制
[root@hanfeng vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com
“test.com”
[root@hanfeng vhost]# 
  1. 这里的用户认证是针对整站

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

  • 比如访问admin的时候,才需要认证
  1. 首先访问admin尝试下
代码语言:javascript
复制
[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/ 目录即可
代码语言:javascript
复制
[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. 检查配置文件是否存在语法错误
代码语言:javascript
复制
[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. 重新加载配置文件
代码语言:javascript
复制
[root@hf-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hf-01 vhost]# 
  1. 这时候再来访问test.com,就不需要指定用户名和密码了
代码语言:javascript
复制
[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com
“test.com”
[root@hf-01 vhost]#

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

代码语言:javascript
复制
[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. 先新建目录
代码语言:javascript
复制
[root@hf-01 vhost]# mkdir /data/wwwroot/test.com/admin
[root@hf-01 vhost]# 
  1. 然后在admin目录下新建index.html
代码语言:javascript
复制
[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,但是指定用户名和密码后就会正常显示
代码语言:javascript
复制
[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即可
代码语言:javascript
复制
[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. 检查配置文件是否存在语法错误
代码语言:javascript
复制
[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. 重新加载配置文件
代码语言:javascript
复制
[root@hf-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hf-01 vhost]# 
  1. 这时候就可以直接访问 test.com/admin/,不需要指定用户名和密码了,但是在访问admin.php的时候,则会显示401——>状态码为401说明需要验证
代码语言:javascript
复制
[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]# 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Nginx用户认证目录概要
  • Nginx用户认证
    • 针对某一个目录下,才需要认证
      • 针对URL
      相关产品与服务
      轻量应用服务器
      轻量应用服务器(TencentCloud Lighthouse)是新一代开箱即用、面向轻量应用场景的云服务器产品,助力中小企业和开发者便捷高效的在云端构建网站、Web应用、小程序/小游戏、游戏服、电商应用、云盘/图床和开发测试环境,相比普通云服务器更加简单易用且更贴近应用,以套餐形式整体售卖云资源并提供高带宽流量包,将热门开源软件打包实现一键构建应用,提供极简上云体验。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档