原网页地址:
How To Limit Rate of Connections (Requests) in NGINX (tecmint.com)
The following configuration example shows limiting the rate of request to a web application API. The shared memory size is 20 MB and the request rate limit is 10 requests per second.
upstream api_service {
server 127.0.0.1:9051;
server 10.1.1.77:9052;
}
limit_req_zone $binary_remote_addr zone=limitreqsbyaddr:20m rate=10r/s;
limit_req_status 429;
server {
listen 80;
server_name testapp.tecmint.com;
root /var/www/html/testapp.tecmint.com/build;
index index.html;
#include snippets/error_pages.conf;
proxy_read_timeout 600;
proxy_connect_timeout 600;
proxy_send_timeout 600;
location / {
try_files $uri $uri/ /index.html =404 =403 =500;
}
location /api {
limit_req zone=limitreqsbyaddr;
proxy_pass http://api_service;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}