军哥的 LNMP 一键包可谓是站长圈比较老牌也比较出名的了,直到最近的 1.1 版本出来、功能更完善了

军哥 LNMP/LNMPA 安装 Varnish 前端

按照文中的方法,你可以打造属于你的 LNMPV/LNMPAV 环境,现在我们就开始教程吧。!

注意事项:

本教程使用 CentOS + LNMPA 作为示范。

第一步、安装 Varnish 同时进行部分配置:

1、依赖直接使用 yum 安装即可。

yum -y install automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig

如果你是 CentOS 5 就执行:

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

如果你是 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

做好如上措施之后,开始安装好 Varnish:

yum -y install varnish

2、安装好了我们开始配置一下文件:

vim /etc/sysconfig/varnish

A、找到 VARNISH_LISTEN_PORT 这一栏,把 6081 改为 80.

B、找到 VARNISH_LISTEN_ADDRESS 去掉前面的 # 号,在后面写上你的公网 IP(如果是云,网卡没有公网 IP,那么就写内网 IP即可)

例子:

VARNISH_LISTEN_ADDRESS=107.183.14.49
VARNISH_LISTEN_PORT=80

别照抄我的,务必记得改为你自己的公网 IP,无公网就用内网 IP

3、接下来配置 Varnish 规则,如果你是 WordPress,我建议使用网上流传的已经写好的规则:

echo > /etc/varnish/default.vcl;vim /etc/varnish/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 = "80";
}
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 ~ "(www.zntec.cn|xxx.xxx)") {
        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 = 4h;
     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);
}

代码中我的网址和 xxx.xxx 你可以改为你需要缓存的第一个域名、第二个域名等。


第二步、修改 Nginx 配置:

1、刚刚已经配置好了 Varnish,现在来折腾 Nginx,首先编辑 nginx.conf:

vim /usr/local/nginx/conf/nginx.conf

把其中的 listen   80 改为:listen  127.0.0.1:80,看到区别了没?就是在 80 前面加上 IP 和 : 号。

如果你已经新建过站点了,你的站点配置也需要按照这么修改。

2、修改新建站点的脚本:

vim /root/vhost.sh

把里面的 listen  80 都改为 listen  127.0.0.1:80,记得保存。

3、使用如下脚本更新一遍 Nginx:

wget https://cdn.zntec.cn/store/tools/upgrade_nginx/upgrade_nginx.sh;sh upgrade_nginx.sh

选择你喜欢的版本或最新版更新一下吧~ 会自动编译 realip 模块。

最后配置:

在所有站点配置中加入如下代码:

                set_real_ip_from 127.0.0.1;
                real_ip_header X-Forwarded-For;

如果不知道加在哪里,请加在“root  /home/wwwroot/xxx;”的下面,保存。


第三步、重启 Nginx 和 Varnish 并设为开机启动:

service nginx restart
service varnish start
chkconfig varnish on

完成后可以 curl -I 127.0.0.1 测试一下是否可以缓存。


小结

量产文章中。。

目前有32条回应
Comment
Trackback
Loading ....
  • 橘子 回应于2015/11/04 22:59 回复TA

    貌似是给未登录用户的。WP登录用户好像每个页面都重新生成,varnish做缓存意义就不大了

    • Tomas 回应于2015/11/07 19:11 回复TA

      可以自定义规则,让所有情况下都缓存就可以了。

  • 本篇文章没有Trackback
你目前的身份是游客,请输入昵称和电邮!