欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > nginx常用模块(2)

nginx常用模块(2)

2025/5/17 21:38:54 来源:https://blog.csdn.net/ayang55566/article/details/147614268  浏览:    关键词:nginx常用模块(2)

版权声明:原创作品,请勿转载!

本文介绍两种常用模块,分别是IP地址限制和请求/连接限制模块

1.IP地址限制模块

一般两种配置方式:①先允许后拒绝②先拒绝后允许

先允许后拒绝allow 10.0.0.1;deny all;先拒绝后允许deny 10.0.0.1,10.0.0.253;allow all;

1.1 先允许后拒绝

只允许本机电脑vmnet8网卡IP10.0.0.1访问下载站,拒绝其他所有IP访问

修改nginx配置文件

[root@web01 conf.d]# cat yewu1.conf 
server {listen       80;server_name  www.yewu1.com;location / {root    /code/;index    index.html;}location /file {       alias   /download;charset utf-8,gbk;autoindex on;autoindex_localtime on;autoindex_exact_size on;auth_basic "test";          # 密码验证登录描述       auth_basic_user_file auth_pass; # 指定密码文件的位置 allow 10.0.0.1;deny all;}
}

修改vmnet8网卡IP

访问测试

将vmnet8网卡地址改为10.0.0.100

再访问测试,报403

1.2 先拒绝后允许

拒绝本机10.0.0.1访问,允许其他所有IP访问

修改nginx配置文件

[root@web01 conf.d]# cat yewu1.conf 
server {listen       80;server_name  www.yewu1.com;location / {root    /code/;index    index.html;}location /file {       alias   /download;charset utf-8,gbk;autoindex on;autoindex_localtime on;autoindex_exact_size on;auth_basic "test";          # 密码验证登录描述       auth_basic_user_file auth_pass; # 指定密码文件的位置 deny 10.0.0.1;allow all;}
}

修改vmnet8网卡IP

访问测试,报403

修改vmnet8网卡IP为10.0.0.100/24

访问测试,可以正常访问

2.连接/请求限制模块

常用于出现网络安全攻击时配置此模块,但要注意生产环境做限制之前要评估正常业务访问的连接数,再以此作为参考值做限制

2.1 连接限制模块limit_conn

2.1.1 修改nginx主配置文件

[root@ycl nginx]# vim nginx.conf
[root@ycl nginx]# grep 'limit_conn' nginx.conflimit_conn_zone $remote_addr zone=conn_zone:10M;limit_conn conn_zone 5;[root@ycl nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

2.1.2 访问测试并查看nginx错误日志

同一时间多个IP访问大部分都会报503错误页面,查看日志可以看到limit_conn字样

2.2 请求限制模块limit_request

2.2.1 修改nginx配置文件

[root@ycl nginx]# vim nginx.conf
[root@ycl nginx]# grep 'limit_req' nginx.conflimit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s; ###rate=1r/s代表TCP三次握手连接上之后每秒只接受1个请求,其余请求拒绝处理并返回错误码给客户端。配置到server区块外http区块内limit_req zone=req_zone burst=3 nodelay;   #配置到server区块内location区块外

2.2.2 访问测试

访问页面f12可以看到每秒一个请求,多个请求会被禁止,查看日志会看到limit request字样

更多的内容期待下篇文章吧~

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词