nginx搭建tcp代理服務(wù)器
nginx不僅可以是http代理服務(wù)器,也可以輕松搭建成tcp代理服務(wù)器。
首先我們看下最新開發(fā)版的搭建方法
1. 安裝
> wget http://nginx.org/download/nginx-1.9.0.tar.gz > tar zxvf nginx-1.9.0.tar.gz
版本要求 1.9.0+
2、配置
worker_processes auto; error_log /var/log/nginx/error.log info; stream { upstream backend { hash $remote_addr consistent; server backend1.example.com:12345 weight=5; server 127.0.0.1:12345 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3; } server { listen 12345; proxy_connect_timeout 1s; proxy_timeout 3s; proxy_pass backend; } server { listen [::1]:12345; proxy_pass unix:/tmp/stream.socket; } }
3. 補(bǔ)充
現(xiàn)在nginx 1.9是開發(fā)版,目前穩(wěn)定版沒有stream的功能,但在下個(gè)的穩(wěn)定版發(fā)布時(shí),這功能就會(huì)集成進(jìn)來。因此推薦以后用http proxy的同學(xué)可以考慮換成tcp proxy,如果只是做簡單的代理而已,而且性能上會(huì)更優(yōu)異。
二、老版本的搭建方法
nginx tcp代理功能由nginx_tcp_proxy_module模塊提供,同時(shí)監(jiān)測后端主機(jī)狀態(tài)。該模塊包括的模塊有: ngx_tcp_module, ngx_tcp_core_module, ngx_tcp_upstream_module, ngx_tcp_proxy_module, ngx_tcp_upstream_ip_hash_module。
1. 安裝
# wget http://nginx.org/download/nginx-1.4.4.tar.gz # tar zxvf nginx-1.4.4.tar.gz # cd nginx-1.4.4 # ./configure --add-module=/path/to/nginx_tcp_proxy_module # make # make install
2. 配置
http { listen 80; location /status { check_status; } } tcp { 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; } server { listen 8888; proxy_pass cluster_www_ttlsa_com; } }
這會(huì)出現(xiàn)一個(gè)問題,就是tcp連接會(huì)掉線。原因在于當(dāng)服務(wù)端關(guān)閉連接的時(shí)候,客戶端不可能立刻發(fā)覺連接已經(jīng)被關(guān)閉,需要等到當(dāng)Nginx在執(zhí)行check規(guī)則時(shí)認(rèn)為服務(wù)端鏈接關(guān)閉,此時(shí)nginx會(huì)關(guān)閉與客戶端的連接。
3. 保持連接配置
http { listen 80; location /status { check_status; } } 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; } server { listen 8888; proxy_pass cluster_www_ttlsa_com; so_keepalive on; tcp_nodelay on; } }
nginx_tcp_proxy_module模塊指令具體參見: http://yaoweibin.github.io/nginx_tcp_proxy_module/README.html
相關(guān)文章
nginx配置多個(gè)前端項(xiàng)目實(shí)現(xiàn)步驟
本文主要介紹了nginx配置多個(gè)前端項(xiàng)目實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Nginx實(shí)現(xiàn)Nacos反向代理的項(xiàng)目實(shí)踐
在日常的web網(wǎng)站部署中,經(jīng)常會(huì)用到nginx反向代理,本文主要介紹了Nginx實(shí)現(xiàn)Nacos反向代理的項(xiàng)目實(shí)踐,Nginx實(shí)現(xiàn)Nacos反向代理的項(xiàng)目實(shí)踐2022-03-03使用nginx部署前端項(xiàng)目的實(shí)現(xiàn)
前端項(xiàng)目的部署以前一直是把靜態(tài)資源放到后端工程中,隨后端部署一起部署,本文主要介紹了使用nginx部署前端項(xiàng)目的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10關(guān)于nginx 實(shí)現(xiàn)jira反向代理的問題
這篇文章主要介紹了關(guān)于nginx 實(shí)現(xiàn)jira反向代理的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Nginx+uwsgi+ssl配置https的詳細(xì)步驟
nginx是一個(gè)輕量級(jí)的web服務(wù)器,在處理靜態(tài)資源和高并發(fā)有優(yōu)勢(shì),uwsgi是一個(gè)基于python的高效率的協(xié)議,處理后端和動(dòng)態(tài)網(wǎng)頁有優(yōu)勢(shì),我這里使用的是Ubuntu18.04版本,服務(wù)器在阿里云,感興趣的朋友跟隨小編一起看看吧2023-10-10Nginx層面配置基礎(chǔ)用戶驗(yàn)證的完整步驟
這篇文章主要給大家介紹了關(guān)于Nginx層面配置基礎(chǔ)用戶驗(yàn)證的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Nginx具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07