安装nginx1.16.1
yum install gcc-c++ pcre* openssl* -y
mkdir /opt
cd /opt
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --prefix=/opt/nginx --with-http_ssl_module --with-stream
make&&make install
多实例nginx启动需指定配置文件
/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
编辑nginx socket长连接
vim /opt/nginx/conf/nginx.conf
upstream shuiwu {
keepalive 300;
server 2.16.124.223:81;
}
server {
location / {
proxy_pass http://shuiwu/;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_read_timeout 600s
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
}
}
nginx自1.9.0开始提供tcp/udp的反向代理功能,直到1.11.4才开始提供session日志功能。启用stream日志配置文件
主配置文件/etc/nginx/nginx.conf增加内容:
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"';
access_log /opt/nginx/logs/tcp-access.log proxy ;
open_log_file_cache off;
include /etc/nginx/conf.d/*.stream;
}
upstream TCP59001 {
hash $remote_addr consistent;
server 192.168.1.176:59001;
}
server {
listen 59001;
proxy_connect_timeout 5s;
proxy_timeout 30s;
proxy_pass TCP59001;
}
/opt/nginx/sbin/nginx -t
/opt/nginx/sbin/nginx
日志目录
tailf /opt/nginx/logs/tcp-access.log