#確保安裝nginx,stream模塊默認不安裝的,需要手動添加參數:–with-stream, nginx1.9或以上版本
#nginx.conf文件中,添加以下內容(只供參考),這個不能放在http模塊里面,否則會報錯
stream {
# 添加socket轉發的代理
upstream client_a {
hash $remote_addr consistent;
# 轉發的目的地址和端口
server ip1:6577 weight=5 max_fails=3 fail_timeout=30s;
server ip2:6588 weight=5 max_fails=3 fail_timeout=30s;
}
upstream client_b {
hash $remote_addr consistent;
# 轉發的目的地址和端口
server ip3:6577 weight=5 max_fails=3 fail_timeout=30s;
server ip4:6588 weight=5 max_fails=3 fail_timeout=30s;
}
# 提供轉發的服務,即訪問localhost:30001,會跳轉至代理bss_num_socket指定的轉發地址
server {
listen 30001;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass client_a;
access_log /soft/nginx/logs/access.log;
error_log /soft/nginx/logs/error.log;
}
server {
listen 30002;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass client_b;
access_log /soft/nginx/logs/access.log;
error_log /soft/nginx/logs/error.log;
}
}
#或者在tcp.d下新建個bss_num_30001.conf文件,然后在nginx.conf引用該文件
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
}
# tcp層轉發的配置文件夾
include /etc/nginx/tcp.d/*.conf;
#查看nginx進程
ps -ef|grep nginx
#關閉nginx
kill -9 進程號
#啟動nginx
/soft/nginx/nginx -c /soft/nginx/nginx.conf
#查看nginx端口占用情況,例如:30001與30002端口是否開啟
netstat -tunlp | grep 端口號
## 修改防火牆
vim /etc/sysconfig/iptables
添加對應端口號:
-A INPUT -p tcp -m tcp --dport 30001 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 30002 -j ACCEPT
# 重啟防火牆
service iptables restart
#在ip1服務器上新建端口,這個就是windows上面新建端口號了