未分类

Nginx限制IP访问

先做一个clientRealIp变量,clientRealIp用http_x_forwarded_for的第一个IP

1
2
3
4
map $http_x_forwarded_for  $clientRealIp {
"" $remote_addr;
~^(?P<firstAddr>[0-9\.]+),?.*$ $firstAddr;
}

设置IP访问限制

1
2
3
4
if ($clientRealIp ~* "121.42.0.18|121.42.0.19") {
return 403;
break;
}

参考:
https://cloud.tencent.com/developer/article/1068413

分享到