Nginx的http_access_module模块

http_access_module模块配置

 

Syntax:allow address | CIDR | unix: | all;
Default:
Context:httpserverlocationlimit_except

Allows access for the specified network or address. If the special value unix: is specified (1.5.1), allows access for all UNIX-domain sockets.

允许指定网段及ip的请求, 也支持unix相关域名配置,all为允许所有

 

Syntax:deny address | CIDR | unix: | all;
Default:
Context:httpserverlocationlimit_except

Denies access for the specified network or address. If the special value unix: is specified (1.5.1), denies access for all UNIX-domain sockets.

禁止指定ip及网段的请求,支持unix相关域名配置,all为禁止所有

location / {
    deny  192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    allow 2001:0db8::/32;
    deny  all;
}
location / {
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    deny  all;
}
location / {
    deny  192.168.1.1;
    allow  all;
}

CIDR(无类别域间路由,Classless Inter-Domain Routing)是一个在Internet上创建附加地址的方法,这些地址提供给服务提供商(ISP),再由ISP分配给客户。CIDR将路由集中起来,使一个IP地址代表主要骨干提供商服务的几千个IP地址,从而减轻Internet路由器的负担。

CIDR技术用子网掩码中连续的1部份表示网络ID,连续的0部份表示主机ID。

公式:

1、主机数=2^主机ID位-2

2、网络数=2^可变的网络ID位

3、网络ID=IP和netmask 相与

4、划分子网数=2^网络ID向主机ID借的位数

5、划分子网损失IP数=2*(划分子网数-1)(因为每段子网中的第一个IP地址用来表示网络位,而最后一个IP地址都被用来表示本网段中的广播。)

注意:

子网掩码:二进制的表示格式中,网络位全为1;

网络位:用于表示该网段的地址;

主机位:用于表示该主机位于该网段中的地址;
--------------------- 
作者:han156 
来源:CSDN 
原文:https://blog.csdn.net/han156/article/details/77817031 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

 

 

limit_except配置方式

Syntax:limit_except method ... { ... }
Default:
Context:location

Limits allowed HTTP methods inside a location. The method parameter can be one of the following: GET,HEADPOSTPUTDELETEMKCOLCOPYMOVEOPTIONSPROPFINDPROPPATCHLOCKUNLOCK, or PATCH. Allowing the GET method makes the HEAD method also allowed. Access to other methods can be limited using thengx_http_access_modulengx_http_auth_basic_module, and ngx_http_auth_jwt_module (1.13.10) modules directives:

在location内限制http的方法,method允许取值如上,配置允许指定方法方法通过,其他方法禁止;允许Get请求的时候,head方法也将被允许。

limit_except GET {//Please note that this will limit access to all methods except GET and HEAD.
    allow 192.168.1.0/32;
    deny  all;
}

location / {

     proxy_pass http://localhost:3000;

     limit_except GET {

       deny all;

     }

   }

 

扩展

名称默认配置作用域官方说明中文解读模块
limit_ratelimit_rate 0;http, server, location, if in locationLimits the rate of response transmission to a client. The rate is specified in bytes per second. The zero value disables rate limiting. The limit is set per a request, and so if a client simultaneously opens two connections, the overall rate will be twice as much as the specified limit.指定每秒该连接能下载的bytes,主要用来限制个别请求的带宽ngx_http_core_module
limit_rate_afterlimit_rate_after 0;http, server, location, if in locationSets the initial amount after which the further transmission of a response to a client will be rate limited.设置多少bytes过后将启动limit计数,如果小于此值则不限速ngx_http_core_module
limit_except没有默认值locationLimits allowed HTTP methods inside a location. The method parameter can be one of the following: GET, HEAD, POST, PUT, DELETE, MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, or PATCH. Allowing the GET method makes the HEAD method also allowed设置除了指定的http methods外其他method将被限制,允许GET就自动允许HEAD方法ngx_http_core_module

 

 

Nginx基于access_module有局限性

原理:基于客户端的IP,但是对于Nginx来说,它不会管你哪个是真正的客户端,如果我们的访问不是客户端与服务端直接连接,而是通过了一层代理,比如它的代理可以负载均衡、CDN的这种代理实现,也就是我们的访问不是客户端直接访问的服务端,而是通过其他的中间件访问服务端,这时候会出现一个问题,因为Nginx的access_module它是基于remote_addr(只是连接服务端的那个IP地址)这个变量来识别客户端的IP的,那么如果一个ip通过中间件访问服务端,那么Nginx认为访问的ip就是中间件的IP,那么我们在基于IP做限制的时候,那么其实是没有作用的。所以这样的话,准确性是不高的,所以就是利用nginx的access_module有局限性。

解决方法:

①:采用别的http头信息访问控制,如HTTP_X_FORWARDED_FOR。

但是http_x_forwarded_for进行访问控制会存在问题,因为是一个协议要求的,并不是所有的cdn和代理厂商它会按照要求来做,甚至x_forwarded_for存在被修改的可能,因为只是一个头信息,所以最终还是不真实。(不是很精确的方式)

http_x_forwardded_for也是Nginx的http头变量的一个常用的变量,它和remote_addr是有区别的。不同的是,x_forwarded_for是http协议中规定头中要携带的,所以在客户端访问中间件,再访问服务端的时候,那么服务端通过Nginx会记录真实IP和中间件的IP。
格式:http_x_forwarded_for = 客户端ip,第一台代理ip,第二台代理ip,第N台代理ip....,所以http_x_forwarded_for是由一连串以逗号分隔的ip组成的。
②:结合geo模块 —— nginx的另外一个模块
③:通过HTTP自定义变量传递
因为nginx有自己的变量,我们可以在http头信息定义一个我们规定的http变量在所有上一级的设备手动把remote_addr的ip一级一级的携带过去,既能避免x_forwarded_for被修改,也能读到客户端的真实ip

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值