Home  >  Article  >  Operation and Maintenance  >  How to use Nginx and Nginx Plus to resist DDOS attacks

How to use Nginx and Nginx Plus to resist DDOS attacks

WBOY
WBOYforward
2023-05-14 20:34:041150browse

1. Characteristics of application layer ddos ​​attacks

Application layer (seventh layer/http layer) ddos ​​attacks are usually initiated by Trojan horse programs, which can be better designed Exploit the vulnerabilities of the target system. For example, for a system that cannot handle a large number of concurrent requests, just by establishing a large number of connections and periodically sending out a small number of data packets to maintain the session, the system's resources can be exhausted, making it unable to accept new connection requests to achieve the purpose of DDoS. Other attacks include sending a large number of connection requests to send large data packets. Because the attack is initiated by a Trojan horse program, the attacker can quickly establish a large number of connections and issue a large number of requests in a short period of time.

The following are some DDoS special evidence, we can use these characteristics to resist DDoS (including but not limited to):

  1. Attacks often originate from some relatively fixed IPs Or IP segment, each IP has a much larger number of connections and requests than real users. (Note: This does not mean that all such requests represent DDoS attacks. In many network architectures using NAT, many clients use the IP address of the gateway to access public network resources. However, even so, the number of such requests and The number of connections will also be far less than that of a ddos ​​attack.)

  2. Because the attack is issued by a Trojan horse and the purpose is to overload the server, the frequency of requests will be far higher than that of a normal person.

  3. user-agent is usually a non-standard value

  4. referer is sometimes a value that is easily associated with an attack

2. Use nginx and nginx plus to resist ddos ​​attacks

Combined with the characteristics of ddos ​​attacks mentioned above, nginx and nginx plus have many features that can To effectively defend against DDoS attacks, we can achieve the purpose of resisting DDoS attacks by adjusting the entrance access traffic and controlling the traffic from the reverse proxy to the back-end server.

1. Limit the request speed

Set the connection request of nginx and nginx plus within a reasonable range of a real user request. For example, if you feel that a normal user can request the login page every two seconds, you can set nginx to receive a request from the client IP every two seconds (roughly equivalent to 30 requests per minute).

limit_req_zone $binary_remote_addr zone=one:10m rate=30r/m; 
server { 
... 
location /login.html { 
limit_req zone=one; 
... 
} 
}

The `limit_req_zone` command sets up a shared memory zone called one to store a specific key value of the request status, in the above example the client ip ($binary_remote_addr). The `limit_req` in the location block limits access to /login.html by referencing one shared memory area.

2. Limit the number of connections

Set the number of connections for nginx and nginx plus within a reasonable range of a real user request. For example, you can set no more than 10 connections to /store per client IP.

limit_conn_zone $binary_remote_addr zone=addr:10m; 
server { 
... 
location /store/ { 
limit_conn addr 10; 
... 
} 
}

The `limit_conn_zone` command sets up a shared memory area called addr to store the state of a specific key value, in the above example the client ip ($binary_remote_addr). `limit_conn` in the location block limits the maximum number of connections to /store/ to 10 by referencing the addr shared memory area.

3. Close slow connections

Some DDoS attacks, such as slowlris, maintain sessions by establishing a large number of connections and periodically sending some data packets. To achieve the purpose of attack, this cycle is usually lower than normal requests. In this case we can resist the attack by closing the slow connection.

The `client_body_timeout` command is used to define the timeout for reading client requests, and the `client_header_timeout` command is used to set the timeout for reading client request headers. The default values ​​of these two parameters are 60s. We can set them to 5s through the following command:

server { 
client_body_timeout 5s; 
client_header_timeout 5s; 
... 
}

4. Set ip blacklist

If it is determined that the attack comes from certain IP addresses, we can add them to the blacklist, and nginx will no longer accept their requests. For example, if you have determined that the attack comes from a range of IP addresses from 123.123.123.1 to 123.123.123.16, you can set it like this:

location / { 
deny 123.123.123.0/28; 
... 
}

Or you have determined that the attack comes from 123.123.123.3, 123.123.123.5, 123.123.123.7 Several IPs can be set like this:

