nginx四层负载均衡配置模板

时间:2022-12-21 18:57:46

一、模板

1、nginx模板
user nginx nginx;
#cpu核数上百,设置成auto最方便
worker_processes auto;
worker_cpu_affinity auto;
error_log logs/error.log;
worker_rlimit_nofile 102400;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 102400;
}

stream {
log_format proxy '$remote_addr $remote_port - $msec - [$time_local] $status $protocol'
'"$upstream_addr" "$upstream_bytes_sent" "$session_time"';
access_log /usr/local/nginx/logs/proxy.log proxy buffer=64k flush=10m;
open_log_file_cache max=1000 inactive=60s min_uses=3 valid=10m;
upstream sftp_srv {
hash $remote_addr consistent;
server 192.168.10.131:22 max_fails=3 fail_timeout=60s;
server 192.168.10.132:22 max_fails=3 fail_timeout=60s;
server 192.168.10.133:22 max_fails=3 fail_timeout=60s;
}
server {
listen 8080;
proxy_pass sftp_srv;
proxy_timeout 300s;
proxy_connect_timeout 300s;
}
}

2、nginx日志切割模板
vi /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log {
create 0640 nginx nginx
daily
rotate 15
missingok
notifempty
compress
delaycompress
dateext
dateformat -%Y%m%d%H
sharedscripts # 所有的文件切割之后只执行一次下面脚本
postrotate
if [ -f /usr/local/nginx/logs/nginx.pid ]; then
/bin/kill -USR1 `cat /usr/local/nginx/logs/nginx.pid 2>/dev/null` 2>/dev/null || true
fi
endscript
}
chmod 644 /etc/logrotate.d/nginx

二、解释说明

  log_format proxy 
'$remote_addr $remote_port - $msec - [$time_local] $status $protocol'
'"$upstream_addr" "$upstream_bytes_sent" "$session_time"';
$remote_addr 远程客户端地址
$remote_port 远程客户端随机端口
$msec 毫秒
$time_local 时间
$status 状态
$protocol 协议
$upstream_addr 虚拟连接池的主机地址
$upstream_bytes_sent 大小
$session_time 连接时间