您的位置:首页 > 理论基础 > 计算机网络

nginx访问控制:如何通过map来控制http_x_forwarded_for访问限制

2016-12-19 10:36 597 查看

通过map修改访问限制

目前,仓库通过allow host的方式来控制访问,但实际上没有启到作用,请求从lb过来,host已经变化,所以需要用http_x_forwarded_for来做限制。

允许访问ip

19.29.55.15 13.56.217.135 211.22.42.100 119.9.14.149*其他都403*

影响范围:

不升级的情况下不影响

实现方法:

1、map(用于http段)
2、location if else来判断(server)

map:

map $http_x_forwarded_for $access
{
default false;
123.56.27.15    true;
123.103.7.29    true;
101.21.57.187  true;
221.12.42.100  true;
116.7.37.5  true;
10.150.189.186  true;
41.29.245.18   true;
}if ( $access = 'false')
{
return 403;
}

location:

set $access false;
if ( $http_x_forwarded_for = '123.56.27.135' )  {
set $access true;
}
if ( $http_x_forwarded_for = '119.29.144.149' )  {
set $access true;
}
if ( $http_x_forwarded_for = '119.29.55.135' )  {
set $access true;
}
if ( $http_x_forwarded_for = '221.122.42.100' )  {
set $access true;
}
if ( $access = 'false') {
return 403;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  http map nginx