Nginx Mirror 模块实现三套ES写入网关
# 概述
本文介绍如何使用 Nginx 的 Mirror 模块实现三套 Elasticsearch 集群的写入网关。通过主请求将数据写入主集群,同时将数据镜像到两个辅助集群,便于数据同步或调试。
# 配置详解
以下是 Nginx 配置文件的完整内容。
upstream es_1 {
server 10.10.10.81:9200;
keepalive 640; # 最大保持活动连接数
keepalive_requests 15000; # 每个连接允许处理的最大请求数
keepalive_timeout 3600s; # 长连接保持的最大时间
}
upstream es_2 {
server 10.10.11.61:9200;
keepalive 640; # 最大保持活动连接数
keepalive_requests 15000; # 每个连接允许处理的最大请求数
keepalive_timeout 3600s; # 长连接保持的最大时间
}
upstream es_3 {
server 10.10.12:9200;
keepalive 640; # 最大保持活动连接数
keepalive_requests 15000; # 每个连接允许处理的最大请求数
keepalive_timeout 3600s; # 长连接保持的最大时间
}
server {
listen 8000 reuseport;
location / {
mirror /mirror; # 将主请求镜像到第一个辅助集群
mirror /mirror2; # 将主请求镜像到第二个辅助集群
proxy_pass http://es_2;
proxy_buffering off; # 禁用代理响应缓冲
proxy_http_version 1.1;
proxy_set_header Connection ""; # 配合keepalive使用,确保持久连接
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
tcp_nopush on;
tcp_nodelay on;
# 优化连接超时
proxy_connect_timeout 50s;
proxy_send_timeout 100s;
proxy_read_timeout 100s;
proxy_buffer_size 1280k;
proxy_buffers 4 2560k;
proxy_busy_buffers_size 2560k;
proxy_temp_file_write_size 5120k;
}
location /mirror {
internal;
proxy_buffering off; # 禁用代理响应缓冲
proxy_pass http://es_1$request_uri;
proxy_http_version 1.1;
proxy_set_header Connection ""; # 配合keepalive使用,确保持久连接
# 优化连接超时
proxy_connect_timeout 50s;
proxy_send_timeout 100s;
proxy_read_timeout 100s;
proxy_buffer_size 1280k;
proxy_buffers 4 2560k;
proxy_busy_buffers_size 2560k;
proxy_temp_file_write_size 5120k;
}
location /mirror2 {
internal;
proxy_buffering off; # 禁用代理响应缓冲
proxy_pass http://es_3$request_uri;
proxy_http_version 1.1;
proxy_set_header Connection ""; # 配合keepalive使用,确保持久连接
# 优化连接超时
proxy_connect_timeout 50s;
proxy_send_timeout 100s;
proxy_read_timeout 100s;
proxy_buffer_size 1280k;
proxy_buffers 4 2560k;
proxy_busy_buffers_size 2560k;
proxy_temp_file_write_size 5120k;
}
}
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
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
# 配置要点
Upstream 模块配置
- 配置了三套 Elasticsearch 集群的
upstream
,分别命名为es_1
、es_2
和es_3
。 - 设置了
keepalive
和keepalive_requests
参数,提升连接复用效率。
- 配置了三套 Elasticsearch 集群的
主请求处理
- 主请求通过
proxy_pass
转发到主集群es_2
。 - 使用
mirror
指令将请求镜像到两个辅助集群。
- 主请求通过
镜像请求处理
location /mirror
和location /mirror2
用于处理镜像请求,配置了internal
指令以限制直接访问。
优化参数
- 通过
proxy_connect_timeout
、proxy_send_timeout
、proxy_read_timeout
等参数优化了连接超时设置。 - 配置了缓冲区大小,防止大请求导致的性能问题。
- 通过
# 总结
该配置充分利用了 Nginx 的 Mirror 模块和 Keepalive 功能,实现了高效的数据同步与分发。镜像功能不仅可以用于日志分析和调试,还能作为备份方案的辅助工具。
如需进一步优化,请根据实际流量和需求调整 keepalive
和缓冲区大小等参数。
上次更新: 12/20/2024
- 01
- Logstash迁移ES数据12-11
- 02
- Elastichsearch使用wildcard字段模糊匹配12-07