首先说一下遇到这个问题是因为网站被攻击,阿里云报警,想到要限制一下访问频率,而不是限制ip(限制ip的方案稍后给出)。nginx连接资源被吃空返回状态码是502,添加本方案限制后返回599,与正常状态码区别开。
步骤如下:
首先nginx.conf里面添加如下内容:
map $http_x_forwarded_for $clientRealIp {
$remote_addr;
~^(?P<firstAddr>[0-9\.]+),?.*$ $firstAddr;
}
###safe setting to limit the request number