首頁 > SEO優化 > 用Varnish和Memcached給WordPress網站提速

用Varnish和Memcached給WordPress網站提速

2015-04-10 admin SEO優化 0人評論 44847

用Varnish和Memcached給WordPress網站提速  SEO優化  第1張

Varnish是一款高性能的緩存加速器,Varnish把數據存放在服務器的內存中,利用內存可以極大的提高PHP頁面執行速度,可以設置0~60秒的精確緩存時間,32位的機器支持的緩存文件最大為2 GB。

3分快3 Varnish采用VCL的配置,而且具有強大的管理功能,如top、stat、admin、lis,管理方式比較靈活。Varnish的狀態機設計不僅巧妙,結構也很清晰,利用二叉堆管理緩存文件,即可達到隨時刪除的目的。

Memcached是一個高性能的分布式內存對象緩存系統,通過在內存中緩存數據和對象來減少讀取數據庫的次數,從而提高動態、數據庫驅動網站的速度。Memcached對于減少MysqL數據查詢壓力非常有幫助。

由于Varnish采用了Visual Page Cache技術,所有緩存的數據都直接從內存讀取,而Squid從硬盤讀取緩存的數據,所以Varnish在訪問速度方面會更快一些。但是Varnish在高并發狀態下,CPU、I/O和內存等資源的開銷高于Squid。

目前Varnish3.0版本解決了服務器重啟后Varnish緩存消失的問題,性能優化上有了更大的提升。本篇文章就來分享一下利用Varnish和Memcached緩存來給Wordpress加速,因為要用到內存,所以比較適合那些大內存的服務器。

用Varnish和Memcached緩存給Wordpress網站提速-內存級加速

一、Varnish安裝方法

1、Varnish官網:

官方網站:http://www.varnish-cache.org/

3分快3 2、對于Centos 5的,可以執行以下命令來安裝:

3分快3 rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release/varnish-release-3.0-1.el5.centos.noarch.rpm

3分快3 yum install varnish

3、對于是Centos 6的,可以執行以下命令來安裝:

rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el6/noarch/varnish-release/varnish-release-3.0-1.el6.noarch.rpm

yum install varnish

用Varnish和Memcached給WordPress網站提速  SEO優化  第2張

4、如果版本搞錯,就會出現如下提示錯誤:

3分快3 error: Failed dependencies:

    rpmlib(FileDigests) <= 4.6.0-1 is needed by varnish-release-3.0-1.el6.noarch

3分快3     rpmlib(PayloadIsXz) <= 5.2-1 is needed by varnish-release-3.0-1.el6.noarch

Error: Missing Dependency: libedit.so.0 is needed by package varnish-3.0.5-1.el5.centos.i386 (varnish-3.0)

用Varnish和Memcached給WordPress網站提速  SEO優化  第3張

5、Centos 5安裝時還會提示有依賴關系不能解決,解決的辦法就是添加擴展的YUM 源,執行以下命令:

rpm -ivh http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.5.1-1.el5.rf.i386.rpm (32位)

rpm -ivh http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm (64位)

3分快3 yum clean allyum update

3分快3 6、對于是Debian系統,可以執行以下命令來安裝:

curl http://repo.varnish-cache.org/debian/GPG-key.txt | apt-key add -echo "deb http://repo.varnish-cache.org/debian/ wheezy varnish-3.0" >> /etc/apt/sources.list

3分快3 apt-get update

apt-get install varnish

6、對于是Ubuntu系統,可以執行以下命令來安裝:

3分快3 curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -echo "deb http://repo.varnish-cache.org/ubuntu/ precise varnish-3.0" | sudo tee -a /etc/apt/sources.listsudo

3分快3 apt-get updatesudo

apt-get install varnish

3分快3 7、設置Varnish開機啟動,執行:chkconfig varnish on和chkconfig varnishncsa on

8、啟動Varnish的命令是:service varnish start和service varnishncsa start

用Varnish和Memcached給WordPress網站提速  SEO優化  第4張

二、Varnish相關配置

