NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
aoscici2000
V2EX  ›  NGINX

Nginx 如何限制相同 request_body 的访问频率

  •  
  •   aoscici2000 · Dec 11, 2019 · 4759 views
    This topic created in 2368 days ago, the information mentioned may be changed or developed.
    limit_req_zone $request_body zone=myzone:10m rate=2r/s;
    
    server {
        location /push {
            limit_except POST {
                deny  all;
            }
            limit_req zone=myzone burst=2 nodelay;
            ...
        }
    }
    

    如上, 我希望能限制相同 request_body 的访问频率, 好像不起作用?

    9 replies    2019-12-13 18:21:26 +08:00
    lihongjie0209
        1
    lihongjie0209  
       Dec 11, 2019
    你计算一下 request_body 的 hash 放到 header 中, 然后根据 header 去重就可以了
    rrfeng
        2
    rrfeng  
       Dec 11, 2019 via Android
    @lihongjie0209
    想当然了,了解一下 Nginx 的处理阶段
    lihongjie0209
        3
    lihongjie0209  
       Dec 11, 2019
    @rrfeng #2 对哦, 先处理 header 再处理 bady, 搞反了
    lihongjie0209
        4
    lihongjie0209  
       Dec 11, 2019
    @rrfeng #2 要不前端做这个 hash ?
    caryqy
        5
    caryqy  
       Dec 11, 2019
    换个方式,通过 redis 来,md5 每次请求添加到 redis 中,设置一个过期时间,下个请求先判断 redis 中是否存在, 频率通过过期时间来控制
    cydleadingx
        6
    cydleadingx  
       Dec 11, 2019 via iPhone
    openresty 处理下
    aoscici2000
        7
    aoscici2000  
    OP
       Dec 11, 2019
    @cydleadingx 为了一个辅助性功能的话, 学习成本太大了
    cydleadingx
        8
    cydleadingx  
       Dec 12, 2019 via iPhone
    @aoscici2000 一天而已
    aoscici2000
        9
    aoscici2000  
    OP
       Dec 13, 2019
    @lihongjie0209 我试了一下多做一层代理, 第一层把 request_body 添加到 header, 由第二层做去重, 但好像不太成功, 不知道 header 是怎么设置的? 能否指点一下 ^^

    limit_req_zone $http_HEADER zone=myzone:10m rate=1r/s;

    location /a {
    proxy_pass http://127.0.0.1/b;
    add_header data $request_body;
    }

    location /b {
    proxy_pass http://127.0.0.1/real-addr;
    limit_req zone=myzone burst=2 nodelay;
    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3139 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 03:37 · PVG 11:37 · LAX 20:37 · JFK 23:37
    ♥ Do have faith in what you're doing.