Configure Nginx server methods to prevent flood attacks _nginx

Source: Internet
Author: User
Tags http 200 iptables nginx server


I'll simply tell you how to configure Nginx's restricted request module and how it protects your site from attacks against DDoS or other HTTP based denial of service attacks.

In this test, I put the sample page in the (now a free service) named About.html, which is used to test limit_req instructions.

First, I use the following instructions on the Blitz to initiate 1075 concurrent requests and continue for a minute, the response timeout is set to 2 minutes, the region is California, and all States other than state 200 are set to be abnormal, or even 503 are considered unsuccessful.

-P 1-1075:60--status 200-t 2000-r California

It's not bad, right? But if this is a PHP document. It is likely that a user will cause the PHP process to be 502/504 states, causing the server to crash or not respond. Especially if you use a VPS or other inexpensive server without any protection, the failure rate will be higher. (original advertisement, here shielding)

Of course, you can use caching or other tools to improve server performance and responsiveness, such as you use wordpress you are sure to use the WordPress caching plugin. Da for those type of people we can use the Limit request module.

In Nginx we create a zone HTTP {}, I call him Blitz set 5 requests per second, the maximum number of data to accommodate 10MB. I use $BINARY_REMOTE_ADDR as the session variable to make you more than the $remote_ Addr's normal visitors can access more than 10MB of space.

Copy Code code as follows:
Limit_req_zone $binary _remote_addr zone=blitz:10m rate=5r/s;

The rules are defined on the server:

Copy Code code as follows:
Location =/about.html {
Limit_req Zone=blitz Nodelay;

Then reload the Nginx configuration and look at the effect:

You will find that now more than 285 people can access to the server, 4.75 requests per second, no more than 5 times per second we set, check the log you will find that no access to the request is HTTP 503, access to the HTTP 200.

Using this setting is helpful for trying to restrict area access, and it can also be applied to all PHP requests.

PHP Application Request Restrictions

If you want to limit all PHP application restrictions, you can do this:

Copy Code code as follows:
Location ~ \.php {
Limit_req Zone=flood;
Include php_params.conf;
Fastcgi_pass Unix:/tmp/php5-fpm.sock;

It can help you with something. Set items like acceleration or deceleration to respond to sudden or no latency requirements. Configuration item details, Bash here: Httplimitreqmodule.

You may notice that the chart above has tested 1075 user requests, which is misleading because all access requests are from the same IP ( that is located in California.

It's hard for me to achieve a real high traffic network or DDoS (distributed Denial-of-service Attack). That's why the number of successful users we visit is not very large with IP. The server load also affects the number of visits or regions of the test user. With the free version, you can access up to 50 users with the maximum number of concurrent accesses. Of course you can spend a day $49 to get 1000 users to visit your site.

If you have enough memory and bandwidth, it is easy to test with a single IP address. Use this tool to achieve: high concurrency, AB, openload and so on. It's just a terminal interface with no UI.

Of course you have to test yourself and remember to use the status flag because Blitz will respond to the access request in about 5 seconds.

A better alternative

There's no further detail here, and if you're serious about blocking DDoS or multi-service attack against your server, there are other great software tools like Iptables (Linux), PF (packet filter for BSD), or your suit You can use your hardware firewall if you provide hardware. The above limiting module will only prevent flood attacks through HTTP requests, it will not prevent ping packet flood attacks or other vulnerabilities, for which you can turn off unwanted services and unwanted ports to prevent others from breaking through.

For example, my server has only Http/https and SSH ports exposed to the external network. The bindings of these services like MySQL are connected to the final. You can also set some generic services to a less-commonly used port so that the sniffer (IPTABLES/PF will help).

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: 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.