Linux环境下nginx集群搭建

#确保安装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上面新建端口号了

 

转载于:https://www.cnblogs.com/yangfan5157/p/10144360.html

你可能感兴趣的:(Linux环境下nginx集群搭建)