想起很久以前没有配置好的Varnish,于是便开始继续在自己的Instance上配置。
1.安装
yum install varnish
2.修改网站对应的端口
server
{
listen 127.0.0.1:80;
server_name aws.phodal.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/www/MK_zhonghua;
include wordpress.conf;
location ~ .*\.(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
}
注意
: 这里需要将listen 80;改为127.0.0.1:80,以免Nginx Varnish端口之间的端口冲突。
3.修改/etc/sysconfig/varnish
查看一下自己的内网或者外网IP,如
ifconfig
就会得到下面的结果
eth0 Link encap:Ethernet HWaddr 06:45:0E:9B:AA:77
inet addr:172.31.26.24 Bcast:172.31.31.255 Mask:255.255.240.0
inet6 addr: fe80::445:eff:fe9b:aa77/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:9001 Metric:1
RX packets:201818 errors:0 dropped:0 overruns:0 frame:0
TX packets:152864 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:23804259 (22.7 MiB) TX bytes:190045784 (181.2 MiB)
Interrupt:72
内网的IP就是172.31.26.24,拿到对应的结果,修改到/etc/sysconfig/varnish中。
VARNISH_LISTEN_ADDRESS=172.31.26.21
VARNISH_LISTEN_PORT=80
将VARNISH_LISTEN_ADDRESS改为内网的IP,PORT改为80.
4.修改/etc/varnish/default.vcl
backend phodalcms {
.host = "127.0.0.1";
.port = "80";
}
backend phodalblog {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_recv {
if (req.http.host == "aws.phodal.com") {
set req.http.host = "aws.phodal.com";
set req.backend = phodalcms;
return (lookup);
} else if (req.http.host == "aws1.phodal.com") {
set req.http.host = "aws1.phodal.com";
set req.backend = phodalblog;
return (lookup);
}
}
phodalcms对应的是80,对应的host是aws.phodal.com 而8080便是另外一个应用,对应的host是aws1.phodal.com
5.验证varnish default.vcl配置是否正确
varnishd -C -f /etc/varnish/default.vcl
6.重启服务
service nginx restart
service varnish restart
7.验证是否成功
curl -I -s aws.phodal.com
注意下面的X-Varnish
HTTP/1.1 200 OK
Server: phodal/0.17.5
Content-Type: text/html; charset=UTF-8
Vary: Accept-Encoding, Accept-Encoding, Cookie
X-Powered-By: PHP/5.6.0
WP-Super-Cache: Served supercache file from PHP
X-Page-Speed: Powered By Phodal
Cache-Control: max-age=0, no-cache
Date: Tue, 23 Sep 2014 13:38:13 GMT
X-Varnish: 18117040
Age: 0
Via: 1.1 varnish
Connection: keep-alive
后台查看
0+04:31:57
Hitrate ratio: 2 2 2
Hitrate avg: 0.0745 0.0745 0.0745
14768 0.00 0.91 client_conn - Client connections accepted
14734 0.00 0.90 client_req - Client requests received
7 0.00 0.00 cache_hit - Cache hits
14684 0.00 0.90 cache_hitpass - Cache hits for pass
41 0.00 0.00 cache_miss - Cache misses
11192 0.00 0.69 backend_conn - Backend conn. success
3492 0.00 0.21 backend_fail - Backend conn. failures
43 0.00 0.00 backend_reuse - Backend conn. reuses
14 0.00 0.00 backend_toolate - Backend conn. was closed
59 0.00 0.00 backend_recycle - Backend conn. recycles
查看80端口
lsof -i:80
查看所有网络相关端口及信息
netstat -punta
返回结果如下
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 19636/hhvm
tcp 0 0 0.0.0.0:3080 0.0.0.0:* LISTEN 2294/thin server (0
tcp 0 0 0.0.0.0:9997 0.0.0.0:* LISTEN 1655/python27
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 18439/nginx
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1507/sshd
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 18439/nginx
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1680/sendmail
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 18439/nginx
围观我的Github Idea墙, 也许,你会遇到心仪的项目