nginx可以做tcp转发
如果我们的nginx是编译安装的话,需要带上./configure --with-stream
这里我用的是淘宝的tengine
一,安装编译tengine
cd /src
wget http://tengine.taobao.org/download/tengine-2.3.3.tar.gz
tar zxvf tengine-2.3.3.tar.gz
cd tengine-2.3.3/
yum install -y pcre pcre-devel openssl openssl-devel
./configure --with-stream
make
make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx
二,做成用systemctl管控nginx
vim /etc/systemd/system/nginx.service
#输入
[Unit]
Description=The Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl start nginx && systemctl enable nginx
三,配置nginx,以下是示例:
#nginx主配置文件里:
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 logs/tcp-access.log proxy ;
open_log_file_cache off;
# 统一放置,方便管理
include tcpconf/*.conf;
}
# 新建tcpconf目录,下面的配置如下
upstream gitlab59878 {
server 10.0.0.53:59878;
}
server {
listen 59878;
proxy_connect_timeout 8s;
proxy_timeout 24h;
proxy_pass gitlab59878;
}