nginx模块

本文介绍了多个Nginx模块,包括http_stub_status_module用于监控客户端连接状态,http_radom_index_module可随机响应页面,http_sub_module能进行响应内容替换,还有限制连接和请求频率的模块,以及基于IP访问控制和用户信任登录的模块,并说明了各模块作用、指令、范围和示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • http_stub_status_module
    • 作用:监控nginx客户端连接状态
    • 指令:stub_status
    • 例子:见代码
    • 分析:
      1)Active connections:2(nginx当前活跃的连接数)
      2)server:2(nginx启动到现在共处理了2个连接)
      3)accepts:2(nginx启动到现在共创建了2次tcp握手)
      4)handled requests:1(nginx启动到现在总共处理了 1 次请求)
      5)Reading(nginx正在读取连接数)
      6)Writing(nginx正在响应的连接数)
      7)Waiting(nginx开启keep-alive情况下,处于空闲状态的连接数,waiting=active-reading-writing)
location /status {
	stub_status;
}
  • http_radom_index_module
    • 作用:随机响应x.html页面(不会读取隐藏文件)
    • 指令:radom_index on|off(default)
    • 范围:location
    • 例子:见代码
location / {
	root /opt/code;
	radom_index on;
}
  • http_sub_module
    • 作用:http响应内容替换
    • 指令:
      • sub_filter string replacement
      • sub_filter_last_modified on|off(default)
      • sub_filter_once on(default)|off
    • 范围:http、server、location
    • 例子:见代码
location / {
	root /opt/code;
	index index.html index.htm;
	sub_filter 'before' 'after';
	#全文替换
	sub_filter_once off;
}
  • limit_conn_module
    • 作用:限制连接频率
    • 指令:
      • limit_conn_zone key zone=name:size(范围:http)
      • limit_conn zone number(范围:http、server、location)
    • 例子:见代码
#分配控制空间
limit_conn_zone $binary_remote_addr zone=conn_zone:1m;	
location / {
	root /opt/code;
	index index.html index.htm;
	#allow only one connection per an IP address at a time
	limit_conn zone=conn_zone 1;
}
  • limit_req_module
    • 作用:限制请求频率
    • 指令:
      • limit_req_zone key zone=name:size rate=rate(范围:http)
      • limit_req zone=name [burst=number] [nodelay|delay=number](范围:http、server、location)
    • 例子:见代码
#分配控制空间
#相同ip地址平均每秒允许1个请求,突发请求不超过5个
limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;
location / {
	root /opt/code;
	index index.html index.htm;
	limit_req_zone=req_zone burst=5 nodelay;
}
#burst配置请求队列长度,突发请求将入列,nodelay可以让服务器同时处理突发请求,但队列位置依然被占用。
#例如:rate=5r/s,burst=20,nodelay
#某次请求数=10,有5个突发请求入列占位,10个请求被同步处理
#下次请求数=2,无突发请求,队列清空3个占位
#队满返回503错误
  • http_access_module
    • 作用:基于ip的访问控制
    • 指令:
      • allow address|CIDR|unix:|all
      • deny address|CIDR|unix:|all
    • 范围:http、server、location、limit_except
    • 例子:见代码
location ~ ^/test.html {
	root /opt/code;
	index index.html index.htm;
	deny 1.1.1.1;
	allow all;
}
location ~ ^/test.html {
	root /opt/code;
	index index.html index.htm;
	allow 192.168.2.0/24;
	deny all;
}
局限:
ip访问控制是通过remote_addr这个变量来区别不同ip地址请求,假如前面还有一层代理则无法识别源头。故判断不准确。
http_x_forwarded_for这个头信息可以携带从源头开始所有代理ip信息,但该信息可以并不是强制要求,且存在被改写风险
http_x_forwarded_for=client ip, proxy1 ip, proxy2 ip...
解决:访问控制还可以通过geo模块实现、或者通过自定义变量方式实现
  • http_auth_basic_module
    • 作用:基于用户信任登录
    • 指令:
      • auth_basic string|off(default);
      • auth_basic_user_file filepath;
    • 范围:http、server、location、limit_except
    • 例子:见代码
#创建用户密码文件
htpasswd -c /opt/conf/auth_conf username
location ~ ^/test.html {
	root /opt/code;
	index index.html index.htm;
	auth_basic "Auth access.Input your password.";
	auth_basic_user_file /opt/conf/auth_conf;
}
局限:
依赖文件方式帮助认证
文件中用户名和密码无法复用
解决:nginx+lua实现高效验证,nginx+ldap(使用nginx-auth-ldap模块)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值