Ubuntu 12.04 + apache2 + Varnish 4 + drupal7

varnish介紹

Varnish是一款高性能且開源的反向代理服務器和HTTP緩存加速器;
它的作者Poul-Henning Kamp是FreeBSD(註1)的內核開發者之一,他認為現在的計算機比起1975年已經復雜許多。在1975年時,儲存媒介只有兩種:內存與硬盤。但現在計算機系統的內存除了主存外,還包括了CPU內的L1、L2,甚至有L3快取。硬盤上也有自己的快取裝置,因此Squid Cache(註2)自行處理物件替換的架構不可能得知這些情況 而做到最佳化,但操作系統可以得知這些情況 ,所以這部份的工作應該交給操作系統處理,這就是Varnish cache設計架構。
嚴格意義上說,Varnish 是一個高性能的反向代理軟件/軟體,只不過與其出色的緩存功能相比,企業更願意使用其搭建緩存服務器。同時,由於其工作在Web Server 的前端,有一部分企業已經在生產環境中使用其作為舊版本的squid 的替代方案,以在相同的服務器成本下提供更好的緩存效果,Varnish 更是作為CDN(註3) 緩存服務器的可選服務之一。

註1
FreeBSD是一種自由的類Unix作業系統,它起源於AT&T Unix,是經過BSD、386BSD和4.4BSD發展而來的類Unix的一個重要分支。雖然出於法律原因FreeBSD不能稱為Unix,但它是BSD的直接後裔,而BSD歷史上被稱為BSD Unix或Berkeley Unix。FreeBSD擁有超過200名活躍開發者和上千名貢獻者。FreeBSD被認為是自由作業系統中的不知名的巨人。它不是Unix,但如Unix一樣執行,相容POSIX。作為一個作業系統,FreeBSD被認為相當穩健可靠。FreeBSD是以一個完善的作業系統的定位來做開發。其核心、驅動程式以及所有的使用者層(Userland)應用程式(比方說是Shell)均由同一原始碼版本控制系統儲存(目前使用Subversion)。相較於另一知名的類Unix作業系統GNU/Linux,其核心Linux為一組開發人員開發設計,而其它元件則是來自他人開發的程式(如GNU計劃),最後再由另外的團體整合並包裝成Linux套件。FreeBSD預設是無桌面環境的命令列介面,想要使用桌面環境必須自行安裝,或是使用PC-BSD之類的桌面發行版。現在,FreeBSD被許多諸如IBM,Nokia,Juniper Networks和NetApp等公司用來打造自己的產品。現在蘋果的Mac OS X作業系統的某些部分就是基於FreeBSD的。PlayStation 3的作業系統也借用了FreeBSD的某些部件,PlayStation 4也源於FreeBSD。
註2
Squid Cache(簡稱為Squid)是HTTP代理伺服器軟體。Squid用途廣泛的,可以作為快取伺服器,可以過濾流量幫助網路安全,也可以作為代理伺服器鏈中的一環,向上級代理轉發資料或直接連線網際網路。可以說varnish的出現,就是為了取代squid cache 在快取方面的問題。實際案例:挪威最大的在線報紙Verdens Gang使用3台Varnish代替了原來的12台Squid,性能比以前更好。–節錄自百度百科
註3
內容傳遞網路(Content delivery network或Content distribution network,縮寫:CDN)是指一種透過網際網路互相連接的電腦網路系統,提供高效能、可擴展性、及低成本的網路將內容傳遞給使用者。

Varnish特點

  • 是基於內存緩存,重啟後數據將消失
  • 利用虛擬內存方式,io性能好
    -支持設置0~60秒內的精確緩存時間
  • VCL配置管理比較靈活
  • 32位機器上緩存文件大小為最大2G
  • 具有強大的管理功能,例如top,stat,admin,list等
  • 狀態機設計巧妙,結構清晰
  • 利用二叉堆管理緩存文件,達到積極刪除目的

I/O(英語:Input/Output),即輸入/輸出,通常指資料在記憶體和外部記憶體或其他周邊裝置之間的輸入和輸出。
二叉堆:簡單地說就是一種有序隊列而已,普通的隊列是先入先出,而二叉堆是:最小先出。

主要效果:

網站快取伺服器
快取js.html.jpg….等等,並且保護伺服器執行cpu
實績:
Varnish目前被用在挪威最大的報社Verdens Gang上
挪威最大的在綫報紙 Verdens
Gang(vg.no)使用3臺Varnish代替了原來的12臺squid,性能比以前更好

varnish4在drupal7上

drupal 安裝 varnish 4 相比 varnish 3 ,最主要的是 settings.php 不用做出更改,減少了網站壞
掉的風險,varnish 4 使用 drupal 內建的效能模組控制,開啟與關閉,甚至連 cache 清除也可
設定,比起 varnish 3 可以說少了很多風險。

安裝方法

1.系統更新

$ sudo apt-get update
$ sudo apt-get upgrade -y

2.安裝 varnish

$ sudo apt-get install apt-transport-https

安裝套件

curl https://repo.varnish-cache.org/ubuntu/GPG-key.txt | apt-key add -

讓 apt 能夠知道 varnish 4

echo "deb https://repo.varnish-cache.org/ubuntu/ precise varnish-4.0" >>/etc/apt/sources.list.d/varnish-cache.list

重新 update

apt-get update

安裝 varnish

apt-get install varnish

3.設定 varnish

nano /etc/default/varnish

