歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> nginx根據IP限制訪問,nginxIP限制訪問

nginx根據IP限制訪問,nginxIP限制訪問

日期:2017/3/3 18:09:30   编辑:學習Linux

nginx根據IP限制訪問,nginxIP限制訪問

nginx根據IP限制訪問,nginxIP限制訪問


nginx有兩個模塊可以控制訪問

HttpLimitZoneModule 限制同時並發訪問的數量

HttpLimitReqModule 限制訪問數據,每秒內最多幾個請求

http{

##取用戶的真實IP

map $http_x_forwarded_for $clientRealIp {
## 沒有通過代理,直接用 remote_addr
"" $remote_addr;
## 用正則匹配,從 x_forwarded_for 中取得用戶的原始IP ## 例如 X-Forwarded-For: 202.123.123.11, 208.22.22.234, 192.168.2.100,...## 這裡第一個 202.123.123.11 是用戶的真實IP,後面其它都是經過的 CDN 服務器
~^(?P<firstAddr>[0-9\.]+),?.*$ $firstAddr;
}
## 通過 map 指令,我們為 nginx 創建了一個變量 $clientRealIp ,這個就是 原始用戶的真實 IP 地址,## 不論用戶是直接訪問,還是通過一串 CDN 之後的訪問,我們都能取得正確的原始IP地址

## 每秒並發連接限制
limit_conn_zone $clientRealIp zone=TotalConnLimitZone:10m ; ##1M大概可以保存16000IP
limit_conn TotalConnLimitZone 10;##每個IP最多有10個並發連接
limit_conn_log_level notice;
## 每秒請求數限制
limit_req_zone $clientRealIp zone=ConnLimitZone:10m rate=10r/s;
limit_req_log_level notice;

}

server{

  listen 80;

  location ~ .*\.(php|php5)?$
{
limit_req zone=ConnLimitZone burst=5 nodelay; #每秒處理 10 個請求 + 5個排隊,超過直接返回503
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_connect_timeout 300s;
fastcgi_read_timeout 300s;
}

}

http://xxxxxx/Linuxjc/1169730.html TechArticle

Copyright © Linux教程網 All Rights Reserved