日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

varnish的服务配置(实验说明)

發布時間:2025/3/19 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 varnish的服务配置(实验说明) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實驗環境

  • varnish ——172.25.54.5 server1(虛擬機test1)
  • apache ——172.25.54.6 server2(虛擬機test2)
  • apache ——172.25.54.7 server3(虛擬機test3)

每個實驗都在前一個實驗的基礎上

實驗一、簡單實現varnish

配置varnish服務器(server1)

  • 安裝varnish yum install varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm -y
    varnish 安裝包
  • 配置varnish服務器server1
[root@server1 ~]# yum install varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm -y [root@server1 ~]# cd /etc/varnish/ [root@server1 varnish]# ls default.vcl secret [root@server1 varnish]# ll default.vcl -rw-r--r-- 1 root root 3119 Dec 2 2013 default.vcl [root@server1 varnish]# ll secret -rw------- 1 root root 37 Jul 28 14:00 secret [root@server1 varnish]# vim default.vcl ##配置一個后端服務器7 backend default {8 .host = "172.25.54.6";9 .port = "80";10 } [root@server1 varnish]# vim /etc/sysconfig/varnish ##配置varnish服務端口66 VARNISH_LISTEN_PORT=80 [root@server1 varnish]# sysctl -a | grep file fs.file-nr = 512 0 98862 fs.file-max = 98862 [root@server1 varnish]# cd /lib64/security/ [root@server1 security]# ll pam_limits.so -rwxr-xr-x. 1 root root 18592 Oct 7 2013 pam_limits.so [root@server1 security]# vim /etc/security/limits.conf 52 varnish - nofile 9800053 varnish - memlock 8200054 varnish - nproc unlimited [root@server1 security]# /etc/init.d/varnish start Starting Varnish Cache: [ OK ] [root@server1 security]# cd /etc/varnish/ [root@server1 varnish]# /etc/init.d/varnish reload Loading vcl from /etc/varnish/default.vcl Current running config name is reload_2018-07-28T14:32:35 Using new config name reload_2018-07-28T14:33:25 VCL compiled.available 0 boot available 0 reload_2018-07-28T14:32:35 active 0 reload_2018-07-28T14:33:25Done [root@server1 varnish]#
  • apache服務器server2
[root@server2 html]# yum install httpd -y [root@server2 ~]# /etc/init.d/httpd start Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.54.2 for ServerName[ OK ] [root@server2 ~]# cd /var/www/html/ [root@server2 html]# vim index.html [root@server2 html]# cat index.html www.westos.org - server2 [root@server2 html]#
  • 真機測試
[root@foundation54 ~]# vim /etc/hosts [root@foundation54 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.25.54.5 www.westos.org bbs.westos.org westos.org [root@foundation54 ~]# curl www.westos.org <h1>www.westos.org - server2</h1> [root@foundation54 ~]#

實驗二、查看緩存情況

  • 配置server1
[root@server1 varnish]# vim default.vcl12 sub vcl_deliver {13 if (obj.hits > 0) {14 set resp.http.X-Cache = "HIT from westos cache"; ##命中緩存15 }16 else {17 set resp.http.X-Cache = "MISS from westos cache"; ##未命中緩存18 }19 return (deliver);20 } [root@server1 varnish]# /etc/init.d/varnish stop Stopping Varnish Cache: [ OK ] [root@server1 varnish]# /etc/init.d/varnish start Starting Varnish Cache: [ OK ] [root@server1 varnish]# /etc/init.d/varnish reload Loading vcl from /etc/varnish/default.vcl Current running config name is boot Using new config name reload_2018-07-28T14:58:29 VCL compiled.available 0 boot active 0 reload_2018-07-28T14:58:29Done [root@server1 varnish]#
  • 真機測試緩存命中

    清除緩存
    [root@server1 varnish]# varnishadm ban.url /index.html
    再測試

    沒有改變,是因為存在瀏覽器緩存的問題,用 curl 測試


清除緩存
[root@server1 varnish]# varnishadm ban.url /index.html

實驗三、定義多個不同域名站點的后端服務器

  • 配置server1
[root@server1 varnish]# vim default.vcl ##定義多個不同域名站點的后端服務器7 backend web1 {8 .host = "172.25.54.6";9 .port = "80";10 }11 12 backend web2 {13 .host = "172.25.54.7";14 .port = "80";15 }##當訪問 www.westos.org 域名時從 web1 上取數據,##訪問 bbs.westos.org 域名時到 web2 取數據,##訪問其他頁面報錯17 sub vcl_recv {18 if (req.http.host ~ "^(www.)?westos.org") {19 set req.http.host = "www.westos.org";20 set req.backend = web1;21 } elsif (req.http.host ~ "^bbs.westos.org") {22 set req.backend = web2;23 } else {error 404 "westos cache";24 }25 }26 27 sub vcl_deliver {28 if (obj.hits > 0) {29 set resp.http.X-Cache = "HIT from westos cache";30 }31 else {32 set resp.http.X-Cache = "MISS from westos cache";33 }34 return (deliver);35 } [root@server1 varnish]# /etc/init.d/varnish reload Loading vcl from /etc/varnish/default.vcl Current running config name is reload_2018-07-28T14:58:29 Using new config name reload_2018-07-28T15:19:24 VCL compiled.available 0 boot available 2 reload_2018-07-28T14:58:29 active 0 reload_2018-07-28T15:19:24Done [root@server1 varnish]#
  • 配置server3
[root@server3 ~]# yum install httpd -y [root@server3 ~]# /etc/init.d/httpd start Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.54.3 for ServerName[ OK ] [root@server3 ~]# cd /var/www/html/ [root@server3 html]# ls [root@server3 html]# vim index.html [root@server3 html]# cat index.html bbs.westos.org [root@server3 html]#
  • 真機測試


