I. Overview
II. Some solutions to prevent DDOS in our projects
A distributed denial-of-service (DDoS) attack attempts to disrupt the traffic of a targeted server, service, or network by overwhelming it with a flood of Internet traffic. By sending too many requests for information to a server, site, or network, a DDoS can effectively shut down a server — leaving it vulnerable and disrupting the normal business operations of an organization.
While DDoS attacks come in many shapes and sizes, there are measures you can take to protect your organizations from these threats. There is no one solution to preventing DDoS attacks, but using the following tips in conjunction can lessen the potential for one:
We can use Cloudflare to prevent DDoS attacks. Cloudflare is a cloud-based service that provides various security and performance features for websites and applications. One of its features is DDoS protection, which is unmetered and unlimited for all customers on all plans and services (read more)
Cloudflare's DDoS protection works by using its global network of over 285 cities and 100 countries to block malicious traffic before it reaches your servers. Cloudflare also uses dynamic fingerprinting to automatically detect and mitigate DDoS attacks without requiring any user action. Cloudflare can protect your web services (L7), your TCP/UDP applications (L4), and your network infrastructure (L3) from different types of DDoS attacks
Some of the benefits of using Cloudflare to prevent DDoS attacks are:
Nginx is a popular web server that can handle high volumes of traffic and provide various features to improve security and performance. One of the ways to prevent DDoS attacks with Nginx is to use some of the following techniques:
These are some of the ways you can prevent DDoS attacks with Nginx, but they are not exhaustive or foolproof. You should always monitor your server's performance and logs, and be ready to take additional measures if needed.
There is no definitive answer to how to configure Nginx to prevent DDoS attacks, as different settings may work better for different scenarios and websites. However, here are some examples of Nginx configuration directives that can help mitigate DDoS attacks:
limit_req_zone
. This directive creates a shared memory zone that can store the number of requests for each client IP address. You can use this zone to limit the rate of requests from a single IP address using the limit_req directive For example:limit_req_zone $binary_remote_addr zone=one:10m rate=30r/m;
This creates a zone named "one" with a size of 10 MB and allows 30 requests per minute from each IP address.
limit_conn_zone
. This directive creates a shared memory zone that can store the number of connections for each client IP address. You can use this zone to limit the number of connections from a single IP address using the limit_conn directive. For example:# This creates a zone named "two" with a size of 10 MB
# and stores the number of connections for each IP address.
limit_conn_zone $binary_remote_addr zone=two:10m;
limit_req
. This directive limits the rate of requests for a given location or server. It uses the zone created by the limit_req_zone directive and can specify a burst parameter that allows exceeding the limit for a short time. For example:# This limits the requests to 30 per minute (as defined by the zone "one")
# and allows a burst of 5 requests.
limit_req zone=one burst=5;
limit_conn
. This directive limits the number of connections for a given location or server. It uses the zone created by the limit_conn_zone directive and can specify a maximum number of connections. For example:# This limits the connections to 10 per IP address (as defined by the zone "two").
limit_conn two 10;
geo
. This directive creates a variable with a value that depends on the client IP address. You can use this variable to block or allow traffic based on the geographic location of the client. For example:# This creates a variable named "$bad_country" that is set to 1 for China and Russia
# and 0 for other countries.
# If the variable is 1 Nginx returns a special status code 444
# that closes the connection without sending any response.
geo $bad_country {
default 0;
CN 1;
RU 1;
}
if ($bad_country) {
return 444;
}
proxy_cache
. This directive enables caching of static and dynamic content from your backend servers. This can reduce the load on your servers and improve the response time for your users. For example:# This creates a cache zone named "my_cache"
# with a size of 10 GB and stores the cached files in /var/cache/nginx.
# It also sets the cache to expire after 60 minutes of inactivity.
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
proxy_cache my_cache;
proxy_cache_key
. This directive defines the key for caching requests. You can use various variables to create a unique key for each request. For example:# This creates a cache key based on the scheme, method, host, and URI of the request.
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid
. This directive sets the validity time for different response codes. You can use this directive to specify how long to cache different types of responses. For example:
# This sets the cache validity time to 10 minutes for 200 and 302 responses,
# and to 1 minute for 404 responses.
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_no_cache
. This directive defines conditions under which the response will not be saved to a cache. You can use this directive to prevent caching of certain responses based on variables. For example:# This prevents caching of responses if there is a "nocache" cookie or argument,
# or a "comment" argument.
proxy_no_cache $cookie_nocache $arg_nocache$arg_comment;
proxy_cache_bypass
. This directive defines conditions under which the request will be passed to the backend server without using a cached copy. You can use this directive to bypass the cache for certain requests based on variables. For example:# This bypasses the cache if there is a "nocache" cookie or argument,
# or a "comment" argument.
proxy_cache_bypass $cookie_nocache $arg_nocache$arg_comment;
References
AWS is a cloud platform that provides various services and features to help you prevent and mitigate DDoS attacks. Some of the ways to prevent DDoS attacks with AWS are:
References