search
HomeOperation and MaintenanceNginxHow to use Nginx and Nginx Plus to resist DDOS attacks

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:亿速云. If there is any infringement, please contact admin@php.cn delete
NGINX Unit's Purpose: Running Web ApplicationsNGINX Unit's Purpose: Running Web ApplicationsApr 30, 2025 am 12:06 AM

The purpose of NGINXUnit is to simplify the deployment and management of web applications. Its advantages include: 1) Supports multiple programming languages, such as Python, PHP, Go, Java and Node.js; 2) Provides dynamic configuration and automatic reloading functions; 3) manages application lifecycle through a unified API; 4) Adopt an asynchronous I/O model to support high concurrency and load balancing.

NGINX: An Introduction to the High-Performance Web ServerNGINX: An Introduction to the High-Performance Web ServerApr 29, 2025 am 12:02 AM

NGINX started in 2002 and was developed by IgorSysoev to solve the C10k problem. 1.NGINX is a high-performance web server, an event-driven asynchronous architecture, suitable for high concurrency. 2. Provide advanced functions such as reverse proxy, load balancing and caching to improve system performance and reliability. 3. Optimization techniques include adjusting the number of worker processes, enabling Gzip compression, using HTTP/2 and security configuration.

NGINX vs. Apache: A Look at Their ArchitecturesNGINX vs. Apache: A Look at Their ArchitecturesApr 28, 2025 am 12:13 AM

The main architecture difference between NGINX and Apache is that NGINX adopts event-driven, asynchronous non-blocking model, while Apache uses process or thread model. 1) NGINX efficiently handles high-concurrent connections through event loops and I/O multiplexing mechanisms, suitable for static content and reverse proxy. 2) Apache adopts a multi-process or multi-threaded model, which is highly stable but has high resource consumption, and is suitable for scenarios where rich module expansion is required.

NGINX vs. Apache: Examining the Pros and ConsNGINX vs. Apache: Examining the Pros and ConsApr 27, 2025 am 12:05 AM

NGINX is suitable for handling high concurrent and static content, while Apache is suitable for complex configurations and dynamic content. 1. NGINX efficiently handles concurrent connections, suitable for high-traffic scenarios, but requires additional configuration when processing dynamic content. 2. Apache provides rich modules and flexible configurations, which are suitable for complex needs, but have poor high concurrency performance.

NGINX and Apache: Understanding the Key DifferencesNGINX and Apache: Understanding the Key DifferencesApr 26, 2025 am 12:01 AM

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

NGINX Unit: Key Features and CapabilitiesNGINX Unit: Key Features and CapabilitiesApr 25, 2025 am 12:17 AM

NGINXUnit is an open source application server that supports multiple programming languages ​​and provides functions such as dynamic configuration, zero downtime updates and built-in load balancing. 1. Dynamic configuration: You can modify the configuration without restarting. 2. Multilingual support: compatible with Python, Go, Java, PHP, etc. 3. Zero downtime update: Supports application updates that do not interrupt services. 4. Built-in load balancing: Requests can be distributed to multiple application instances.

NGINX Unit vs. Other Application ServersNGINX Unit vs. Other Application ServersApr 24, 2025 am 12:14 AM

NGINXUnit is better than ApacheTomcat, Gunicorn and Node.js built-in HTTP servers, suitable for multilingual projects and dynamic configuration requirements. 1) Supports multiple programming languages, 2) Provides dynamic configuration reloading, 3) Built-in load balancing function, suitable for projects that require high scalability and reliability.

NGINX Unit: The Architecture and How It WorksNGINX Unit: The Architecture and How It WorksApr 23, 2025 am 12:18 AM

NGINXUnit improves application performance and manageability with its modular architecture and dynamic reconfiguration capabilities. 1) Modular design includes master processes, routers and application processes, supporting efficient management and expansion. 2) Dynamic reconfiguration allows seamless update of configuration at runtime, suitable for CI/CD environments. 3) Multilingual support is implemented through dynamic loading of language runtime, improving development flexibility. 4) High performance is achieved through event-driven models and asynchronous I/O, and remains efficient even under high concurrency. 5) Security is improved by isolating application processes and reducing the mutual influence between applications.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function