實驗四、把多個后端聚合為一個組

  • 配置server3
[root@server3 html]# vim /etc/httpd/conf/httpd.conf 990 NameVirtualHost *:80 1010 <VirtualHost *:80> 1011 DocumentRoot /var/www/html 1012 ServerName bbs.westos.org 1013 </VirtualHost> 1014 1015 <VirtualHost *:80> 1016 DocumentRoot /www1 1017 ServerName www.westos.org 1018 </VirtualHost> [root@server3 html]# mkdir /www1 [root@server3 html]# cd /www1/ [root@server3 www1]# ls [root@server3 www1]# vim index.html [root@server3 www1]# cat index.html www.westos.org - server3 [root@server3 www1]# /etc/init.d/httpd restart [root@server3 www1]# vim /etc/hosts [root@server3 www1]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.25.54.7 server7 www.westos.org bbs.westos.org [root@server3 www1]# curl www.westos.org www.westos.org - server3 [root@server3 www1]# curl bbs.westos.org bbs.westos.org [root@server3 www1]#
  • 配置server1
[root@server1 varnish]# vim default.vcl 17 director tutu round-robin {18 {.backend = web1;}19 {.backend = web2;}20 }21 22 sub vcl_recv {23 if (req.http.host ~ "^(www.)?westos.org") {24 set req.http.host = "www.westos.org";25 set req.backend = tutu;26 return (pass); #為了測試方便,不進行緩存27 } elsif (req.http.host ~ "^bbs.westos.org") {28 set req.backend = web2;29 } else {error 404 "westos cache";30 }31 } [root@server1 varnish]# /etc/init.d/varnish reload Loading vcl from /etc/varnish/default.vcl Current running config name is reload_2018-07-28T15:19:24 Using new config name reload_2018-07-28T15:50:54 VCL compiled.available 0 boot available 0 reload_2018-07-28T14:58:29 available 2 reload_2018-07-28T15:19:24 active 0 reload_2018-07-28T15:50:54Done [root@server1 varnish]#
  • 真機測試
[root@foundation54 ~]# curl www.westos.org www.westos.org - server2 [root@foundation54 ~]# curl www.westos.org www.westos.org - server3 [root@foundation54 ~]# curl www.westos.org www.westos.org - server2 [root@foundation54 ~]# curl www.westos.org www.westos.org - server3 [root@foundation54 ~]# curl www.westos.org www.westos.org - server2 [root@foundation54 ~]# curl www.westos.org www.westos.org - server3 [root@foundation54 ~]#

實驗五、varnish cdn 推送平臺

  • 配置server1
[root@server1 ~]# yum install lftp php unzip -y [root@server1 ~]# ls bansys.zip [root@server1 ~]# unzip bansys.zip -d /var/www/html/ [root@server1 ~]# cd /var/www/html/ [root@server1 html]# ls bansys [root@server1 html]# cd bansys/ [root@server1 bansys]# ls class_socket.php config.php index.php purge_action.php static [root@server1 bansys]# mv * .. [root@server1 bansys]# ls [root@server1 bansys]# cd .. [root@server1 html]# ls bansys class_socket.php config.php index.php purge_action.php static [root@server1 html]# vim config.php

[root@server1 html]# yum install httpd -y [root@server1 html]# vim /etc/httpd/conf/httpd.conf 136 Listen 8080 ##為了避免與varnish沖突 [root@server1 html]# /etc/init.d/httpd start [root@server1 html]# netstat -antpl Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 :::8080 :::* LISTEN 1422/httpd


[root@server1 html]# cd /etc/varnish/ [root@server1 varnish]# ls default.vcl secret [root@server1 varnish]# vim default.vcl ##添加7 acl westos {8 "127.0.0.1";9 "172.25.54.0"/24;10 }sub vcl_recv {28 ##在函數內添加29 if (req.request == "BAN") {30 if (!client.ip ~ westos) {31 error 405 "Not allowed.";32 }33 ban("req.url ~ " + req.url);34 error 200 "ban added";35 }36 #####37 if (req.http.host ~ "^(www.)?westos.org") {38 set req.http.host = "www.westos.org";39 set req.backend = tutu;41 } elsif (req.http.host ~ "^bbs.westos.org") {42 set req.backend = web2;43 } else {error 404 "westos cache";44 }45 } [root@server1 varnish]# /etc/init.d/varnish reload Loading vcl from /etc/varnish/default.vcl Current running config name is boot Using new config name reload_2018-07-29T09:43:47 VCL compiled.available 2 boot active 0 reload_2018-07-29T09:43:47Done [root@server1 varnish]#





瀏覽器頁面未改變是因為瀏覽器緩存導致的

  • 瀏覽器推送+shell的curl測試

[root@foundation54 ~]# curl www.westos.org/index.html <h1>www.westos.org - server3</h1> [root@foundation54 ~]# curl www.westos.org/index.html <h1>www.westos.org - server3</h1> [root@foundation54 ~]# curl www.westos.org/index.html <h1>www.westos.org - server3</h1> [root@foundation54 ~]#


[root@foundation54 ~]# curl www.westos.org/index.html <h1>www.westos.org - server2</h1> [root@foundation54 ~]# curl www.westos.org/index.html <h1>www.westos.org - server2</h1> [root@foundation54 ~]# curl www.westos.org/index.html <h1>www.westos.org - server2</h1> [root@foundation54 ~]#

總結

以上是生活随笔為你收集整理的varnish的服务配置(实验说明)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。