location / { 
deny 123.123.123.3; 
deny 123.123.123.5; 
deny 123.123.123.7; 
... 
}

5. Set the IP whitelist

If your website only allows access to specific IPs or IP segments, You can use the allow and deny commands together to restrict access to your website to only the IP addresses you specify. As follows, you can set only intranet users in the 192.168.1.0 segment to be allowed to access: The

location / { 
allow 192.168.1.0/24; 
deny all; 
... 
}

deny command will deny access requests from all other IP addresses except the IP range specified by allow.

6. Use caching for traffic peak shaving

By turning on the caching function of nginx and setting specific caching parameters, you can reduce the traffic from attacks and at the same time It can also reduce the request pressure on the back-end server. Here are some useful settings: The updating parameter of

  1. proxy_cache_use_stale ` tells nginx when to update the cached object. Only an update request to the backend is required, and client requests for the object do not require access to the backend server while the cache is valid. When an attack is carried out through frequent requests for a file, caching can greatly reduce the number of requests to the backend server.

  2. proxy_cache_key ` 命令定义的键值通常包含一些内嵌的变量(默认的键值 $scheme$proxy_host$request_uri 包含了三个变量)。如果键值包含 `$query_string` 变量,当攻击的请求字符串是随机的时候就会给 nginx 代理过重的缓存负担,因此我们建议一般情况下不要包含 `$query_string` 变量。

7. 屏蔽特定的请求

可以设置 nginx、nginx plus 屏蔽一些类型的请求:

  1. 针对特定 url 的请求

  2. 针对不是常见的 user-agent 的请求

  3. 针对 referer 头中包含可以联想到攻击的值的请求

  4. 针对其他请求头中包含可以联想到攻击的值的请求

比如,如果你判定攻击是针对一个特定的 url:/foo.php,我们就可以屏蔽到这个页面的请求:

location /foo.php { 
deny all; 
}

或者你判定攻击请求的 user-agent 中包含 foo 或 bar,我们也可以屏蔽这些请求:

location / { 
if ($http_user_agent ~* foo|bar) { 
return 403; 
} 
... 
}

http_name 变量引用一个请求头,上述例子中是 user-agent 头。可以针对其他的 http 头使用类似的方法来识别攻击。

8. 限制到后端服务器的连接数

一个 nginx、nginx plus 实例可以处理比后端服务器多的多的并发请求。在 nginx plus 中,你可以限制到每一个后端服务器的连接数,比如可以设置 nginx plus 与 website upstream 中的每个后端服务器建立的连接数不得超过200个:

upstream website { 
server 192.168.100.1:80 max_conns=200; 
server 192.168.100.2:80 max_conns=200; 
queue 10 timeout=30s; 
}

`max_conns` 参数可以针对每一个后端服务器设置 nginx plus 可以与之建立的最大连接数。`queue` 命令设置了当每个后端服务器都达到最大连接数后的队列大小,`timeout` 参数指定了请求在队列中的保留时间。

9. 处理特定类型的攻击

有一种攻击是发送包含特别大的值的请求头,引起服务器端缓冲区溢出。nginx、nginx plus 针对这种攻击类型的防御,可以参考

[using nginx and nginx plus to protect against cve-2015-1635]
)

10. 优化nginx性能

ddos 攻击通常会带来高的负载压力,可以通过一些调优参数,提高 nginx、nginx plus 处理性能,硬抗 ddos 攻击,详细参考:

[tuning nginx for performance]

三、识别ddos攻击

到目前为止,我们都是集中在如何是用 nginx、nginx plus 来减轻 ddos 攻击带来的影响。如何才能让 nginx、nginx plus 帮助我们识别 ddos 攻击呢?`nginx plus status module` 提供了到后端服务器流量的详细统计,可以用来识别异常的流量。nginx plus 提供一个当前服务状态的仪表盘页面,同时也可以在自定义系统或其他第三方系统中通过 api 的方式获取这些统计信息,并根据历史趋势分析识别非正常的流量进而发出告警。

The above is the detailed content of How to use Nginx and Nginx Plus to resist DDOS attacks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete