编译参数
--prefix=/usr/local/nginx --with-pcre --user=daemon --group=daemon --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-stream --with-http_gzip_static_module
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
stream {
log_format proxy '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time "$upstream_addr" '
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"'
'$remote_addr $remote_port';
map $bytes_received $loggable {
0 0;
default 1;
}
access_log /usr/local/nginx/logs/tcp-access.log proxy if=$loggable;
open_log_file_cache off;
upstream backend {
server 8.132.3.23:2222;
}
server {
listen 12236;
proxy_pass backend;
}
}
HAproxy
###########全局配置#########
global
daemon #以后台形式运行harpoxy
nbproc 1 #设置进程数量
pidfile /usr/local/haproxy/conf/haproxy.pid #进程PID文件
########默认配置############
defaults
mode tcp #设置HAProxy默认的工作模式,有tcp,http,health三种
retries 2 #两次连接失败就认为是服务器不可用,也可以通过后面设置
option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器,以后将不支持
option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
timeout connect 5000ms #连接超时
timeout client 30000ms #客户端超时
timeout server 30000ms #服务器超时
timeout check 2000 #心跳检测超时
log 127.0.0.1 local0 err
listen http_front #haproxy的客户页面
bind 0.0.0.0:18888
mode http
option httplog
stats uri /haproxy
stats auth admin:12300admin
stats refresh 5s
stats enable
########统计页面配置########
listen test1 #这里是配置负载均衡,test1是名字,可以任意
option forwardfor
bind 0.0.0.0:12348 #这里是监听的IP地址和端口,端口号可以在0-65535之间,要避免端口冲突
mode tcp #连接的协议,这里是tcp协议
server s1 192.168.8.12:5222
systemctl status haproxy
systemctl restart haproxy
将服务端13.159..23.9端口转发到本地电脑上18888端口上
ssh -L18888:127.0.0.1:18888 root@113.159..23.9
http://127.0.0.1:18888/haproxy