51
#优化:Nginx防御DDOS和CC攻击
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
加载HTTP段
 
##
    #  基础配置
    ##
    keepalive_timeout    10;
    server_tokens off;
    types_hash_max_size  2048;
    ##
    # 主要配置
    ##
    sendfile     on;
    tcp_nopush   on;
    tcp_nodelay  on;
    open_file_cache            max=50000  inactive=20s;
    open_file_cache_valid      30s;
    open_file_cache_min_uses   2;
    open_file_cache_errors     on;
    reset_timedout_connection  on;
    client_body_timeout        10;
    send_timeout               2;
    ##
    # DDoS 和 CC 防御配置,主要是限制链接数,详细:http://nginx.org/en/docs/http/ngx_http_limit_conn_module.html
    ##
    client_body_buffer_size      128k;
    large_client_header_buffers  4 32k;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    client_max_body_size 50m;
 
    limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;
    limit_req_zone $binary_remote_addr zone=req_limit_per_ip:10m rate=50r/s;
    limit_conn conn_limit_per_ip 20;
    limit_req zone=req_limit_per_ip burst=20;
 
 
完整的nginx配置文件
[root@panni ~]# cat /etc/nginx/nginx.conf
user  nobody;
worker_processes  auto;
 
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;
worker_rlimit_nofile 1024;
 
 
events {
    use epoll;
    worker_connections  1024;
    multi_accept on;
 
}
 
 
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
     
    ##
    # 基础配置
    ##
    keepalive_timeout    10;
    server_tokens off;
    types_hash_max_size  2048;
    ##
    # 主要配置
    ##
    sendfile     on;
    tcp_nopush   on;
    tcp_nodelay  on;
    open_file_cache            max=50000  inactive=20s;
    open_file_cache_valid      30s;
    open_file_cache_min_uses   2;
    open_file_cache_errors     on;
    reset_timedout_connection  on;
    client_body_timeout        10;
    send_timeout               2;
    ##
    # DDoS 和 CC 防御配置,主要是限制链接数,详细:http://nginx.org/en/docs/http/ngx_http_limit_conn_module.html
    ##
    client_body_buffer_size      128k;
    large_client_header_buffers  4 32k;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    client_max_body_size 50m;
 
    limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;
    limit_req_zone $binary_remote_addr zone=req_limit_per_ip:10m rate=50r/s;
    limit_conn conn_limit_per_ip 20;
    limit_req zone=req_limit_per_ip burst=20;
 
    fastcgi_connect_timeout 300; #如果你不使用FastCGI,请用井号注释该段每一行
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 256k;
 
    gzip on; #如果你不使用GZip,请用井号注释该段每一行
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types       text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary off;
    gzip_proxied        expired no-cache no-store private auth;
    gzip_disable        "MSIE [1-6]\.";
 
    include /etc/nginx/conf.d/*.conf;
}

 


这条帮助是否解决了您的问题? 已解决 未解决

提交成功!非常感谢您的反馈,我们会继续努力做到更好! 很抱歉未能解决您的疑问。我们已收到您的反馈意见,同时会及时作出反馈处理!