一、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.