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报错

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
报错提示的意思是在nginx.conf配置文件的第53行出现了一个错误,错误是"server"指令在这里不被允许使用。这个错误导致了nginx的配置文件测试失败。\[1\] 这个错误通常是由于配置文件中的语法错误或者指令放置位置不正确引起的。要解决这个问题,可以按照以下步骤进行排查: 1. 首先,检查nginx.conf配置文件的第53行,确保"server"指令的使用位置正确。通常,"server"指令应该在http块内部使用,而不是在全局配置块中使用。 2. 检查配置文件中的其他语法错误。可能会有其他的指令或者语法错误导致了这个错误。可以使用nginx -t命令来测试配置文件的语法是否正确,如果有其他错误,会一并显示出来。\[2\] 3. 如果以上步骤都没有解决问题,可以尝试使用备份的配置文件或者重新安装nginx来解决。有时候,配置文件可能被损坏或者不完整,重新安装nginx可以解决这个问题。\[3\] 总结起来,当出现"nginx: \[emerg\] 'server' directive is not allowed here"的错误时,需要检查配置文件中的语法错误和指令位置是否正确,并可以尝试使用备份的配置文件或者重新安装nginx来解决问题。 #### 引用[.reference_title] - *1* *2* *3* [nginx: [emerg] “server“ directive is not allowed here in /usr/local/nginx/conf/nginx.conf53](https://blog.csdn.net/guguai7/article/details/118145007)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值