php, nginx高並發優化


linux內核層面

以centos7.0為例

# 允許等待中的監聽 echo 50000 >/proc/sys/net/core/somaxconn #tcp連接快速回收 echo 1 >/proc/sys/net/ipv4/tcp_tw_recycle # tcp連接重用 echo 1 >/proc/sys/net/ipv4/tcp_tw_reuse #不抵御洪水攻擊 echo 0 >/proc/sys/net/ipv4/tcp_syncookies 

nginx優化

worker_process

worker_process 修改為內核數的1-2倍, 一般是4或8, 8以上優化不大

這里需要注意, 開太多的worker進程, 會增加cpu開銷,cpu占用會增高

keepalive_timeout

高並發下設為0

但是文件上傳需要保持連接, 開發時需注意, 做好業務拆分

worker_connections

設置worker進程最大打開的連接數, 建議盡量高,比如20480

worker_rlimit_nofile

將此值增加到大於worker_processes * worker_connections的值。 應該是增加當前worker運行用戶的最大文件打開數值

php-fpm

2020年5月4日22:09:18

php-fpm 可以選擇sock 和 tcp連接方式, 高並發下可以使用sock連接, 經過實測可以提高 20% 左右的rps(35 -> 42) , 不過技術穩定性較差

emergency_restart*

# 60秒內有10次子進程中斷,則重啟php-fpm, 防止因php垃圾代碼造成的中斷問題 emergency_restart_threshold =10 emergency_restart_interval =60 

process.max

允許的最大進程數, 一個php-fpm進程大約占用15-40m的內從, 具體設置值需要根據實際情況得出 我這里設為 512

pm.max_children

某個連接池允許的最大子進程, 不要超過process_max

pm.max_requests

允許的最大請求 ,設置2048

關掉慢請求日志

;request_slowlog_timeout = 0 ;slowlog = var/log/slow.log 

成果

環境

硬件

  • i5-3470 CPU
  • 4g 內存

軟件

  • php7.1.30
  • thinkPHP 5.1.35
  • nginx

業務說明

ab 到 thinkPHP框架首頁面, tp開啟了強路由模式, 未配置首頁路由, 走到miss 路由, 返回miss信息, 未調用db,返回的miss信息如下:

{"code":-8,"msg":"api不存在"} 

ab測試結果, 1w並發, 請求10次, 共10w請求

D:\soft\phpstudy\PHPTutorial\Apache\bin>ab -c 10000 -n 100000 http://fs_server.test/ This is ApacheBench, Version 2.3 <$Revision: 1748469 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking fs_server.test (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: nginx Server Hostname: fs_server.test Server Port: 80 Document Path: / Document Length: 32 bytes Concurrency Level: 10000 Time taken for tests: 492.928 seconds Complete requests: 100000 Failed requests: 0 Total transferred: 19500000 bytes HTML transferred: 3200000 bytes Requests per second: 202.87 [#/sec] (mean) Time per request: 49292.784 [ms] (mean) Time per request: 4.929 [ms] (mean, across all concurrent requests) Transfer rate: 38.63 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 1 2 6.6 2 1365 Processing: 18749 46094 8055.0 49145 52397 Waiting: 12231 45636 8504.8 48793 51627 Total: 18751 46096 8055.0 49147 52399 Percentage of the requests served within a certain time (ms) 50% 49147 66% 49279 75% 49347 80% 49386 90% 49473 95% 49572 98% 49717 99% 50313 100% 52399 (longest request) 

無丟失的請求, 就是花費時間有些長, 畢竟雖然沒走db,也是走的一套完整的tp框架, 總體算是合理的結果

不過這塊有個非常難堪的問題, 如果進行mysql,redis的操作, 會因為存儲媒介的連接問題, 造成響應丟失, nginx直接5XX錯誤,初步方案是提高其最大連接數待測試.

附幾個常用指令, 可以查看當前開啟了幾個fpm進程, 總內存開銷, 正在處理請求的進程等

# 確認php-fpm的worker進程是否夠用,如果不夠用就等於沒有開啟一樣 計算開啟worker進程數目: ps -ef | grep 'php-fpm'|grep -v 'master'|grep -v 'grep' |wc -l #計算正在使用的worker進程,正在處理的請求 netstat -anp | grep 'php-fpm'|grep -v 'LISTENING'|grep -v 'php-fpm.conf'|wc -l # 內存開銷 ps auxf | grep php | grep -v grep | grep -v master | awk '{sum+=$6} END {print sum}'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM