问题原文
原帖链接 · Mike Purcell · CC BY-SA 4.0
Given the following NGinx config, how would I alter it to issue a Retry-After header to ONLY 429 reponses?
limit_req_zone $binary_remote_addr zone=ip:10m rate=5r/s;
limit_req_status 429;
limit_conn_status 429;
server {
listen 80;
location /api {
limit_req zone=ip burst=12 nodelay;
proxy_pass http://website;
}
}
已采纳解答
原帖链接 · Alexey Ten · CC BY-SA 4.0
There is variable $status that holds response status value. You could use map directive to define another variable that holds retry delay and use add_header directive.
add_header Retry-After $retry_after always;
add_header will not send header if value (second argument) is empty. And we need always flag, otherwise it will not set header for 429 status code.
Full example:
limit_req_zone $binary_remote_addr zone=ip:10m rate=5r/s;
limit_req_status 429;
limit_conn_status 429;
map $status $retry_after {
default '';
429 '120';
}
server {
listen 80;
location /api {
limit_req zone=ip burst=12 nodelay;
proxy_pass http://website;
add_header Retry-After $retry_after always;
}
}
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
