nginx – 为强大的服务器优化php-fpm和varnish

前端之家收集整理的这篇文章主要介绍了nginx – 为强大的服务器优化php-fpm和varnish前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我的设置是英特尔®酷睿™i7-2600和内存16 GB DDR3内存

清漆Nginx PHP-fpm apc用于W3 Total Cache和CDN的非常重的wordpress博客

我的问题是,根据blitz.io清漆开始发出超时后,每秒55次点击.此时的cpu使用率几乎不是1%.可用内存始终为10GB.

我尝试直接对PHP-fpm进行基准测试,结果为150次点击/秒,没有任何超时.但之后cpu使用率达到100%并停止响应.

你能帮我优化一下来处理更多吗?

据我所知,Nginx在这里无关,所以我没有包含它的配置.

PHP-fpm配置

  1. listen = /tmp/PHP5-fpm.sock
  2. listen.allowed_clients = 127.0.0.1
  3. user = Nginx
  4. group = Nginx
  5. pm = dynamic
  6. pm.max_children = 150
  7. pm.start_servers = 7
  8. pm.min_spare_servers = 2
  9. pm.max_spare_servers = 15
  10. pm.max_requests = 500
  11. slowlog = /var/log/PHP-fpm/www-slow.log
  12. PHP_admin_value[error_log] = /var/log/PHP-fpm/www-error.log
  13. PHP_admin_flag[log_errors] = on

APC

  1. extension = apc.so
  2. apc.enabled=1
  3. apc.shm_size=512MB
  4. apc.num_files_hint=0
  5. apc.user_entries_hint=0
  6. apc.ttl=7200
  7. apc.use_request_time=1
  8. apc.user_ttl=7200
  9. apc.gc_ttl=3600
  10. apc.cache_by_default=1
  11. apc.filters
  12. apc.mmap_file_mask=/tmp/apc.XXXXXX
  13. apc.file_update_protection=2
  14. apc.enable_cli=0
  15. apc.max_file_size=1M
  16. apc.stat=1
  17. apc.stat_ctime=0
  18. apc.canonicalize=0
  19. apc.write_lock=1
  20. apc.report_autofilter=0
  21. apc.rfc1867=0
  22. apc.rfc1867_prefix =upload_
  23. apc.rfc1867_name=APC_UPLOAD_PROGRESS
  24. apc.rfc1867_freq=0
  25. apc.rfc1867_ttl=3600
  26. apc.include_once_override=0
  27. apc.lazy_classes=0
  28. apc.lazy_functions=0
  29. apc.coredump_unmap=0
  30. apc.file_md5=0
  31. apc.preload_path

