這幾天遊戲上線在查看 Nginx 的訪問日誌時,發現有很多 HTTP 返回碼爲 400 的記錄。 以下是Nginx 的訪問日誌: 222.89.55.137 - - [27/Dec/2014:19:35:15 +0800] "POST /dia.php HTTP/1.1" 400 172 "-" "-" "-" 網站查了很多文檔關於400問題的解決方法: 1.修改nginx配置文件 client_header_buffer_size 128k; large_client_header_buffers 4 128k; 按照這個方法實驗了,錯誤依舊問題依然沒有解決。 2.增加default配置文件 增加默認的server到配置文件,以下配置的解釋看這裏禁止未綁定域名訪問 代碼如下 複製代碼 server { listen 80 (www.111cn.net)default_server; server_name _; return 404; access_log off; } from:http://www.111cn.net/sys/nginx/52851.htm 這種方法也測試了也沒能解決。 於是查看RFC文檔: A client MUST include a Host header field in all HTTP/1.1 request messages . If the requested URI does not include an Internet host name for the service being requested, then the Host header field MUST be given with an empty value. An HTTP/1.1 proxy MUST ensure that any request message it forwards does contain an appropriate Host header field that identifies the service being requested by the proxy. All Internet-based HTTP/1.1 servers MUST respond with a 400 (Bad Request) status code to any HTTP/1.1 request message which lacks a Host header field. 上面是http1.1的rfc關於host部分的解釋, 從上面我們瞭解到如果一個http1.1的請求沒有host域, 那麼server應該給client段發送400的狀態碼, 表明這個請求server不能處理。而對於nginx server來說, 也遵循這樣的方式,說明client發送了一個無效的請求, nginx server無法處理,故返回400的狀態碼。 根據上面的描述分析判斷是客戶端請求的http header 是不可用造成的 於是繼續google 把Nginx的錯誤日誌的級別調到info級別來證明自己的判斷,結果真有收穫, 以下信息是Nginx errorlog中的內容: 2014/12/27 19:35:11 [info] 20798#0: *9705609 client sent invalid header line: "User-Agent: ^M 根據以上的錯誤信息判斷是客戶端發過來的header 是錯誤的導致nginx返回400錯誤, 至於爲什麼客戶端會發送錯誤的header 信息,我也只能讓開發來看了。