nginx的error.log日志常见的几个错误解决方法

来源:互联网 发布:淘宝优惠券推广赚佣金 编辑:程序博客网 时间:2024/04/19 17:54

nginx.conf里会有两个日志,分为access.log 和 error.log。其中这两个日志可以细化,一般来说在nginx目录下会有一个logs会保存,然后也可以在对应的server目录里可以分别的设定access.log和error.log来了解对应server的情况。


access.log主要是记录"谁来登陆了,从哪里登陆的,登陆后发生了什么",具体格式可以在nginx.conf里设定。


error.log主要记录的是检查nginx.conf里发现的错误,模式不支持自定义。与access.log后面经常用main结尾不同,error.log后面的结尾可能是warm也可能是crit,这里的warm或者crit代表错误的等级,crit表示最少,而debug表示记录的最详细,屁大点事都记下来。


error_log off并不能关闭日志记录功能,它将日志文件写入一个文件名为off的文件中,如果你想关闭错误日志记录功能,应使用以下配置:error_log /dev/null crit;(把存储位置设置到Linux的黑洞中去 )。


当打开error.log的时候,可能会看到各种样的内容,比如:

1
“2016/05/03 10:20:51 [emerg] 20952#0: unexpected "}" in /usr/local/nginx/conf/nginx.conf:87”

这句话就说明在nginx.conf的87行里有一个 } 是错误的,检查一下}是不是多余了,或者;少了,这个错误的级别是emergency

1
2016/05/03 10:23:01 [emerg] 21023#0: "root" directive is duplicate in /usr/local/nginx/conf/nginx.conf:86

这句话就是说明在nginx.conf的第86行里root设定重复了,级别同样是emergency。以上两个都是书写的问题,很好纠正;

1
2016/05/03 10:23:31 [notice] 21045#0: signal process started

这个意思是nginx已经在运行的状态下,被执行启动,这个不算致命错误;

1
nginx: [alert] could not open error log fileopen()"/usr/local/nginx/logs/error.log" failed (13:Permissiondenied)


这个是说当前用户没有权限写入error.log的日志,解决方法要来权限就行了;

1
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

nginx提示无法找到nginx.pid这个文件了,使用#/usr/local/nginx/sbin/nginx -c  /usr/local/nginx/conf/nginx.conf,重新启动一下,就自动生成pid文件了。


下面说几个有特殊代表性的错误:

1)worker process 某某某 exited on signal 11 (core dumped)   
这种错误基本就是刷error.log的屏,严重的甚至直接让nginx崩掉。具体表现在用户端就是“视频打不开,网页打不开等等等”。


这种错误一般是表示用户程序nginx进行读操作时访问的地址无效,具体一点就是搜索引擎的蜘蛛在爬取到加密部分时,得不到正确的路径,又没有被定位到错误页导致的。


如何修改,在nginx.conf里的防盗链部分检查一下“secure_download_fail_location;” ,即“请求错误时,定向到错误页”的模块,确认location是否定向到一个正确地址为错误页面。


2)nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)    

nginx先监听了ipv4的80端口之后又监听了ipv6的80端口,于是就重复占用了。

把nginx.conf里的:

1
2
listen 80;
listen [::]:80 default_server;

改成

1
2
listen 80;
listen [::]:80 ipv6only=on default_server;

http://stackoverflow.com/questions/14972792/nginx-nginx-emerg-bind-to-80-failed-98-address-already-in-use


3)  rewrite or internal redirection cycle while internally redirecting to "/ie.html", client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "203.46.90.146"

这个错误一般就是rewrite重定向进入了死循环,用户端的情况就是登陆localhost/ie.html的时候,出现500错误。要解决这个问题需要vim一下nginx.conf这个配置文件,查找到ie.html。发现这一行是这么写的:

1
2
3
4
5
6
7
location / {
    if ($http_usr_agent ~ MSIE) {        #如果用户使用的浏览器匹配ie的话
        rewrite ^.*$ /ie.html;        #ie用户会rewrite到/usr/local/nginx/html/ie.html里
        }
    root /usr/local/nginx/html;          #非ie用户就是访问/usr/local/nginx/html的index.html界面
    index index.html
}

返回到/usr/local/nginx/html,发现的确存在ie.html,且格式正确。


问题在 if语句那一段,ie用户被发现使用的浏览器是ie就会去登陆/ie.html界面,但是,要登陆/ie.html的时候还是会先判断浏览器型号,发现是ie,然后又被派去登陆/ie.html,然后再登陆/ie.html的时候,又要判断浏览器型号,就这样周而复始死循环,所以最后就500,Internal Server Error。


遇到这种情况怎么办?增加一个break,跳出循环重定向。

1
2
3
4
5
6
7
8
location / {
    if ($http_usr_agent ~ MSIE) {        #如果用户使用的浏览器匹配ie的话
        rewrite ^.*$ /ie.html;        #ie用户会rewrite到/usr/local/nginx/html/ie.html里
         break
            }
    root /usr/local/nginx/html;          #非ie用户就是访问/usr/local/nginx/html的index.html界面
    index index.html
}


4)open() "某网址" failed (24:Too Many open files),client:某某某,server:某某某...

这种错误很明显,就是一个socket进程打开的文件超额,这种情况,需要适当放开可打开的文件数量。

ulimit -n 20000,这个值默认是1024,现在放大到20000。


5)499错误

Nginx的error.log有可能会出现499错误,这种错误有两种可能,一个是客户端主动断开链接;第二个就是两个post距离过近,nginx认为这种快速提交post是不安全的,服务器就主动拒绝链接。

解决这种错误就是在nginx.conf的全局配置里,添加一句话:

proxy_ingore_client_abort on;

意思是让服务器不要主动关闭对客户端的链接。这么搞,安全性肯定是会有所下降,但是总比找不到服务器要好。

本文出自 “生活就是等待戈多” 博客,请务必保留此出处http://chenx1242.blog.51cto.com/10430133/1769724

原创粉丝点击