清漆VCL

  1. backend default {
  2. .host = "127.0.0.1";
  3. .port = "8080";
  4. .connect_timeout = 6s;
  5. .first_byte_timeout = 6s;
  6. .between_bytes_timeout = 60s;
  7. }
  8. acl purgehosts {
  9. "localhost";
  10. "127.0.0.1";
  11. }
  12. # Called after a document has been successfully retrieved from the backend.
  13. sub vcl_fetch {
  14. # Uncomment to make the default cache "time to live" is 5 minutes,handy
  15. # but it may cache stale pages unless purged. (TODO)
  16. # By default Varnish will use the headers sent to it by Apache (the backend server)
  17. # to figure out the correct TTL.
  18. # WP Super Cache sends a TTL of 3 seconds,set in wp-content/cache/.htaccess
  19. set beresp.ttl = 24h;
  20. # Strip cookies for static files and set a long cache expiry time.
  21. if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$") {
  22. unset beresp.http.set-cookie;
  23. set beresp.ttl = 24h;
  24. }
  25. # If wordpress cookies found then page is not cacheable
  26. if (req.http.Cookie ~"(wp-postpass|wordpress_logged_in|comment_author_)") {
  27. # set beresp.cacheable = false;#versions less than 3
  28. #beresp.ttl>0 is cacheable so 0 will not be cached
  29. set beresp.ttl = 0s;
  30. } else {
  31. #set beresp.cacheable = true;
  32. set beresp.ttl=24h;#cache for 24hrs
  33. }
  34. # Varnish determined the object was not cacheable
  35. #if ttl is not > 0 seconds then it is cachebale
  36. if (!beresp.ttl > 0s) {
  37. # set beresp.http.X-Cacheable = "NO:Not Cacheable";
  38. } else if ( req.http.Cookie ~"(wp-postpass|wordpress_logged_in|comment_author_)" ) {
  39. # You don't wish to cache content for logged in users
  40. set beresp.http.X-Cacheable = "NO:Got Session";
  41. return(hit_for_pass); #prevIoUsly just pass but changed in v3+
  42. } else if ( beresp.http.Cache-Control ~ "private") {
  43. # You are respecting the Cache-Control=private header from the backend
  44. set beresp.http.X-Cacheable = "NO:Cache-Control=private";
  45. return(hit_for_pass);
  46. } else if ( beresp.ttl < 1s ) {
  47. # You are extending the lifetime of the object artificially
  48. set beresp.ttl = 300s;
  49. set beresp.grace = 300s;
  50. set beresp.http.X-Cacheable = "YES:Forced";
  51. } else {
  52. # Varnish determined the object was cacheable
  53. set beresp.http.X-Cacheable = "YES";
  54. if (beresp.status == 404 || beresp.status >= 500) {
  55. set beresp.ttl = 0s;
  56. }
  57. # Deliver the content
  58. return(deliver);
  59. }
  60. sub vcl_hash {
  61. # Each cached page has to be identified by a key that unlocks it.
  62. # Add the browser cookie only if a wordpress cookie found.
  63. if ( req.http.Cookie ~"(wp-postpass|wordpress_logged_in|comment_author_)" ) {
  64. #set req.hash += req.http.Cookie;
  65. hash_data(req.http.Cookie);
  66. }
  67. }
  68. # vcl_recv is called whenever a request is received
  69. sub vcl_recv {
  70. # remove ?ver=xxxxx strings from urls so css and js files are cached.
  71. # Watch out when upgrading wordpress,need to restart Varnish or flush cache.
  72. set req.url = regsub(req.url,"\?ver=.*$","");
  73. # Remove "replytocom" from requests to make caching better.
  74. set req.url = regsub(req.url,"\?replytocom=.*$","");
  75. remove req.http.X-Forwarded-For;
  76. set req.http.X-Forwarded-For = client.ip;
  77. # Exclude this site because it breaks if cached
  78. if ( req.http.host == "sr.ituts.gr" ) {
  79. return( pass );
  80. }
  81. # Serve objects up to 2 minutes past their expiry if the backend is slow to respond.
  82. set req.grace = 120s;
  83. # Strip cookies for static files:
  84. if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$") {
  85. unset req.http.Cookie;
  86. return(lookup);
  87. }
  88. # Remove has_js and Google Analytics __* cookies.
  89. set req.http.Cookie = regsuball(req.http.Cookie,"(^|;\s*)(__[a-z]+|has_js)=[^;]*","");
  90. # Remove a ";" prefix,if present.
  91. set req.http.Cookie = regsub(req.http.Cookie,"^;\s*","");
  92. # Remove empty cookies.
  93. if (req.http.Cookie ~ "^\s*$") {
  94. unset req.http.Cookie;
  95. }
  96. if (req.request == "PURGE") {
  97. if (!client.ip ~ purgehosts) {
  98. error 405 "Not allowed.";
  99. }
  100. #prevIoUs version ban() was purge()
  101. ban("req.url ~ " + req.url + " && req.http.host == " + req.http.host);
  102. error 200 "Purged.";
  103. }
  104. # Pass anything other than GET and HEAD directly.
  105. if (req.request != "GET" && req.request != "HEAD") {
  106. return( pass );
  107. } /* We only deal with GET and HEAD by default */
  108. # remove cookies for comments cookie to make caching better.
  109. set req.http.cookie = regsub(req.http.cookie,"1231111111111111122222222333333=[^;]+(; )?","");
  110. # never cache the admin pages,or the server-status page,or your Feed? you may want to..i don't
  111. if (req.request == "GET" && (req.url ~ "(wp-admin|bb-admin|server-status|Feed)")) {
  112. return(pipe);
  113. }
  114. # don't cache authenticated sessions
  115. if (req.http.Cookie && req.http.Cookie ~ "(wordpress_|PHPSESSID)") {
  116. return(lookup);
  117. }
  118. # don't cache ajax requests
  119. if(req.http.X-Requested-With == "XMLHttpRequest" || req.url ~ "nocache" || req.url ~
  120. "(control.PHP|wp-comments-post.PHP|wp-login.PHP|bb-login.PHP|bb-reset-password.PHP|register.PHP)") {
  121. return (pass);
  122. }
  123. return( lookup );
  124. }

清漆守护进程选项

  1. DAEMON_OPTS="-a :80 \
  2. -T 127.0.0.1:6082 \
  3. -f /etc/varnish/ituts.vcl \
  4. -u varnish -g varnish \
  5. -S /etc/varnish/secret \
  6. -p thread_pool_add_delay=2 \
  7. -p thread_pools=8 \
  8. -p thread_pool_min=100 \
  9. -p thread_pool_max=1000 \
  10. -p session_linger=50 \
  11. -p session_max=150000 \
  12. -p sess_workspace=262144 \
  13. -s malloc,5G"

我不知道从哪里开始.我应该从优化PHP-fpm开始然后去清漆吗?或者现在是最大的PHP-fpm所以我应该开始寻找清漆中的问题了吗?

最佳答案
在配置中:Varnish Daemon选项

更改后,请遵循specials参数,并默认.我看他们一样

猜你在找的Nginx相关文章