A tutorial on how to limit the frequency of access in a unit time in Nginx _nginx

Source: Internet
Author: User

First of all, the problem is encountered because the site was attacked, Aliyun alarm, thought to limit the frequency of access, rather than restricting IP (IP restrictions on the scheme later given). Nginx connection resource is eaten empty return status code is 502, add this scheme limit after return 599, and normal status code.
The steps are as follows:

First nginx.conf Add the following:

Map $http _x_forwarded_for  $clientRealIp {"
    " $remote _addr;
    ~^(? P<firstaddr>[0-9\.] +),?. *$  $firstAddr;
    }

    # # #safe setting to limit ' request number per second
    limit_req_status 599;
    Limit_req_zone $clientRealIp zone=allips:70m rate=5r/s;

Session Pool Size 70M, if the limit of IP a lot of that can be adjusted small, if the limited IP is very few, that can access a lot of IP to be adjusted large
5 requests per second, this is also adjusted according to the situation, 5 is more appropriate or slightly larger a little.

Then modify the www.xxoo.com.conf (this is LNMP has a profile for each virtual host) server, location add the following line above:

Limit_req zone=allips burst=5 Nodelay;

This restarts the nginx to be able to write a script to test the concurrency.

A python concurrency script is provided as follows:

Import Threading
import time,urllib2


url = ' http://sf.gg/'
def worker ():
  try:
   response = Urllib2.urlopen (URL)
   print response.getcode ()
  except URLLIB2. Httperror, E:
   print E.code for

i in range:
  t = Threading. Thread (Target=worker)
  T.start ()

2000 and http://sf.gg/can be modified, and then execute Python *.py > out to analyze the distribution of status codes within the out file, if 599 says the frequency limit plays a role.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.