nginx [emerg] “stream“ directive is not allowed here in

先说 下背景,公司内部需要用到nginx反向代理ssh端口,需要用到stream进行配置,网上通常配置为:

#注意stream代码块要和http代码块同级。
#通常加到nginx.conf文件中
http {
    ....
}
stream {
    upstream ssh-proxy {
      server 需要代理的ip:22;
    }
    server {
      listen 8019;
      proxy_pass ssh-proxy;
    }
}

并且,通常在安装nginx时,默认不会加载stream模块,需要在安装nginx后,重新对nginx文件进行编译,添加stream模块,所以在nginx.conf文件添加了以上配置后会出现:nginx: [emerg] “stream” directive is not allowed here in报错,此时就需要添加stream模块

以本次需要的stream模块为例,编译方式为:

先进入到安装nginx时,源码包处,找到configure文件,这个文件位置根据个人安装习惯,位置也不会相同,我的源码路径为:/root/nginx/nginx-1.16.1/ 安装路径为:/opt/nginx/
**注意:**在编译文件前,需要先保存目前nginx有编译哪些模块

cd /opt/nginx/sbin/
./nginx -V
#以下为nginx目前编译信息
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2l  25 May 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx --user=www --group=www --with-http_stub_status_module --with-file-aio --with-http_ssl_module --with-pcre=/root/nginx-1.16.1/pcre-8.40 --with-openssl=/root/nginx-1.16.1/openssl-1.0.2l --with-zlib=/root/nginx-1.16.1/zlib-1.2.11 --add-module=/root/nginx-1.16.1/nginx_upstream_check_module-master --with-http_realip_module

加入需要添加的模块:–with-stream

cd /root/nginx/nginx-1.16.1
./configure --prefix=/opt/nginx --user=www --group=www --with-http_stub_status_module --with-file-aio --with-http_ssl_module --with-pcre=/root/nginx-1.16.1/pcre-8.40 --with-openssl=/root/nginx-1.16.1/openssl-1.0.2l --with-zlib=/root/nginx-1.16.1/zlib-1.2.11 --add-module=/root/nginx-1.16.1/nginx_upstream_check_module-master --with-http_realip_module --with-stream

编译文件

当前目录下(/root/nginx/nginx-1.16.1)执行:make

注意:千万不要执行 make install 不然就GG了,会将此前编译安装好的nginx进行覆盖

执行完编译后,将新的nginx文件替换安装目录下的nginx文件

#替换前,先对安装目录下的nginx做个备份
cp /opt/nginx/sbin/nginx /opt/nginx/sbin/nginx.bak20220428
#替换旧的nginx文件
cp ./objs/nginx /otp/nginx/sbin/nginx

此时在对安装目录下的nginx文件检查编译情况,可以看见新的stream模块已经加入到nginx下了

/opt/nginx/sbin/nginx -V

#以下为新的编译详情
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2l  25 May 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx --user=www --group=www --with-http_stub_status_module --with-file-aio --with-http_ssl_module --with-pcre=/root/nginx-1.16.1/pcre-8.40 --with-openssl=/root/nginx-1.16.1/openssl-1.0.2l --with-zlib=/root/nginx-1.16.1/zlib-1.2.11 --add-module=/root/nginx-1.16.1/nginx_upstream_check_module-master --with-http_realip_module --with-stream

此时再次执行/opt/nginx/sbin/nginx -t

就不会出现nginx: [emerg] “stream” directive is not allowed here in报错

扫描二维码关注公众号,回复: 14433658 查看本文章

先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

猜你喜欢

转载自blog.csdn.net/m0_67403188/article/details/126113704