参考:https://www.cnblogs.com/vijayfly/p/7663552.html
参考:https://blog.csdn.net/xqnode/article/details/83062033
因为socket
stream配置不能放到http内,即不能放到/etc/nginx/conf.d/,因为stream是通过tcp层转发,而不是http转发
# tcp流
stream {
# 添加socket转发的代理
upstream da_service {
#hash $remote_addr consistent; #均衡方式,使用远程ip的hash算法,ip一样的均衡到同一个机器
least_conn; # 均衡方式,当前连接数最少的server来处理
server 172.16.70.122:8908 weight=1;
server 172.16.70.123:8908 weight=1;
#ip_hash;
}
# 提供转发服务,即访问forward.xxx.cn:80,并配置好请求头,会调整至proxy_forward指定的地址
server {
#listen 127.0.0.1:80;
listen 9000;
proxy_pass da_service;
}
}
# 下面是http流,stream 不能放在http里面
http {
.......
}
nginx默认安装的时候无法加载流stream模块,需要在启动参数里加上–with-stream。
解决方法:
重新对源文件进行编译、安装,通过添加–with-stream参数指定安装stream模块。
./configure --with-stream
make & make install
再次检查nginx.conf配置文件,确认配置无语法错误后,再次尝试启动服务。
./nginx
这个时候就可以启动成功了。
如果需要获取远程的ip,可以打开协议proxy_protocol on;
server {
#listen 127.0.0.1:80;
listen 9000;
proxy_pass da_service;
proxy_protocol on; # 代理协议开启,把协议也发送过去
}
socket接收端可以发现第一行为协议,第二行开始才是接收到的信息
PROXY TCP4 172.18.1.211 172.16.70.143 62603 9001
第二行开始才是接收到的信息