Nginx-报错总结

重启 nginx 时 nginx -s reload,报错信息如下:

1
2
nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)
# 没有 nginx.pid 这个文件,每次当我们停止 nginx 时(nginx -s stop),nginx 会把 /usr/local/var/run/ 路径下名为 nginx.pid 的文件删掉

可以直接启动 nginx,重新生成 nginx.pid 就可以了:

1
nginx

如果直接启动还是不可行,执行 nginx -t 查看 nginx 配置文件路径:

1
2
3
$ nginx -t
# nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

指定一下 conf 文件:

1
nginx -c /usr/local/etc/nginx/nginx.conf

再次重启 nginx -s reload ,就不会报错了。

net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK)

在开发过程中,遇到浏览器 ERR_CONTENT_LENGTH_MISMATCH 报错,在网上查找了资料,下面贴解决思路与办法。

解决办法

在开发过程中,遇到前端页面加载css,js或woff,ttf 文件的时候,经常出现 ERR_CONTENT_LENGTH_MISMATCH 的报错情况。但不是所有的js或css 报错,报错的文件较没报错的文件偏大。并且报错的文件也可以单独在浏览器中打开,所以排除了最简单的地址错误。前端项目是由nginx代理的,所以可以查看nginx的日志,看看有无线索。

查找nginx 错误日志文件

1. 查找nginx配置文件

1
ps -ef | grep nginx

结果如下:

1
2
3
4
5
6
7
8
9
www      16951 18739  0 16:15 ?        00:00:00 nginx: worker process
www 16952 18739 0 16:15 ? 00:00:00 nginx: worker process
www 16953 18739 0 16:15 ? 00:00:00 nginx: worker process
www 16954 18739 0 16:15 ? 00:00:00 nginx: worker process
www 16955 18739 0 16:15 ? 00:00:00 nginx: worker process
www 16956 18739 0 16:15 ? 00:00:00 nginx: worker process
www 16957 18739 0 16:15 ? 00:00:00 nginx: worker process
www 16958 18739 0 16:15 ? 00:00:00 nginx: worker process
root 18739 1 0 Sep20 ? 00:00:26 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

我的开发环境nginx配置文件路径为: /usr/local/nginx/conf/nginx.conf

2. 查看错误日志文件位置

1
cat /usr/local/nginx/conf/nginx.conf

我的开发环境nginx错误日志路径为:cnk_data/nginx_logs/error.log

3. 打开错误日志

根据出错的文件过滤一下(若文件过大,请稍等片刻):

1
cat /cnk_data/nginx_logs/error.log | grep "query.dataTables.min.js"

结果如下:

1
2019/10/16 16:11:37 [crit] 5878#0: *81035 mkdir() "/usr/local/openresty/nginx/proxy_temp/2/35" failed (13: Permission denied) while reading upstream, client: 123.116.114.84, server: sunct.goapi.youlai.cn, request: "GET /static/js/adminone/jquery.dataTables.min.js HTTP/1.1", upstream: "http://127.0.0.1:9092/static/js/adminone/jquery.dataTables.min.js", host: "sunct.goapi.youlai.cn", referrer: "http://sunct.goapi.youlai.cn/admin/index"

其中有一句 mkdir() Permission denied 错误:

2019/10/16 16:11:36 [crit] 5881#0: *80999 mkdir() “/usr/local/openresty/nginx/proxy_temp/1/35” failed (13: Permission denied) while reading upstream, client: 123.116.114.84, server: sunct.goapi.youlai.cn, request: “GET /static/js/adminone/jquery.dataTables.min.js HTTP/1.1”, upstream: “http://127.0.0.1:9092/static/js/adminone/jquery.dataTables.min.js”, host: “sunct.goapi.youlai.cn”, referrer: “http://sunct.goapi.youlai.cn/admin/index”

到此,可以得知是没有mkdir() 成功,结果因为没有权限,导致了请求失败,被拒绝。

那么,为什么nginx要访问 proxy_temp 文件夹呢?

因为 proxy_temp 是nginx的缓存文件夹,我的css和js文件过大了,所以nginx一般会从缓存里面去拿,而不是每次都去原地址直接加载。

4. 尝试解决

进入报错的路径,我的是 /usr/local/openresty/nginx/,查看文件夹proxy_temp 权限。

1
cd /usr/local/openresty/nginx

结果如下(注:这是改后的权限)

《浏览器报错 net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK) 解决办法》

根据个人情况,给proxy_temp 文件夹重新修改权限和组 即可。

1
2
chown www root proxy_temp
chmod -Rf 777 proxy_temp

《浏览器报错 net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK) 解决办法》

5. 重启 nginx 服务

根据自己的nginx 服务配置来重启即可,命令可能如下:

1
nginx -s reload

《浏览器报错 net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK) 解决办法》

6. 重新刷新浏览器

完美,一切正常!

参考文章

浏览器报错 net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK) 解决办法