nginx try_files引起500错误的解决过程

Nginx 500错误(Internal Server Error内部服务器错误):500错误指的是服务器内部错误,也就是说服务器遇到意外情况,而无法履行请求。


最近在更新版本时,发现Nginx error日志不停在打印rewrite or internal redirection cycle while internally redirecting to..,但是服务器又存在这些静态文件,以下是分析步骤:

1.查看报错日志

2019/08/16 00:42:01 [error] 1801#1801: *60690 rewrite or internal redirection cycle while internally redirecting to "/tcoinShop-h5/credit_h5/index.html", client: 100.12
0.34.90, server: tcoin.trc.com, request: "GET /credit_h5/static/js/1.bddd748718eee4b77012.js HTTP/1.1", host: "tcoin.trc.com", referrer: "https://tcoin.trc.com/credit_h5/"

发现是很多请求/credit_h5/static/js/1.bddd748718eee4b77012.js时的报错,由https://tcoin.trc.com/credit_h5/请求;

2.查看该文件是否存在
去服务器上查询该文件并不存在,但是请求的就是这个地址;

3.reload nginx
怀疑是nginx配置文件没有生效,重新reload的一遍,发现该现象仍然存在;

4.ping 域名

[root@MI ~]# ping tcoin.trc.com
PING tcoin.trc.com.m.alikunlun.com (122.228.234.240) 56(84) bytes of data.
^C64 bytes from 122.228.234.240: icmp_seq=1 ttl=56 time=7.75 ms

--- tcoin.trc.com.m.alikunlun.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 7.750/7.750/7.750/0.000 ms

发现该域名过了CDN,然后怀疑是CDN缓存的问题,清理一下CDN的缓存;

5.错误日志变得很少了,还是会有error进来,怀疑是客户端app存在本地缓存,告知用户清理缓存;

6.问题解决;

故障原因:
在nginx配置文件中使用了try_files,如果平时不会出问题,但是由于更新存在缓存的问题,导致部分用户还是会去加载老的静态文件,导致服务端500错误。

try_files

    location ~.*\.(gif|jpg|jpeg|png)$ {
        root /web/wwwroot;
        try_files /static/$uri $uri;
    }

意思为当你去访问http://abc.com/test.jpg时,会去先检查/web/wwwroot/static/test.jpg是否存在,不存在就读取/web/wwwroot/test.jpg,但是由于最后一个参数时内部重定向,所以并不会去检查/web/wwwroot/test.jpg是否存在,只要第一个路径不存在就会重定向,然后再进入到这个location造成死循环。结果出现500 Internal Server Error

原文地址:https://www.cnblogs.com/hzxyf/p/13857416.html