一。前言#
NGINX 反向代理 後端介面時報錯:
[error] ...: *1918 readv() failed (104: Connection reset by peer) while reading upstream ...
控制台報錯訊息
NGINX 錯誤日誌
二。解決辦法#
參考文章 http://blog.51yip.com/apachenginx/2203.html
查看 nginx error 錯誤,發現上傳介面報以下錯:
2019/10/10 19:58:25 [error] 299784#0: \*5967188 readv() failed (104: Connection reset by peer) while reading upstream, client: 59.34.155.7, server: xxxxxxxx, request: "POST /stream/tracking/file HTTP/1.1", upstream: "http://xxxxxxxx/stream/tracking/file", host: "xxxxxxxx"
這種錯誤日誌不多,第一感覺就是上傳檔案過大,傳輸時間過長,然後連接被中斷。
當使用nginx作為反向代理時,為了支援長連線,需要做到兩點:
從client到nginx的連接是長連線,對於客戶端來說,nginx長連線是預設開啟的。 從nginx到server的連接是長連線,需要自己開啟
upstream bigdata { server 10.0.20.xx:18018; server 10.0.20.xx:18018; server 10.0.20.xx:18018; server 10.0.20.xx:18018; keepalive 100; //根據qps來調整 } location ~ / { 。。。。。。。。。省略。。。。。。。。。。。。。 proxy_connect_timeout 120; //加大120 proxy_send_timeout 120; //加大120 proxy_read_timeout 120; //加大120 proxy_http_version 1.1; //開啟後端,長連線 proxy_set_header Connection ""; //開啟後端,長連線 }
注意:
keepalive指定的數值是Nginx每個worker連接後端的最大長連線數,而不是整個Nginx的.
作者:海底蒼鷹
三。個人配置記錄#
$ cat cpen.top.conf
server {
listen xxx ssl;
server_name xxx.xxx;
location / {
proxy_pass http://127.0.0.1:xxx;
proxy_connect_timeout 120; # 加大120
proxy_send_timeout 120; # 加大120
proxy_read_timeout 120; # 加大120
proxy_http_version 1.1; # 開啟後端,長連線
proxy_set_header Connection ""; # 開啟後端,長連線
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
}
ssl_certificate /usr/local/nginx/ssl/xxx.pem;
ssl_certificate_key /usr/local/nginx/ssl/xxx.key;
access_log /data/service_logs/nginx/xxx.log misc;
error_log /data/service_logs/nginx/xxx.log;
}