[英]Nginx limit_req_zone using http_x_forwarded_for is not working for me
我正在嘗試為我的服務添加速率限制,因為它有時會收到來自特定 ips 的突發呼叫這是我的 nginx 配置 -
files:
/etc/nginx/sites-enabled/https.conf:
content: |
map $http_upgrade $connection_upgrade {
default "upgrade";
"" "";
}
limit_req_zone $request_method$uri zone=employees:1m rate=75r/s;
limit_req_zone $http_x_forwarded_for zone=employees_by_ip:1m rate=5r/s;
limit_req_log_level error;
limit_req_status 403;
# HTTPS server
server {
listen 443;
gzip on;
gzip_comp_level 4;
gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
set $hour $4;
}
access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
access_log /var/log/nginx/access.log logger-json;
ssl on;
ssl_certificate /etc/pki/tls/certs/server.crt;
ssl_certificate_key /etc/pki/tls/certs/server.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
proxy_http_version 1.1;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
location /employees/configs {
limit_req zone=employees burst=75 nodelay;
limit_req zone=employees_by_ip burst=5 nodelay;
proxy_pass http://docker;
}
location /employees {
limit_req zone=employees burst=75 nodelay;
limit_req zone=employees_by_ip burst=5 nodelay;
proxy_pass http://docker;
}
# Everything else
location / {
proxy_pass http://docker;
}
}
奇怪的是,這在我的一個環境中有效,但在另一個環境中無效,我無法弄清楚為什么它不會。 請幫忙,我有點卡住了
根據請求方法和 URI 限制請求不是一個好主意。 如果您想限制特定 ip 的連接,請使用$binary_remote_addr
類的東西。
您確定要將每秒請求數限制為5嗎? 所以讓我們把它畫出來:
HTTP1.1 GET / --> index.html (contains images, css, js)
HTTP1.1 GET /style.css ->
HTTP1.1 GET /header.png
HTTP1.1 GET /avatar.png
HTTP1.1 GET /script.js
HTTP1.1 GET /date.js --> ERROR Limit exeeded.
您可以使用burst
和nodeplay
指令在您的limit_req
中調整此行為。 我們有一篇很棒的博客文章:
https://www.nginx.com/blog/rate-limiting-nginx/
最重要的是,小心X-Forwarded-For
。 這個 header 可以被任何客戶端操縱,並可能導致速率限制繞過漏洞。
我剛剛回答了關於 SO 的另一個速率限制問題。
Nginx 檢查 Cloudflare 是轉發還是直接 IP 並相應限制
在這個線程中,我解釋了X-Forwarded-For
的問題以及如何緩解。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.