更改成監聽 80port 與更改記憶體 ( 預設 256m)

DAEMON_OPTS="-a :6082 
-T localhost:6082 
-f /etc/varnish/default.vcl 
-S /etc/varnish/secret 
-s malloc,256m"

-a 監聽 port
-T 管理 port
-f varnish 的詳細設定檔位子
-S varnish 的 key
-s 內存設定

儲存後,更改 default.vcl
nano /etc/varnish/default.vcl
當然在編輯前建議先複製一份改名以避免設定出錯後無法復原

默認後端的定義。將其設置為指向您的 web 服務器。

backend default {
.host = "127.0.0.1";
.port = "8080";}

在default.vcl內最下方貼上:

sub vcl_recv {

    # Return (pass) instructs Varnish not to cache the request
    # when the condition is met.

    ## ADMIN PAGES ##

    # Here we filter out all URLs containing Drupal administrative sections
    if (req.url ~ "^/status.php$" ||
        req.url ~ "^/update.php$" ||
        req.url ~ "^/admin$" ||
        req.url ~ "^/admin/.*$" ||
        req.url ~ "^/user$" ||
        req.url ~ "^/user/.*$" ||
        req.url ~ "^/flag/.*$" ||
        req.url ~ "^.*/ajax/.*$" ||
        req.url ~ "^.*/ahah/.*$") {
           return (pass);
    }


    ## BACKUP AND MIGRATE MODULE ##

    # Backup and Migrate is a very popular Drupal module that needs to be excluded
    # It won't work with Varnish
    if (req.url ~ "^/admin/content/backup_migrate/export") {
        return (pipe);
    }

    ## COOKIES ##

    # Remove cookies for stylesheets, scripts, and images used throughout the site.
    # Removing cookies will allow Varnish to cache those files.
    if (req.url ~ "(?i).(css|js|jpg|jpeg|gif|png|ico)(?.*)?$") {
        unset req.http.Cookie;
    }

    # Remove all cookies that are not necessary for Drupal to work properly.
    # Since it would be cumbersome to REMOVE certain cookies, we specify
    # which ones are of interest to us, and remove all others. In this particular
    # case we leave SESS, SSESS and NO_CACHE cookies used by Drupal's administrative
    # interface. Cookies in cookie header are delimited with ";", so when there are
    # many cookies, the header looks like "Cookie1=value1; Cookie2=value2; Cookie3..." 
    # and so on. That allows us to work with ";" to split cookies into individual
    # ones.
    #
    # The method for filtering unnecessary cookies has been adopted from:
    # https://fourkitchens.atlassian.net/wiki/display/TECH/Configure+Varnish+3+for+Drupal+7
    if (req.http.Cookie) {
        # 1. We add ; to the beginning of cookie header
        set req.http.Cookie = ";" + req.http.Cookie;
        # 2. We remove spaces following each occurence of ";". After this operation
        # all cookies are delimited with no spaces.
        set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
        # 3. We replace ";" INTO "; " (adding the space we have previously removed) in cookies
        # named SESS..., SSESS... and NO_CACHE. After this operation those cookies will be 
        # easy to differentiate from the others, because those will be the only one with space
        # after ";"   
        set req.http.Cookie = regsuball(req.http.Cookie, ";(SESS[a-z0-9]+|SSESS[a-z0-9]+|NO_CACHE)=", "; 1=");
        # 4. We remove all cookies with no space after ";", so basically we remove all cookies other
        # than those above.
        set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
        # 5. We strip leading and trailing whitespace and semicolons.
        set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

        # If there are no cookies after our striping procedure, we remove the header altogether,
        # thus allowing Varnish to cache this page
        if (req.http.Cookie == "") {
            unset req.http.Cookie;
        }
        # if any of our cookies of interest are still there, we disable caching and pass the request
        # straight to Apache and Drupal
        else {
            return (pass);
        }
    }
}


sub vcl_backend_response {
    # Remove cookies for stylesheets, scripts and images used throughout the site.
    # Removing cookies will allow Varnish to cache those files. It is uncommon for
    # static files to contain cookies, but it is possible for files generated
    # dynamically by Drupal. Those cookies are unnecessary, but could prevent files
    # from being cached.
    if (bereq.url ~ "(?i).(css|js|jpg|jpeg|gif|png|ico)(?.*)?$") {
        unset beresp.http.set-cookie;
    }
}


儲存後重新啟動varnish

service varnish restart

4.web伺服器的port 80 改port 8080

apache2

nano /etc/apache2/ports.conf

NameVirtualHost *:8080
Listen 8080

nano /e tc/apache2/sites-available/default


更改完後重啟apache2

service apache2 restart

5.drupal設定

開啟drupal的網頁

登入後,設定>開發>效能

勾選

匿名使用者頁面快取

下面的

  • 最短的快取保存時間
  • 快取頁面有效期限

ex:都選擇1分鐘
這樣每過1分鐘快取緩存就會清除並在再次生成
這樣外面匿名使用者觀看網頁時,過1分鐘重新回首頁,會出現編輯者剛編輯文章

一般網站設定5分鐘
如果不是即時網站,快取清除大概30分鐘左右即可。

測試資料

測試機資料(vagrant )

沒加varnish

這次測試的時候cup跑到70%

加入varnish

這時的cpu 在0.2%上下,跟沒有啟用varnish相比起來,varnish不僅讓apache2每次秒處理數上升,更減少了伺服器cpu的消耗