nginx 502 Bad Gateway 相关优化

压测发时返回502,观察nginx错误日志:

2018/12/28 17:59:54 [error] 505#0: *372671 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 10.0.1.67, server: cs.bs-advertising.arhieason.com, request: "GET /api/advertising?day_timestamp= HTTP/1.1", upstream: "http://10.0.1.63:8091/api/advertising?day_timestamp=", host: "10.0.1.63:82"

 

解决思路:

1.优化系统的IO:如tcp复用,打开文件数

2.nginx相关参数的优化

a)tcp

tcp_nopush on;
tcp_nodelay on;

b)http 1.1的 keepalive

keepalive_timeout 300;

c)proxy buffer :代理缓冲区
proxy_buffer_size 64k;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
types_hash_max_size 2048;

d)在相应的location中设置代理的timeout

proxy_connect_timeout 60;

最长不能超过75秒

如:

location ^~ /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header host $host;
proxy_pass http://cs.bs-advertising-backend;
proxy_http_version 1.1;
proxy_set_header Connection "Keep-Alive";
proxy_connect_timeout 60;
}

 

 

 

参考链接:

https://ningyu1.github.io/site/post/03-nginx-502-bad-gateway/

 

==================

n

location /healthz {
#access_log off;
return 200;
}

 

 

==========

/etc/logrotate.d/nginx

'''

/var/log/nginx/*.log {
daily
dateext
rotate 1095
sharedscripts
postrotate
kill -USR1 `cat /run/nginx.pid`
endscript
}

'''

 

===========

location ~* ^/(admin|api|app|metrics) {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Max-Age 1728000;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS, PUT, DELETE';
add_header Access-Control-Allow-Headers 'Page-Code,Authorization,Content-Type';
if ($request_method = 'OPTIONS') {
return 204;
}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header host $host;
proxy_pass http://infinite-window-backend;
}

posted @ 2018-12-29 09:58  rootid  阅读(523)  评论(0)    收藏  举报