大网数据广告活动
服务器运维问答

NGinx add Retry-After header to ONLY 429 Responses

· 52CE 我爱测

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;…

问题原文

原帖链接 · 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;
    }
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-08-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。