nginx访问控制

本文详细介绍了如何使用Nginx进行访问控制,包括启用stub_status模块,理解allow和deny规则,以及实现用户认证。同时,文章还阐述了Nginx的HTTPS配置过程,包括生成密钥、证书签署请求、配置HTTPS以及验证步骤。
摘要由CSDN通过智能技术生成

一、nginx访问控制

//用于location段
Allow:设定允许哪台或哪些主机访问,多个参数间用空格隔开
Deny:设定禁止那台或哪些主机访问,多个参数间用空格隔开

开启stub_status模块

stub_status模块主要作用于查看nginx的一些状态信息

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location /aaa  {
   
                echo "renweiwei";
                deny 192.168.232.66;   //真实机ip
                stub_status on;
        }
[root@localhost ~]# rm -rf /usr/local/nginx/html/index.html 
[root@localhost ~]# nginx -s reload
  • Active connections:当前nginx正在处理的活动连接数
  • Server accepts handled requests:nginx总共处理了63个连接,成功创建63次握手,总共处理了62个请求
  • Reading:nginx读取到客户端的Header信息数
  • Writing:nginx返回给客户端的Header信息数
  • Waiting:开启keep-alive的情况下,这个值等于active-(reading+writing),意思就是nginx已经处理完成,正在等候下一次请求指令的驻留连接。所以,在访问效率高、请求很快就被处理完毕的情况下,waiting数比较多是正常的。如果reading+writing数较多,则说明并发访问量非常大,正在处理过程中。
当allow和deny同时存在时
真实机不能访问,虚拟机能访问
location /status  {
   
                stub_status on;
                deny 192.168.232.66;
                allow all;
        }

在这里插入图片描述
在这里插入图片描述

默认两台都能访问
location /status  {
   
                stub_status on;
                deny 192.168.232.66;
                allow all;
        }

在这里插入图片描述

二、用户认证

auth_basic “欢迎信息”;
auth_basic_user_file “/path/to/user_auth_file”;
//user_auth_file内容格式


[root@localhost ~]# ls -a
.                .bash_history  .bashrc                   echo-nginx-module-master.zip  .tcshrc
..               .bash_logout   .cshrc                    nginx-1.22.0                  .user_auth_file
anaconda-ks.cfg  .bash_profile  echo-nginx-module-master  nginx-1.22.0.tar.gz           .viminfo
[root@localhost ~]# cat .user_auth_file 
renweiwei:$apr1$kGrzOOe4$mUifWhIUNdmmgFJKQyP.K.

授权用户

安装httpd-tools软件包

yum -y install httpd-tools
创建用户密钥文件
# 这里的密码为加密后的密码串,建议用htpasswd来创建文件

[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# htpasswd -c -m .user_auth_file renweiwei
New password: 
Re-type new password: 
Adding password for user renweiwei
[root@localhost conf]#  cat .user_auth_file
renweiwei:$apr1$FPEyERxK$zKb9zmuDJ2eZvJLZH7CY3.

配置nginx(注意auth_basic_user_file必须用绝对路径)
location /status  {
   
                stub_status on;
                auth_basic "my name is renweiwei";
                auth_basic_user_file "/usr/local/nginx/conf/.user_auth_file";
        }

ngint -t 测试配置文件并重载配置文件
[root@localhost conf]#  nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值