記一次nginx錯誤
阿新 • • 發佈:2019-02-06
背景:當我編寫upstream模組時,在回撥函式create_request中,我構造了發向後端的http請求,並且後端服務也接收到請求,但是nginx卻接收不到後端服務返回的應答,導致回撥函式process_header不會被呼叫。
錯誤標識:upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "POST /upload HTTP/1.1", upstream: "127.0.0.1:9999", host: "localhost:8091", referrer: "http://localhost:8091/up.html"
分析:通過查詢nginx原始碼,在ngx_http_upstream.c檔案中列印的錯誤標識,定位在此
n = c->recv(c, u->buffer.last, u->buffer.end - u->buffer.last);
if (n == 0) {
ngx_log_error(NGX_LOG_ERR, c->log, 0,"upstream prematurely closed connection");
}
所以說,是接收到的資料個數為0,我在recv方法之前列印了upstream的buffer大小,發現是0,這意味著我沒有設定upstream的buffer size。因此,我在我的模組中添加了如下語句:buffer大小設定為操作頁大小,4K。同時要將upstream的buffering標誌設為0。這樣服務就正常了。conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE; ngx_conf_merge_size_value(conf->upstream.buffer_size,prev->upstream.buffer_size,(size_t) ngx_pagesize);