[nginx] Limiting the rate of requests and connection

26 Jun 2016

Share to:

nginx สามารถ config ให้ limit จํานวน request และ connection ได้ ทั้งนี้เพื่อป้องกันการ request เข้ามามากๆเกินความเป็นจริงที่ client จะทําได้

Limiting the rate of requests

nginx สามารถ limit request ที่มาจาก client ได้ เช่น ถ้าเราต้องการจะกําหนดให้ใน 1 วินาทีสามารถเรียก page1.html ได้ไม่เกิน 5 ครั้ง สามารถเขียน config ได้ดังนี้

limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s;

server {
    ...
    location /page1.html {
        limit_req zone=one;
    ...
    }
}

Limiting the Number of Connections

เราสามารถกําหนดจํานวน connection ที่เข้ามาต่อ 1 ip ได้ เช่น ถ้าต้องการให้ client แต่ละคนเข้าใช้งาน page2.html ได้ไม่เกิน 20 connection/minute สามารถ config ได้ดังนี้

limit_conn_zone $binary_remote_addr zone=addr:10m;

server {
    ...
    location /page2.html {
        limit_conn addr 20;
        ...
    }
}

Copyright © 2019 - 2024 thiti.dev |  v1.19.0 |  Privacy policy |  status | 

            วงแหวนเว็บ