# 主配置文件 nginx.conf
events {
worker_connections 1024;
}
# Stream模块配置块
stream {
# 共享配置
log_format proxy '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time "$upstream_addr"';
access_log /var/log/nginx/stream-access.log proxy;
# 上游服务器组
upstream backend_tcp {
zone backend_tcp 64k;
server 192.168.1.10:3306 weight=3;
server 192.168.1.11:3306 weight=2;
server 192.168.1.12:3306 weight=1 backup;
}
upstream backend_udp {
server 192.168.1.20:53;
server 192.168.1.21:53;
}
# TCP服务配置
server {
listen 3306;
proxy_pass backend_tcp;
proxy_connect_timeout 3s;
proxy_timeout 1h;
# SSL透传或终止
# ssl_preread on;
# proxy_ssl on;
}
# UDP服务配置
server {
listen 53 udp reuseport;
proxy_pass backend_udp;
proxy_timeout 3s;
proxy_responses 1;
}
# 负载均衡算法示例
upstream lb_example {
# 轮询(默认)
server 192.168.1.30:6379;
# 最少连接数
least_conn;
server 192.168.1.31:6379;
# Hash
hash $remote_addr consistent;
server 192.168.1.32:6379;
}
}
# TCP监听
listen 1935; # 默认TCP
listen 1935 ssl; # TCP+SSL
listen 1935 http2; # HTTP/2
listen [::1]:1935 ipv6only=on; # IPv6
# UDP监听
listen 53 udp; # UDP协议
listen 53 udp reuseport; # 端口重用(提升性能)
# 参数说明:
# reuseport - Linux 3.9+,多个worker独立socket,减少锁竞争
# so_keepalive - TCP keepalive
# backlog - 连接队列大小
2.2.2 代理指令
proxy_pass upstream_name; # 转发到上游组
proxy_pass 192.168.1.100:8080; # 直接转发
# 超时控制
proxy_connect_timeout 10s; # 连接上游超时
proxy_timeout 24h; # 连接空闲超时
proxy_upload_rate 100k; # 上传速率限制
proxy_download_rate 1m; # 下载速率限制
# 缓冲控制
proxy_buffer_size 16k; # 缓冲区大小
2.2.3 SSL/TLS处理
# SSL终止(解密后再转发)
server {
listen 443 ssl;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
ssl_protocols TLSv1.2 TLSv1.3;
proxy_pass backend;
}
# SSL透传(不解密直接转发)
server {
listen 443;
ssl_preread on; # 预读SSL信息
proxy_pass $ssl_preread_server_name; # 基于SNI转发
}
# 上游SSL加密
proxy_ssl on;
proxy_ssl_certificate /path/to/client.crt;
proxy_ssl_certificate_key /path/to/client.key;
proxy_ssl_protocols TLSv1.2 TLSv1.3;
proxy_ssl_verify on;
proxy_ssl_trusted_certificate /path/to/ca.crt;
stream {
upstream mysql_masters {
zone mysql_masters 64k;
server db1.example.com:3306 weight=5;
server db2.example.com:3306 weight=3;
server db3.example.com:3306 weight=2;
}
upstream mysql_slaves {
least_conn;
server slave1.example.com:3306;
server slave2.example.com:3306;
server slave3.example.com:3306;
}
# 读写分离(基于端口)
server {
listen 3307; # 读端口
proxy_pass mysql_slaves;
proxy_connect_timeout 3s;
}
server {
listen 3308; # 写端口
proxy_pass mysql_masters;
proxy_connect_timeout 3s;
}
# 健康检查
match mysql_check {
send "\x00\x00\x00\x0a\x40\x00\x00\x00\x00\x00\x00\x00";
expect ~* "MySQL";
}
}
stream {
# 一致性Hash分片
upstream redis_shard_0 {
server 192.168.1.100:6379;
}
upstream redis_shard_1 {
server 192.168.1.101:6379;
}
server {
listen 6379;
# 基于key分片
set $redis_shard $arg_key;
hash $redis_shard consistent;
location @shard_0 {
proxy_pass redis_shard_0;
}
location @shard_1 {
proxy_pass redis_shard_1;
}
}
}
stream {
# SSH访问控制
map $remote_addr $allow_ssh {
default 0;
10.0.0.0/8 1;
192.168.0.0/16 1;
}
server {
listen 22;
# IP白名单
if ($allow_ssh = 0) {
return;
}
# 基于用户名的路由
proxy_ssl_preread on;
ssl_preread on;
# SSH协议解析
preread_buffer_size 1k;
proxy_pass ssh_backend;
}
upstream ssh_backend {
server 10.1.1.100:22;
server 10.1.1.101:22 backup;
}
}
stream {
# UDP游戏服务器
upstream game_servers {
server 10.0.1.100:7777;
server 10.0.1.101:7777;
}
server {
listen 7777 udp reuseport;
proxy_pass game_servers;
proxy_timeout 10s;
# 会话保持
hash $remote_addr consistent;
}
# 状态统计
server {
listen 8080;
return 200 "Active connections: $connections\n";
}
}
stream {
# 全局配置
worker_processes auto; # CPU核心数
worker_rlimit_nofile 65535; # 文件描述符限制
# 事件模块
events {
use epoll; # Linux推荐
worker_connections 20480; # 每个worker连接数
multi_accept on;
}
# TCP优化
tcp_nodelay on; # 禁用Nagle算法
tcp_nopush on;
# 缓冲区优化
proxy_buffer_size 4k;
proxy_buffers 8 4k;
# 连接池
resolver 8.8.8.8 valid=30s;
resolver_timeout 3s;
}
stream {
# 主动健康检查
upstream backend {
zone backend 64k;
server 192.168.1.10:3306 max_fails=3 fail_timeout=30s;
server 192.168.1.11:3306 max_fails=3 fail_timeout=30s;
# 定制检查
match tcp_check {
send "PING\r\n";
expect ~* "PONG";
interval 10s;
passes 2;
fails 3;
}
}
# 被动健康检查
server {
proxy_pass backend;
proxy_next_upstream on;
proxy_next_upstream_timeout 0;
proxy_next_upstream_tries 2;
}
}
stream {
# 自定义日志格式
log_format stream_detail '$remote_addr - $remote_user [$time_local] '
'"$protocol" $status $bytes_sent $bytes_received '
'$session_time "$upstream_addr" '
'"$upstream_bytes_sent" "$upstream_bytes_received" '
'$upstream_connect_time';
access_log /var/log/nginx/stream.log stream_detail buffer=32k flush=5s;
error_log /var/log/nginx/stream_error.log warn;
# 状态统计
server {
listen 8081;
return 200 "
Upstream: $upstream_addr\n
Status: $status\n
Session Time: $session_time\n
Bytes Sent: $bytes_sent\n
Bytes Received: $bytes_received\n
";
}
}
stream {
# IP白名单
allow 192.168.1.0/24;
allow 10.0.0.0/8;
deny all;
# 连接限制
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
server {
listen 3306;
limit_conn conn_limit 10; # 每个IP最多10个连接
limit_conn_status 444; # 关闭连接不响应
# 速率限制
proxy_upload_rate 100k;
proxy_download_rate 500k;
}
# GeoIP限制
geo $deny_country {
default 0;
CN 1; # 允许中国
US 1; # 允许美国
}
server {
if ($deny_country = 0) {
return;
}
}
}
stream {
server {
listen 443 ssl;
# 强密码套件
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;
# 安全协议
ssl_protocols TLSv1.2 TLSv1.3;
# HSTS
add_header Strict-Transport-Security "max-age=31536000" always;
# OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
proxy_pass backend;
}
}
# mysql-proxy.conf
stream {
# 日志配置
log_format mysql_log '$remote_addr [$time_local] "$protocol" '
'upstream: $upstream_addr '
'bytes: $bytes_sent/$bytes_received '
'time: $upstream_connect_time/$session_time';
access_log /var/log/nginx/mysql-proxy.log mysql_log;
# 健康检查
match mysql_health {
# MySQL ping包
send "\x0a\x00\x00\x00\x0a\x53\x45\x4c\x45\x43\x54\x20\x31\x00";
expect ~* "MySQL";
interval 5s;
passes 2;
fails 3;
}
# 主库组
upstream mysql_master {
zone mysql_master 64k;
server 192.168.1.100:3306 max_fails=3 fail_timeout=30s;
server 192.168.1.101:3306 backup;
}
# 从库组
upstream mysql_slave {
zone mysql_slave 64k;
least_conn;
server 192.168.1.102:3306 weight=3;
server 192.168.1.103:3306 weight=2;
server 192.168.1.104:3306 weight=1;
}
# 写端口
server {
listen 3306;
# 连接限制
limit_conn mysql_conn 50;
# 超时设置
proxy_connect_timeout 2s;
proxy_timeout 3600s;
# 失败重试
proxy_next_upstream on;
proxy_next_upstream_timeout 1s;
proxy_next_upstream_tries 2;
proxy_pass mysql_master;
# 监控端点
status_zone mysql_master_proxy;
}
# 读端口
server {
listen 3307;
# 会话保持(同一客户端路由到同一从库)
hash $remote_addr consistent;
proxy_pass mysql_slave;
# 监控端点
status_zone mysql_slave_proxy;
}
# 管理端口
server {
listen 8080;
location /status {
stream_status;
access_log off;
}
location /stats {
prometheus;
access_log off;
}
}
}
# 1. 检查配置
nginx -t -c /etc/nginx/nginx.conf
# 2. 调试模式运行
nginx -g "daemon off; error_log stderr debug;"
# 3. 连接测试
nc -zv proxy_host 3306
telnet proxy_host 3306
# 4. 查看连接状态
ss -tnp | grep nginx
netstat -tnp | grep nginx
# 5. 实时日志监控
tail -f /var/log/nginx/stream-access.log
tail -f /var/log/nginx/error.log
# 6. 性能监控
nginx -T | grep stream # 查看完整配置
连接超时:
proxy_connect_timeout 5s;
proxy_timeout 300s;
连接数限制:
worker_rlimit_nofile 65535;
events {
worker_connections 20480;
}
内存不足:
proxy_buffer_size 4k;
proxy_buffers 8 4k;
架构设计:
安全建议:
性能优化:
reuseport减少锁竞争高可用:
监控告警:
通过合理使用Nginx Stream模块,可以构建高性能、高可用的四层代理服务,满足数据库、游戏、实时通信等各种场景的需求。