版本:
ubuntu 16.04
nginx-1.5.11
varnish-3.0.4
tomcat 7
参考链接:
https://www.jianshu.com/p/00e1ab183b6b
https://www.jianshu.com/p/ffdc53ed0115
在上面的第二个链接的文章里面,我完成了varnish的安装
存在的问题就是配置文件的配置不知道是个蛇
开始我也是一头雾水
后来在同学的帮助下实现了varnish、nginx及tomcat之间的整合
这篇文档就重新把varnish给装上吧
1.varnish的编译安装
dss@dss:/usr/local$ ls -l varnish-install.sh
-rw-r--r-- 1 root root 552 7月 2 22:49 varnish-install.sh
脚本内容:
#!/bin/bash
sudo apt-get install libpcre3-dev automake autotools-dev
sudo apt-get install pkg-config python-docutils python-sphinx
sudo apt-get install libedit-dev libjemalloc-dev
sudo apt-get install ncurses-dev
cd /usr/local
sudo mkdir varnish
sudo mkdir varnish/bin
sudo mkdir varnish/sbin
sudo wget http://varnish-cache.org/_downloads/varnish-3.0.4.tgz
sudo tar -vxzf varnish-3.0.4.tgz
cd varnish-3.0.4
sudo ./configure --prefix=/usr/local/varnish --bindir=/usr/local/varnish/bin--sbindir=/usr/local/varnish/sbin
sudo make
sudo make install
以上就是制作的脚本了!(其实就是把我编译安装的命令给打包起来.执行脚本也就是从上往下一步一步的执行命令!)
那么接下来运行脚本即可!!
dss@dss:/usr/local$ sudo sh varnish-install.sh
dss@dss:/usr$ ls -l local
drwxr-xr-x 9 root root 4096 7月 2 22:54 varnish
drwxr-xr-x 10 dss dss 4096 7月 2 22:53 varnish-3.0.4
-rw-r--r-- 1 root root 2081361 6月 30 01:36 varnish-3.0.4.tgz
-rw-r--r-- 1 root root 552 7月 2 22:49 varnish-install.sh
varnish的安装就已完成!
2.配置varnish
varnish的配置文件位置,先对生成的配置文件备份,有备无患嘛!
dss@dss:/usr/local/varnish/etc/varnish$ ls
default2018-7-2.vcl 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 = "192.168.3.12";
.port = "80";
}
#
# 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 {
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) {
/* 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 {
return (deliver);
}
sub vcl_miss {
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);
}
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);
}
其实这里只做了很简单的改动!
在源文件的基础上,将所有的方法前面的#号去除!
-
若配置文件没有配置好,会出现这样的错误:
Message from VCC-compiler:
No backends or directors found in VCL program, at least one is necessary. -
根据tomcat的端口号改动下面部分的内容:
tomcat的负载均衡:https://www.jianshu.com/writer#/notebooks/26994302/notes/30048485/writing/usr/local/tomcat7A/conf$ sudo vim server.xml <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
注:这样做只用到了varnish很少的功能,要进一步开发的话,就需要在配置文件里添加好多东西了呢!!
3.开启
完成了上面的工作之后就是开启这些咚咚了!!
- 开启tomcat
dss@dss:/usr/local/tomcat7A/bin$ sudo sh startup.sh
Using CATALINA_BASE: /usr/local/tomcat7A
Using CATALINA_HOME: /usr/local/tomcat7A
Using CATALINA_TMPDIR: /usr/local/tomcat7A/temp
Using JRE_HOME: /usr/local/jkd8
Using CLASSPATH: /usr/local/tomcat7A/bin/bootstrap.jar:/usr/local/tomcat7A/bin/tomcat-juli.jar
Tomcat started.
- 开启varnish
dss@dss:/usr/local/varnish/sbin$ sudo ./varnishd -f /usr/local/varnish/etc/varnish/default.vcl -T 192.168.3.12:2000 -a 0.0.0.0:80 -s file,/tmp,200M
开启成功!
4.在网页上显示
项目是我原来在tomcat均衡负载时创建的呢!
详情请参考:https://www.jianshu.com/p/00e1ab183b6b
刷新一下
大功告成!!
people people for me ,me for people people!!