欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

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

发布时间:2025/5/22 编程问答 42 豆豆
生活随笔 收集整理的这篇文章主要介绍了 nginx访问控制:如何通过map来控制http_x_forwarded_for访问限制 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

通过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;}



转载于:https://blog.51cto.com/mersap/1883889

总结

以上是生活随笔为你收集整理的nginx访问控制:如何通过map来控制http_x_forwarded_for访问限制的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。