nginx安装tcp代理模块
最近公司有台服务器要求把tcp的请求分发到下面的redis服务器上去,我还以为就是http协议哦,没有想到经过确认后才知道是把tcp协议分发到下面去,这个真是让我很无语,还好nginx已经开发了tcp代理模块,不然我还真只有哭的命了.
系统:centos 5.x
需要的软件包:nginx_tcp_proxy_module
nginx-1.4.6
1.安装前准备
yum -y install gcc gcc-c++ ncurses-devel libxml2-devel \
openssl-devel curl-devel libjpeg-devel libpng-devel autoconf pcre-devel \
libtool-libs freetype-devel gd zlib-devel zip unzip \
wget crontabs iptables file bison cmake patch mlocate \
flex diffutils automake make readline-devel \
glibc-devel glibc-static glib2-devel bzip2-devel \
gettext-devel libcap-devel logrotate ntp \
libmcrypt-devel patch
wget --no-check-certificate https://github.com/yaoweibin/nginx_tcp_proxy_module/archive/master.zip
wget http://nginx.org/download/nginx-1.4.6.tar.gz
2.安装nginx_tcp_proxy_module
unzip master
tar zxf nginx-1.4.6.tar.gz && cd nginx-1.4.6
patch -p1 < /root/install/nginx_tcp_proxy_module-master/tcp.patch
./configure --add-module=/root/install/nginx_tcp_proxy_module-master
make && make install
3.配置
vi /etc/nginx/nginx.conf
ps:
tcp{...}与http{...}是在同一层级,不要把tcp加到http里去了,最开始我没有注意就加到http{...}里去了,造成nginx无法启动.
配置好后,重启nginx,用浏览器进行验证,例如:https://blog.slogra.com:8078,即可对tcp的连接请求进行分发了.
如果大家有兴趣还可以去看看这篇文章nginx tcp代理,他这篇文章里说了一个保持tcp连接的问题,这里我只把配置贴出来:
系统:centos 5.x
需要的软件包:nginx_tcp_proxy_module
nginx-1.4.6
1.安装前准备
yum -y install gcc gcc-c++ ncurses-devel libxml2-devel \
openssl-devel curl-devel libjpeg-devel libpng-devel autoconf pcre-devel \
libtool-libs freetype-devel gd zlib-devel zip unzip \
wget crontabs iptables file bison cmake patch mlocate \
flex diffutils automake make readline-devel \
glibc-devel glibc-static glib2-devel bzip2-devel \
gettext-devel libcap-devel logrotate ntp \
libmcrypt-devel patch
wget --no-check-certificate https://github.com/yaoweibin/nginx_tcp_proxy_module/archive/master.zip
wget http://nginx.org/download/nginx-1.4.6.tar.gz
2.安装nginx_tcp_proxy_module
unzip master
tar zxf nginx-1.4.6.tar.gz && cd nginx-1.4.6
patch -p1 < /root/install/nginx_tcp_proxy_module-master/tcp.patch
./configure --add-module=/root/install/nginx_tcp_proxy_module-master
make && make install
3.配置
vi /etc/nginx/nginx.conf
tcp { upstream web1 { server 133.2xx.82.2x:8078; server 133.2xx.83.1x:8078; check interval=3000 rise=2 fall=5 timeout=100; #这里间隔设置为1秒检查一次,启动nginx后,要等待1秒,以上配置的服务器状态才会标识为有效状态,也就是要1秒后scoket才能连接上.主要是开发要求的,本来是设置的10秒,但开发嫌太慢了,所以就改成1秒. } server { listen 8078; proxy_pass web1; } } http { include mime.types; default_type application/octet-stream; .......
ps:
tcp{...}与http{...}是在同一层级,不要把tcp加到http里去了,最开始我没有注意就加到http{...}里去了,造成nginx无法启动.
配置好后,重启nginx,用浏览器进行验证,例如:https://blog.slogra.com:8078,即可对tcp的连接请求进行分发了.
如果大家有兴趣还可以去看看这篇文章nginx tcp代理,他这篇文章里说了一个保持tcp连接的问题,这里我只把配置贴出来:
tcp { timeout 1d; proxy_read_timeout 10d; proxy_send_timeout 10d; proxy_connect_timeout 30; upstream cluster_www_ttlsa_com { # simple round-robin server 127.0.0.1:1234; check interval=3000 rise=2 fall=5 timeout=1000; #check interval=3000 rise=2 fall=5 timeout=1000 type=ssl_hello; #check interval=3000 rise=2 fall=5 timeout=1000 type=http; #check_http_send "GET / HTTP/1.0\r\n\r\n"; #check_http_expect_alive http_2xx http_3xx; }
评论: