nginx 做為反向代理(reverse proxy)時偶爾有發生 error 的問題及排除

最近同事在問我,他的 nginx reverse proxy 上常會看到一堆錯誤日誌,看了上面總是會看到類似下面這類的錯誤

upstream sent too big header while reading response header from upstream

2018/12/04 08:50:13 [error] 18553#0: *23 upstream sent too big header while reading response header from upstream, client: 172.18.55.253, server: promoteradmin.mywebcom, request: "POST / HTTP/1.1", upstream: "http://172.18.55.5:8090/", host: "promoteradmin.mywebcom", referrer: "http://promoteradmin.mywebcom/"
2018/12/04 08:52:08 [error] 18932#0: *28 upstream sent too big header while reading response header from upstream, client: 172.18.55.253, server: promoteradmin.mywebcom, request: "POST / HTTP/1.1", upstream: "http://172.18.55.5:8090/", host: "promoteradmin.mywebcom", referrer: "http://promoteradmin.mywebcom/"
2018/12/04 08:52:33 [error] 18932#0: *28 upstream sent too big header while reading response header from upstream, client: 172.18.55.253, server: promoteradmin.mywebcom, request: "POST / HTTP/1.1", upstream: "http://172.18.55.5:8090/", host: "promoteradmin.mywebcom", referrer: "http://promoteradmin.mywebcom/"

自字面上的解釋是後面的 server 在丟回內容時含括了較大的 header 所以在 http response header 處理超過了定義大小,這個只要去調整  proxy_buffer 系列的參數, 這個類似的問題要看你後面的 Server 是什麼型式,例如後面若是php 等這類 fastcgi 的主機,就要調整 fastcgi_buffer 相關的參數

proxy_buffer_size 主要要調整這三個

proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;

不過由於 nginx 要求 temp的參數要和proxy buffer size 大於或一致,所以下面這個參數也要一起調整

proxy_temp_file_write_size 256k;

若是 fastcgi, 則調整下面這幾個

fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;

紅字的部份就要看你的環境,及檔案大小及代碼的大小調整 .

調整後 restart or reload  nginx 即可.

Author: jerryw1974

learning and focus on computer science, cloud infrastructure, virtualization and information security, technical, networking,platform system and cyber-security related topic.