1、設置好Varnish緩存規則。默認是/etc/varnish/default.vcl,大家可以下載這個Varnish WordPress配置文件覆蓋原來的,地址:http://centos.googlecode.com/files/default.vcl。源碼內容:

  • # This is a basic VCL configuration file for varnish.  See the vcl(7)
  • # man page for details on VCL syntax and semantics.
  • # Default backend definition.  Set this to point to your content
  • # server.
  • backend default {
  • .host = "127.0.0.1";
  • .port = "8080";
  • }
  • acl purge {
  • "localhost";
  • "127.0.0.1";
  • }
  • # Below is a commented-out copy of the default VCL logic.  If you
  • # redefine any of these subroutines, the built-in logic will be
  • # appended to your code.
  • sub vcl_recv {
  • # Only cache the following site
  • if (req.http.host ~ "(amhg.freehao123.info)") {
  • set req.backend = default;
  • } else {
  • return (pass);
  • }
  • if (req.request == "PURGE") {
  • if (!client.ip ~ purge) {
  • error 405 "Not allowed.";
  • }
  • return (lookup);
  • }
  • if (req.restarts == 0) {
  • if (req.http.x-forwarded-for) {
  • set req.http.X-Forwarded-For =
  • req.http.X-Forwarded-For + ", " + client.ip;
  • } else {
  • set req.http.X-Forwarded-For = client.ip;
  • }
  • }
  • if (req.request != "GET" &&
  • req.request != "HEAD" &&
  • req.request != "PUT" &&
  • req.request != "POST" &&
  • req.request != "TRACE" &&
  • req.request != "OPTIONS" &&
  • req.request != "DELETE") {
  • /* Non-RFC2616 or CONNECT which is weird. */
  • return (pipe);
  • }
  • if (req.request != "GET" && req.request != "HEAD") {
  • /* We only deal with GET and HEAD by default */
  • return (pass);
  • }
  • if (req.http.Authorization || req.http.Cookie ~ "wordpress_logged" || req.http.Cookie ~ "comment_") {
  • /* Not cacheable by default */
  • return (pass);
  • }
  • return (lookup);
  • }
  • sub vcl_pipe {
  • # Note that only the first request to the backend will have
  • # X-Forwarded-For set.  If you use X-Forwarded-For and want to
  • # have it set for all requests, make sure to have:
  • # set bereq.http.connection = "close";
  • # here.  It is not set by default as it might break some broken web
  • # applications, like IIS with NTLM authentication.
  • return (pipe);
  • }
  • sub vcl_pass {
  • return (pass);
  • }
  • sub vcl_hash {
  • hash_data(req.url);
  • if (req.http.host) {
  • hash_data(req.http.host);
  • } else {
  • hash_data(server.ip);
  • }
  • return (hash);
  • }
  • sub vcl_hit {
  • if (req.request == "PURGE") {
  • purge;
  • error 200 "Purged.";
  • }
  • return (deliver);
  • }
  • sub vcl_miss {
  • if (req.request == "PURGE") {
  • purge;
  • error 200 "Purged.";
  • }
  • return (fetch);
  • }
  • sub vcl_fetch {
  • if (beresp.ttl <= 0s ||
  • beresp.http.Set-Cookie ||
  • beresp.http.Vary == "*") {
  • /*
  • * Mark as "Hit-For-Pass" for the next 2 minutes
  • */
  • set beresp.ttl = 120 s;
  • return (hit_for_pass);
  • }
  • set beresp.ttl = 1d;
  • return (deliver);
  • }
  • sub vcl_deliver {
  • return (deliver);
  • }
  • sub vcl_error {
  • set obj.http.Content-Type = "text/html; charset=utf-8";
  • set obj.http.Retry-After = "5";
  • synthetic {"
  • <?xml version="1.0" encoding="utf-8"?>
  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  • "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  • <html>
  • <head>
  • <title>"} + obj.status + " " + obj.response + {"</title>
  • </head>
  • <body>
  • <h1>Error "} + obj.status + " " + obj.response + {"</h1>
  • <p>"} + obj.response + {"</p>
  • <h3>Guru Meditation:</h3>
  • <p>XID: "} + req.xid + {"</p>
  • <hr>
  • <p>Varnish cache server</p>
  • </body>
  • </html>
  • "};
  • return (deliver);
  • }
  • sub vcl_init {
  • return (ok);
  • }
  • sub vcl_fini {
  • return (ok);
  • }

2、下載下來的default.vcl你需要調整的地方有一處,就是將域名更改為自己要使用Varnish緩存的域名。

用Varnish和Memcached給WordPress網站提速  SEO優化  第5張

3、另外default.vcl還設置緩存時間,單位是s(秒),h(小時),d(天)。

用Varnish和Memcached給WordPress網站提速  SEO優化  第6張

3分快3 4、配置Varnish的訪問端口。Varnish配置默認的訪問端口不是80端口,因此需要修改/etc/sysconfig/varnish配置文件,把端口設置為80。

用Varnish和Memcached給WordPress網站提速  SEO優化  第7張

5、在/etc/sysconfig/varnish這個文件中還可以設置Varnish緩存大小,默認是1GB。

用Varnish和Memcached給WordPress網站提速  SEO優化  第8張

標簽:

發表評論

◎歡迎參與討論,請在這里發表您的看法、交流您的觀點。

官方微信公眾號
70755559
09:00 - 22:00
QQ客服: 70755559
客服郵箱: 678128@qq.com
2分快3-推荐 1分快3-官网 好运快3-欢迎您 幸运快3-安全购彩 网投app-Welcome 分分快3app-Home 1分快三平台-3分快3 彩票代理-推荐 五福彩票-官网 彩票大赢家-欢迎您