天天看點

nginx的error.log日志常見的幾個錯誤解決方法

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

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

這句話就說明在nginx.conf的87行裡有一個 } 是錯誤的,檢查一下}是不是多餘了,或者;少了,這個錯誤的級别是emergency;

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

這句話就是說明在nginx.conf的第86行裡root設定重複了,級别同樣是emergency。以上兩個都是書寫的問題,很好糾正;

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

這個意思是nginx已經在運作的狀态下,被執行啟動,這個不算緻命錯誤;

<code>nginx: [alert] could not </code><code>open</code> <code>error log </code><code>file</code><code>: </code><code>open</code><code>()</code><code>"/usr/local/nginx/logs/error.log"</code> <code>failed (13:Permissiondenied)</code>

這個是說目前使用者沒有權限寫入error.log的日志,解決方法要來權限就行了;

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

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裡的:

2

<code>listen 80;</code>

<code>listen [::]:80 default_server;</code>

改成

<code>listen [::]:80 ipv6only=on default_server;</code>

<a href="http://stackoverflow.com/questions/14972792/nginx-nginx-emerg-bind-to-80-failed-98-address-already-in-use" target="_blank">http://stackoverflow.com/questions/14972792/nginx-nginx-emerg-bind-to-80-failed-98-address-already-in-use</a>

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。發現這一行是這麼寫的:

3

4

5

6

7

<code>location / {</code>

<code>    </code><code>if</code> <code>($http_usr_agent ~ MSIE) {        </code><code>#如果使用者使用的浏覽器比對ie的話</code>

<code>        </code><code>rewrite ^.*$ </code><code>/ie</code><code>.html;        </code><code>#ie使用者會rewrite到/usr/local/nginx/html/ie.html裡</code>

<code>        </code><code>}</code>

<code>    </code><code>root </code><code>/usr/local/nginx/html</code><code>;          </code><code>#非ie使用者就是通路/usr/local/nginx/html的index.html界面</code>

<code>    </code><code>index index.html</code>

<code>}</code>

傳回到/usr/local/nginx/html,發現的确存在ie.html,且格式正确。

問題在 if語句那一段,ie使用者被發現使用的浏覽器是ie就會去登陸/ie.html界面,但是,要登陸/ie.html的時候還是會先判斷浏覽器型号,發現是ie,然後又被派去登陸/ie.html,然後再登陸/ie.html的時候,又要判斷浏覽器型号,就這樣周而複始死循環,是以最後就500,Internal Server Error。

遇到這種情況怎麼辦?增加一個break,跳出循環重定向。

8

<code>         </code><code>break</code><code>;</code>

<code>            </code><code>}</code>

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;

意思是讓伺服器不要主動關閉對用戶端的連結。這麼搞,安全性肯定是會有所下降,但是總比找不到伺服器要好。

 本文轉自 蘇幕遮618 51CTO部落格,原文連結:http://blog.51cto.com/chenx1242/1769724

繼續閱讀