1 前言
Nacos配置集群后有多個地址,需要配置slb進行統一接口接入,由於開發代碼指定Tcp協議,故nginx轉發只能使用四層轉發,nginx需要stream模塊插件來實現。
1.1 安裝nginx
1.1.1 安裝Nginx
此處不再贅述nginx安裝步驟。
1.2 配置stream模塊
1.2.1 查看nginx目前模塊
/usr/local/nginx/sbin/nginx -V 默認沒有安裝
如圖:
1.2.2 添加模塊 stream
重新解壓同版本nginx安裝包,進入目錄進行編譯添加stream模塊
cd /usr/local/nginx-1.16.0
#需要把之前的參數帶上
./configure --prefix=/usr/local/nginx --with-pcre=/app/software/pcre-8.43 --with-zlib=/app/software/zlib-1.2.11 --with-openssl=/app/1.1.1c --with-http_ssl_module --with-stream
然后make (切忌不要輸入make install,否則會覆蓋掉nginx文件)
只有make后才會產生新的nginx文件
1.2.3 停止Nginx
使用以下命令停止運行的nginx服務:
ps -ef | grep nginx|grep -v grep|cut -c 9-15|xargs kill -9
1.2.4 拷貝Nginx啟動文件
先備份原本的nginx文件再拷貝:
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp /app/software/nginx-1.16.0/objs/nginx /usr/local/nginx/sbin/
1.2.5 啟動Nginx
/usr/local/nginx/sbin/nginx
查看依賴模塊 /usr/local/nginx/sbin/nginx -V
2 配置nginx四層tcp轉發nacos
2.1 修改Nginx的配置文件
修改Nginx.conf添加stream配置進行tcp轉發,注意,stream和http模塊同級別,千萬不要配置在http的{}內,否則將報錯stream
vi /usr/local/nginx/conf/nginx.conf 底部增加stream模塊,默認是沒有這個的,如果你的有就直接配置一個加載指定配置文件即可;
以下是加載的指定配置文件:
2.1.1 重新加載配置文件生效
/usr/local/nginx/sbin/nginx -s reload
2.1.2 校驗
校驗
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
2.1.3 啟動和停止
2、啟動
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
3、停止
/usr/local/nginx/sbin/nginx -s stop
3 附錄<常見報錯>
3.1 啟動nginx返回stream報錯
nginx: [emerg] "stream" directive is not allowed here in /usr/local/nginx/conf/nginx.conf:7
解決:stream模塊和http同級別,更改配置存放位置
3.2 加載nginx報錯
unknown directive “stream” in /usr/local/nginx
解決:這是因為缺少stream模塊,需要按照以上操作進行增加模塊,若是apt或者yum安裝的,則進行在線安裝模塊即可,此處不做贅述。