首先說下 先看 按照ab 每秒請求的結果 看看 都有每秒能請求幾個 如果並發量超出你請求的個數 會這樣 所以一般圖片和代碼服務器最好分開 還有看看io瓶ding 和有沒有延遲的PHP代碼執行
0 先修改內核參數
<pre>
1、調整同時打開文件數量
ulimit -n 20480
2、TCP最大連接數(somaxconn)
echo 10000 > /proc/sys/net/core/somaxconn
3、TCP連接立即回收、回用(recycle、reuse)
echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
4、不做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>
然后修改nginx.conf 比如worker_processes等等
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實例的方法
1、系統盤
通過創建自定義鏡像的方式,創建一個自定義鏡像,然后使用這個自定義鏡像創建ECS即可。
http://help.aliyun.com/knowledge_detail.htm?spm=5176.7114037.1996646101.1.uamHK9&categoryId=8314847&knowledgeId=5974456&pos=1
2、數據盤
對已經配置完成的數據盤進行打快照。
然后在購買或者升級頁面,添加磁盤的地方點:“用快照創建磁盤”,選擇你要的快照即可。