最新历史版本 :Nginx常见报错及解决方案 返回文档
编辑时间: 内容长度:图片数:目录数: 修改原因:

目录:

1. 概述编辑

本文汇总使用 nginx 过程中常见报错及解决方案。

2. 示例编辑

2.1 permission denied

问题描述:

集群环境,有线网络正常,无线网络登录页面空白,但是节点可以正常登录。

日志文件如下图所示:

11.png

原因分析:

nginx 无权限读取数据流。

解决方案:

修改 nginx.conf ,将用户组改成 root ,并且取消注释。如下图所示:

1603095337682109.png

2.2 https protocol requires SSL

问题描述:

启动时报错:

nginx:[emerg]https protocol requires SSL support in /usr/nginx/conf/nginx.conf:39

原因分析:

nginx.conf 中配置了 https ,但 nginx 未配置 ssl 模块。

解决方案:

给 nginx 增加 ssl 模块,请参见 配置 SSL 证书实现 HTTPS 访问

2.3 nginx 编译缺乏依赖

问题描述:

报错:SSL modules require the OpenSSL library

如下图所示:

1603096371969177.png

原因分析:

nginx 在编译一些模块时,需要三个依赖库:prce、zlib、openssl

解决方案:


yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

2.4 nginx 入口访问平台空白

问题描述:

nginx 入口访问平台空白,日志显示写缓存文件失败,如下图所示:

13.png

解决方案:

如果启动 nginx 的用户有对应目录读写权限,则可能是磁盘空间不足引起,可用df -h指令查看。

nginx.conf 中关闭 acces.log ,避免日志过大,把磁盘空间占完。

2.5 集群环境/服务器经常出现卡慢

问题描述:

集群环境,服务器经常出现卡慢。

解决方案:

由于磁盘空间不足,nginx.conf 中关闭 acces.log ,避免日志过大,把磁盘空间占完。

2.6 nginx 配置外网后端口丢失

问题描述:

用户做了 Web 集群,现在做了外网映射,访问 URL 能登录到登录页面,但是点击登录之后,映射的端口号不见了,跳转之后就直接报 404 。

原因分析:

登录跳转后,host 变动,导致端口丢失。

解决方案:

nginx 修改 head 的 host 字段(proxy_set_header Host)为实际外部访问的地址,如下图所示:

14.png

2.7 nginx 分块请求异常

问题描述:

Linux 服务器集群,刷新 frm 模板,会出现部分模块加载不出的情况,每次加载不出的模块都是随机的。Dec.socket.connected返回 true ,但是前端有关于 38888 和 38889 的报错。

原因分析:

用户 nginx.conf 默认是用的是http1.0,tcp 连接生命周期不够,导致一些耗时较长的图表请求超时。

解决方案:

nginx 配置 http1.1 和 connection 头置空,响应分块请求。如下图所示:

15.png

2.8 nginx 配置无法访问 css 和 js 等静态资源

问题描述:

访问类似localhost:8080\WebReport\ReportServer,页面空白,控制台报错:Failed to load resource:the server responded with a status of 404(not Found)

16.png

原因分析:

nginx入口:http://localhost/reportService/WebReport/decision

Tomcat:http://ip:port/WebReport/decision

用户在 nginx 在匹配/reportService转发给后端 Tomcat 处理,而后端返回资源请求时不带/reportService,导致重定向到了 nginx 静态资源路径,访问报错。

nginx配置:

1603098476510572.png

18.png

解决方案:

方案一:nginx 层面

拦截/WebReport/decision,重定向为/reportService/WebReport/decision

需要注意以下两点:

  • rewrite 是 301 重定向,return 是 307 重定向。

  • server_name 为 localhost 时,通过 127.0.0.1 访问,重定向后会有跨域报错,ajax error。

19.png

方案二:Tomcat 层面

设置虚拟目录,使得 tomcat url 变为:http://ip:port/reportService/WebReport/decision

具体步骤

(1)将工程移出webapp,避免重复加载;

(2)修改tomcat conf/server.xml,docbase为工程绝对路径

20.png

(3)修改nginx.conf,去掉斜杠

注:带斜杠时,是将/reportService/后面的请求转发给后面。

21.png

2.9 模板挂载到决策平台不显示

问题描述:

模板直接在设计器预览是正常的;挂载到集群地址,模板空白;挂载到集群节点,模板正常展示。

原因分析:

外网有一个转发工具,转发到了设置在内网的 nginx ,然后通过代理到后台节点,内网访问也会经过 nginx 转发,但是由于转发工具的端口与 nginx 端口不一致,并且没有把外网地址的 port 正确传入,导致最后拿到的是 nginx 端口号,但是内网是直接经过 nginx 了,所以可以正常展示端口号

解决方案:

nginx 配置的端口监听直接改成与外网端口号一致,这样就避免了此类 port 不一致的情况。