nginx recv() failed (104: Connection reset by peer) while reading response header from upstream解决方法

首先说下 先看 按照ab 每秒请求的结果 看看 都有每秒能请求几个 若是并发量超出你请求的个数 会这样 因此通常图片和代码服务器最好分开 还有看看io瓶ding 和有没有延迟的PHP代码执行php

0 先修改内核参数nginx

<pre>
一、调整同时打开文件数量服务器

ulimit -n 20480
二、TCP最大链接数(somaxconn)cookie

echo 10000 > /proc/sys/net/core/somaxconn
三、TCP链接当即回收、回用(recycle、reuse)并发

echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
四、不作TCP洪水抵御负载均衡

echo 0 > /proc/sys/net/ipv4/tcp_syncookies
</pre>
也能够直接使用优化后的配置,在/etc/sysctl.conf中加入:
<pre>
net.core.somaxconn = 20480
net.core.rmem_default = 262144
net.core.wmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 4096 16777216
net.ipv4.tcp_wmem = 4096 4096 16777216
net.ipv4.tcp_mem = 786432 2097152 3145728
net.ipv4.tcp_max_syn_backlog = 16384
net.core.netdev_max_backlog = 20000
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_max_orphans = 131072
net.ipv4.tcp_syncookies = 0
</pre>tcp


而后修改nginx.conf 好比worker_processes等等php-fpm

 

 

 

 


1 查看nginx错误日记 error.log日志获得connect() to unix:/tmp/php-cgi.sock failed xxxxx........
若是是由于忽然的网站访问量大(这里说明这个是说tcp太高,并不能说明cpu太高和内存使用率太高,带宽太高)致使那么通常是由于子进程数不足:
到php-fpm.conf找到max_children也就是子进程最大数改成:<value name="max_children">128</value>性能

 


2优化

<pre>
tail -n 10 /var/log/messages


Dec 1 15:33:31 iZ238qupob7Z kernel: TCP: time wait bucket table overflow
Dec 1 15:33:31 iZ238qupob7Z kernel: TCP: time wait bucket table overflow
Dec 1 15:33:31 iZ238qupob7Z kernel: TCP: time wait bucket table overflow
Dec 1 15:33:31 iZ238qupob7Z kernel: TCP: time wait bucket table overflow
Dec 1 15:33:31 iZ238qupob7Z kernel: TCP: time wait bucket table overflow
Dec 1 15:33:31 iZ238qupob7Z kernel: TCP: time wait bucket table overflow
Dec 1 15:33:31 iZ238qupob7Z kernel: TCP: time wait bucket table overflow
Dec 1 15:33:32 iZ238qupob7Z kernel: TCP: time wait bucket table overflow
Dec 1 15:33:32 iZ238qupob7Z kernel: TCP: time wait bucket table overflow
Dec 1 15:33:32 iZ238qupob7Z kernel: TCP: time wait bucket table overflow
</pre>
time wait bucket table overflow 这种表明 服务器的TCP链接数,超出了内核定义最大数

查看当前的内核定义最大数
<pre>
cat /proc/sys/net/ipv4/tcp_max_tw_buckets
</pre>


<pre>
解决方法:
修改内核参数 /proc/sys/net/ipv4/tcp_max_tw_buckets
# echo 30000 > /proc/sys/net/ipv4/tcp_max_tw_buckets
写入/etc/sysctl.conf使之永久生效
echo 'net.ipv4.tcp_max_tw_buckets = 30000' >> /etc/sysctl.conf && sysctl -p
</pre>

 

 

 


3 固然也有可能PHP代码 消耗太多CPU 执行时间过长 将采起以下方法 看看里面的错误链接的连接是否是请求时间过长

 

nginx recv() failed (104: Connection reset by peer) while reading response header from upstream解决方法 除了升级服务器配置还有如下方法能够参考下


request_terminate_timeout = 0//这样 就不怕了 以前是PHP执行时间由于超过了设置的值因此报错 这个时候须要重启php-fpm 或者等他本身重启时间至关漫长
还有另外设置下PHP限制执行时间 设置2秒便可 超过2秒就直接报错让用户不要继续访问了 就不影响服务器性能了
<pre>
max_execution_time = 10;
</pre>

好比像导出excel耗时的 在代码中加
<pre>
set_time_limit(0);
</pre>


ps:能够开启慢查询日记 PHP执行超过1秒的记录下
<pre>
slowlog = /data/logs/php-fpm.log.slow
request_slowlog_timeout = 6
</pre>

 

 

 


4 若是以上都不行 图片都用CDN 或者就直接再买个服务器负载均衡就行了 买1个星期的 估计能抗几下

复制ecs实例的方法 一、系统盘 经过建立自定义镜像的方式,建立一个自定义镜像,而后使用这个自定义镜像建立ECS便可。 http://help.aliyun.com/knowledge_detail.htm?spm=5176.7114037.1996646101.1.uamHK9&categoryId=8314847&knowledgeId=5974456&pos=1 二、数据盘 对已经配置完成的数据盘进行打快照。 而后在购买或者升级页面,添加磁盘的地方点:“用快照建立磁盘”,选择你要的快照便可。

相关文章
相关标签/搜索