Centos上安装部署varnish图片缓存服务器进行加速

centos安装配置varnish
如果再整合DNS,GeoIP,Nginx可就称得上私有的CDN服务了!

  1. [root@Centos ~]# yum install logrotate libgomp gcc cpp binutils kernel-headers glibc-headers glibc-devel -y #安装所需环境
  2. [root@Centos ~]# rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release-3.0-1.noarch.rpm #安装varnish
  3. [root@Centos ~]# yum install varnish -y #安装varnish
  4. [root@Centos ~]# service varnish start
  5. Starting Varnish Cache:                                    [  OK  ]
  6. [root@Centos ~]# cp /etc/sysconfig/varnish /etc/sysconfig/varnish.default #备份varnish默认的配置文件
  7. [root@Centos ~]# sed -i '/VARNISH_LISTEN_PORT=6081/s/6081/80/'  /etc/sysconfig/varnish #varnish默认端口为6081,改为80
  8. [root@Centos ~]# cp /etc/varnish/default.vcl /etc/varnish/default.vcl.default #备份varnish默认的配置文件
  9. [root@Centos ~]# vi /etc/varnish/default.vcl #修改backend的值
  10. backend default {
  11.   .host = "目标IP或域名";
  12.   .port = "目标端口";
  13. }
  14. [root@Centos ~]# cp /etc/sysconfig/varnish /etc/sysconfig/varnish.default
  15. [root@Centos ~]# vim /etc/sysconfig/varnish
  16. VARNISH_STORAGE_SIZE=50% #缓存文件大小
  17. [root@Centos ~]# more /etc/varnish/default.vcl #只缓存文本文件js等跟图片
  18. backend default {
  19.   .host = "baiqiuyi.com";
  20.   .port = "80";
  21. } 
  22. sub vcl_recv {
  23.     remove req.http.X-real-ip;
  24.     set req.http.X-real-ip = client.ip;
  25.     set req.http.X-Forwarded-For = client.ip;
  26. if (req.request == "GET" && req.url ~ "^/[^?]+\.(jpeg|jpg|png|gif|ico|swf|js|css|txt)(\?.*|)$") {
  27.     unset req.http.Cookie;
  28.     }
  29. if (req.url ~ "\.(png|gif|jpg|swf|css|js)$") {
  30. unset req.http.cookie;
  31. }
  32. }
  33. sub vcl_fetch {
  34. if (req.url ~ "\.(png|gif|jpg|swf|css|js)$") {
  35. unset beresp.http.set-cookie;
  36. }
  37. if (req.request == "GET" && req.url ~ "\.(js|css|jpg|png|gif|jpeg|ico)$") {
  38. set beresp.ttl = 365d;
  39. }
  40. }

以下为varnishi的linux版默认配置文件

  1. # This is a basic VCL configuration file for varnish.  See the vcl(7)
  2. # man page for details on VCL syntax and semantics.
  3. #
  4. # Default backend definition.  Set this to point to your content
  5. # server.
  6. #
  7. backend default {
  8.   .host = "127.0.0.1";
  9.   .port = "80";
  10. }
  11. #
  12. # Below is a commented-out copy of the default VCL logic.  If you
  13. # redefine any of these subroutines, the built-in logic will be
  14. # appended to your code.
  15. # sub vcl_recv {
  16. #     if (req.restarts == 0) {
  17. #       if (req.http.x-forwarded-for) {
  18. #           set req.http.X-Forwarded-For =
  19. #               req.http.X-Forwarded-For + ", " + client.ip;
  20. #       } else {
  21. #           set req.http.X-Forwarded-For = client.ip;
  22. #       }
  23. #     }
  24. #     if (req.request != "GET" &&
  25. #       req.request != "HEAD" &&
  26. #       req.request != "PUT" &&
  27. #       req.request != "POST" &&
  28. #       req.request != "TRACE" &&
  29. #       req.request != "OPTIONS" &&
  30. #       req.request != "DELETE") {
  31. #         /* Non-RFC2616 or CONNECT which is weird. */
  32. #         return (pipe);
  33. #     }
  34. #     if (req.request != "GET" && req.request != "HEAD") {
  35. #         /* We only deal with GET and HEAD by default */
  36. #         return (pass);
  37. #     }
  38. #     if (req.http.Authorization || req.http.Cookie) {
  39. #         /* Not cacheable by default */
  40. #         return (pass);
  41. #     }
  42. #     return (lookup);
  43. # }
  44. #
  45. # sub vcl_pipe {
  46. #     # Note that only the first request to the backend will have
  47. #     # X-Forwarded-For set.  If you use X-Forwarded-For and want to
  48. #     # have it set for all requests, make sure to have:
  49. #     # set bereq.http.connection = "close";
  50. #     # here.  It is not set by default as it might break some broken web
  51. #     # applications, like IIS with NTLM authentication.
  52. #     return (pipe);
  53. # }
  54. #
  55. # sub vcl_pass {
  56. #     return (pass);
  57. # }
  58. #
  59. # sub vcl_hash {
  60. #     hash_data(req.url);
  61. #     if (req.http.host) {
  62. #         hash_data(req.http.host);
  63. #     } else {
  64. #         hash_data(server.ip);
  65. #     }
  66. #     return (hash);
  67. # }
  68. #
  69. # sub vcl_hit {
  70. #     return (deliver);
  71. # }
  72. #
  73. # sub vcl_miss {
  74. #     return (fetch);
  75. # }
  76. #
  77. # sub vcl_fetch {
  78. #     if (beresp.ttl <= 0s ||
  79. #         beresp.http.Set-Cookie ||
  80. #         beresp.http.Vary == "*") {
  81. #               /*
  82. #                * Mark as "Hit-For-Pass" for the next 2 minutes
  83. #                */
  84. #               set beresp.ttl = 120 s;
  85. #               return (hit_for_pass);
  86. #     }
  87. #     return (deliver);
  88. # }
  89. #
  90. # sub vcl_deliver {
  91. #     return (deliver);
  92. # }
  93. #
  94. # sub vcl_error {
  95. #     set obj.http.Content-Type = "text/html; charset=utf-8";
  96. #     set obj.http.Retry-After = "5";
  97. #     synthetic {"
  98. # <?xml version="1.0" encoding="utf-8"?>
  99. # <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  100. #  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  101. # <html>
  102. #   <head>
  103. #     <title>"} + obj.status + " " + obj.response + {"</title>
  104. #   </head>
  105. #   <body>
  106. #     <h1>Error "} + obj.status + " " + obj.response + {"</h1>
  107. #     <p>"} + obj.response + {"</p>
  108. #     <h3>Guru Meditation:</h3>
  109. #     <p>XID: "} + req.xid + {"</p>
  110. #     <hr>
  111. #     <p>Varnish cache server</p>
  112. #   </body>
  113. # </html>
  114. # "};
  115. #     return (deliver);
  116. # }
  117. #
  118. # sub vcl_init {
  119. #       return (ok);
  120. # }
  121. #
  122. # sub vcl_fini {
  123. #       return (ok);
  124. # }

2 Comments

  1. 用RAM缓存图片。。。好奢侈

    • 如果硬盘的IO跟不上并发,短时间内只能切到内存里比较实际了