Varnish第二版配置
之前有写过一个Varnish,用起来感觉怪怪的。准确地说是有人反应会出现时快时慢的感觉。这几天参考官方文档重新配置过了Varnish。现在的Varnish主要是跟Apache配合来使用。暂时还没有以独立的服务器来跑。因为个人的BLog没有什么流量。呵呵。。
FreeBSD安装软件就是简单
- portmaster /usr/ports/www/varnish
- vi /etc/sysctl.conf
- kern.ipc.somaxconn=16384
- kern.maxfiles=131072
- kern.maxfilesperproc=104856
- kern.threads.max_threads_per_proc=4096
- net.inet.tcp.blackhole=2
- net.inet.udp.blackhole=1
- net.inet.icmp.icmplim_output=0
- vi /etc/rc.conf
- varnishd_enable="YES"
- varnishd_listen=":80"
将Apache监听的端口改为8080
- vi /usr/local/etc/apache22/http.conf
- #大概在第40行左右。使用:40可以直接到
- Listen 8080
- vi /usr/local/etc/varnish/default.vcl
- #-e 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";
- }
- #
- #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 {
- # remove req.http.X-Forwarded-For;
- # set req.http.X-Forwarded-For = client.ip;
- set req.grace = 30s;
- 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.http.Cache-Control ~ "no-cache") {
- purge_url(req.url);
- }
- 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) {
- /* 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 req.http.connection = "close";
- # # here. It is not set by default as it might break some broken web
- # # applications, like IIS with NTLM authentication.
- set bereq.http.connection = "close";
- return (pipe);
- }
- #
- sub vcl_pass {
- return (pass);
- }
- #
- sub vcl_hash {
- set req.hash += req.url;
- if (req.http.Accept-Encoding ~ "gzip") {
- set req.hash += "gzip";
- } else if (req.http.Accept-Encoding ~ "deflate") {
- set req.hash += "deflate";
- }
- if (req.http.host) {
- set req.hash += req.http.host;
- } else {
- set req.hash += server.ip;
- }
- return (hash);
- }
- #
- sub vcl_hit {
- if (req.request == "PURGE") {
- set obj.ttl = 0s;
- error 200 "Purged.";
- }
- if (!obj.cacheable) {
- return (pass);
- }
- return (deliver);
- }
- #
- sub vcl_miss {
- if (req.request == "PURGE") {
- error 404 "Not in cache.";
- }
- return (fetch);
- }
- #
- sub vcl_fetch {
- set obj.grace = 30s;
- if (!obj.cacheable) {
- return (pass);
- }
- if (obj.http.Set-Cookie) {
- return (pass);
- }
- set obj.prefetch = -30s;
- return (deliver);
- }
- #
- sub vcl_deliver {
- if (obj.hits > 0) {
- set resp.http.X-Cache = "HIT";
- } else {
- set resp.http.X-Cache = "MISS";
- }
- return (deliver);
- }
- #
- sub vcl_discard {
- # /* XXX: Do not redefine vcl_discard{}, it is not yet supported */
- return (discard);
- }
- #
- sub vcl_prefetch {
- # /* XXX: Do not redefine vcl_prefetch{}, it is not yet supported */
- return (fetch);
- }
- #
- sub vcl_timeout {
- # /* XXX: Do not redefine vcl_timeout{}, it is not yet supported */
- return (discard);
- }
- #
- sub vcl_error {
- set obj.http.Content-Type = "text/html; charset=utf-8";
- 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>
- <address>
- <a href="http://www.varnish-cache.org/">Varnish</a>
- </address>
- </body>
- </html>
- "};
- return (deliver);
- }
欢迎各位看客指出我配置上的问题。我也是在学习使用中。