2017-04-12 4 views
2

をこれは私のnginxの構成設定されて動作していない設定 -Nginxは:レートリミットは

{ 
    limit_req_zone $binary_remote_addr zone=main:10m rate=1r/s; # on top of conf file 
... 

    location /login { 
      limit_req zone=main burst=3 nodelay; 
      ModSecurityEnabled on; 
      ModSecurityConfig /usr/local/nginx/conf/modsecurity.conf; 
      proxy_pass http://localhost:4000; 
      proxy_http_version 1.1; 
      proxy_set_header Upgrade $http_upgrade; 
      proxy_set_header Connection 'upgrade'; 
      proxy_set_header Host $host; 
      proxy_cache_bypass $http_upgrade; 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    } 

}

次のコードを使用して、APIのURL(http://localhost:4000/login)を複数回押す -

for i in {0..2000}; do (curl -Is http://localhost:4000/login | head -n1 &) 2>/dev/null; done 

を私は拒否されるべきいくつかの要求に対して503を得る代わりに、常に200応答コードを得るようになっています。

この問題を解決してください。

+0

同じ、これに対する任意のソリューション? – pahnin

答えて

1

これは私の設定です。これで、しきい値を超えた後、503リクエストが正しく表示されます。

limit_req_zone $http_x_forwarded_for zone=req_limit_per_ip:100m rate=10r/m; 
limit_conn_zone $http_x_forwarded_for zone=conn_limit_per_ip:100m; 


server { 

listen 80; 

server_name *.xxxxxx.com; 
add_header 'Access-Control-Allow-Headers' "X-Forwarded-For; X-Forwarded-Proto; X-Forwarded-Port; Host; X-Amzn-Trace-Id; Connection"; 
#add_header 'Access-Control-Allow-Headers' "X-Requested-With"; 
add_header 'Access-Control-Allow-Methods' "GET, POST, OPTIONS"; 
#add_header 'Access-Control-Allow-Origin' "$http_origin"; 

server_tokens off; 
client_body_timeout 60s; 
client_header_timeout 60s; 
add_header 'X-Frame-Options' "SAMEORIGIN"; 
add_header 'Strict-Transport-Security' "max-age=31536000; includeSubDomains" ; 

location /api/ { 
    ModSecurityEnabled off; 
    ModSecurityConfig /usr/local/nginx/conf/modsecurity.conf; 
    proxy_pass http://xx.xxx.xxx.xxx:7000/; 
    proxy_http_version 1.1; 
    proxy_set_header Upgrade $http_upgrade; 
    proxy_set_header Connection 'upgrade'; 
    proxy_set_header Host $host; 
    proxy_cache_bypass $http_upgrade; 
    proxy_connect_timeout  60s; 
    proxy_send_timeout   60s; 
    proxy_read_timeout   60s; 
    send_timeout    60s; 

} 
} 

効果を確認するために、私は.jsファイルを作成し、上記URLにループ内で20回を要求しました。あなたは、以下の結果を確認することができます -

出力:私と一緒に enter image description here