Nginx 代理 varnish 的姿式

之前作網站的時候遇到了網站的訪問量很大,而致使後端處理程序響應超時而致使的一些問題。當時採用的架構是nginx+php-fastcgi,同事想到了用nginx-proxycache來作頁面緩存,效果也還行。下面我想介紹一下varnish的使用技巧javascript

準備

varnish嚴格來講是能夠看成一個代理服務器的軟件,直接將HTTP請求轉發到php-cgi,而後交給php處理,varnish會獲取通過php處理後的數據,最後返回給瀏覽器。如圖php

fastcig

可是,如今php-fastcgi已經被逐漸淘汰了,也就是說咱們通常狀況下不會使用php-fastcgi,那麼咱們不能直接將varnish與php組合,由於php-fpm的交互方式爲socket,而再也不是監聽本機的9000端口
因此咱們必須找一個的媒介,鏈接varnishphp-fpm,nginx能夠扮演這個媒介,以下圖:css

varnish

那麼問題來了,根據研究發現,varnish處理http請求不如nginx那麼高效。因此若是咱們讓nginx作前鋒,這樣就更完美了。那咱們須要怎麼才能達到這個目的呢,下面咱們來整理一下流程html

圖片描述

下面就來實現一下圖三的架構吧。
事先須要準備nginx,varnish,php-fpm,php這些軟件,OS是ubuntu,全部軟件均可以用apt-get install來安裝,不瞭解包名全稱的話能夠先apt-get update,更新一下源,而後再用apt-cache search xxx來查找軟件包名java

安裝完varnish後,能夠使用service varnish回車,查看可操做選項* Usage: /etc/init.d/varnish {start|stop|restart|reload|force-reload|configtest},通常安裝完畢後,系統會自動啓動varnish的,nginx也是同樣,便不贅述了node

配置

安裝完所需的軟件後,下面須要配置這些軟件,來實現這個架構nginx

nginx部分

vi /etc/nginx/nginx.confweb

http {
    ## proxy global setting
    proxy_connect_timeout 5;
    proxy_read_timeout 60;
    proxy_send_timeout 5;
    proxy_buffer_size 16k;
    proxy_buffers 4 64k;
    proxy_busy_buffers_size 128k;
    ##END
    ## cache proxy pass
    upstream cache {
            server  127.0.0.1:6081;
    }
    ##END
    ## php proxy pass
    upstream php { 
            server  127.0.0.1:8080;
    }
    ##END
    # Basic Settings
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;
    #depend on nginx-extras 須要安裝nginx-extras才能定義Server
    more_set_headers 'Server: Bird-shark';
    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    ##
    # SSL Settings
    ##
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;
    ##
    # Logging Settings
    ##
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    ##
    # Gzip Settings
    ##
    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    ##
    # Virtual Host Configs
    ##
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;  
}

vi /etc/nginx/sites-available/defaultchrome

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    index index.html index.htm index.php;
    server_name localhost;
    location ~ .*\.(gif|jpg|png|css|js|flv|ico|swf|html)$ {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://cache;
    }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location / {
        proxy_pass http://php;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass_header Server;
    }
}
server {
    listen 8080;
    root /var/www/html;
    index  index.html index.htm index.php;
    location / {
        if (!-e $request_filename){
            rewrite  ^(.*)$  /index.php?s=$1  last;
            break;
        }
        try_files $uri $uri/ =404;
    }
    location ~ ^(.+\.php)(.*)$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_intercept_errors on;
        fastcgi_buffers 8 128k;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

varnish部分

vi /etc/varnish/default.vcljson

vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}
acl purgers {
    "localhost";
    #"103.22.188.169";
}
sub vcl_recv {
    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
    if (req.restarts == 0) {
        unset req.http.X-Purger;
    }
    if(req.method == "PURGE"){
        if(!client.ip ~ purgers){
            return(synth(405,"Not Allowed."));
        }
        return (purge);
        #ban("obj.http.x-url ~ " + req.url);
    }
    if(req.method == "GET" && req.url ~ "\.(js|css|jpg|png|gif|swf|jpeg|ico)$"){
        unset req.http.cookie;
    }
}

sub vcl_backend_response {
    #set beresp.http.x-url = bereq.url;
    if (bereq.url ~ "\.(js|css|jpg|png|gif|swf|jpeg|ico)$") {
        unset beresp.http.Cache-Control;
        unset beresp.http.set-cookie;
        set beresp.ttl = 10h;
        set beresp.http.Cache-Control = "max-age=36000";
        set beresp.do_gzip = true;
    }
    if(bereq.url ~ "\.html$"){
        set beresp.ttl = 10m;
        set beresp.do_gzip = true;
        unset beresp.http.Cache-Control;
        unset beresp.http.Pragma;
        set beresp.http.Cache-Control = "max-age=600";
        unset beresp.http.Expires;
    }
}

sub vcl_deliver {
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT from " + req.http.host;
        #set resp.http.X-Cache-Hits = obj.hits;
    } else {
        set resp.http.X-Cache = "MISS from " + req.http.host;
    }
    if (req.http.X-Purger) {
        set resp.http.X-Purger = req.http.X-Purger;
    }
    unset resp.http.X-Powered-By;
    unset resp.http.Server;
     
    unset resp.http.Via;
    unset resp.http.X-Varnish;
     
    unset resp.http.Age;
    #unset resp.http.x-url; # Optional
}
sub vcl_hit {
    if (req.method == "PURGE") {
        return (synth(200,"Purged."));
    }
}
sub vcl_miss {
    if (req.method == "PURGE") {
        return (synth(404,"Purged."));
    }
}
sub vcl_purge {
    if (req.method == "PURGE") {
        #set req.http.X-Purge = "Purged";
        ban("req.url ~ "+req.url);
        #return (restart);
        set req.method = "GET";
        set req.http.X-Purger = "Purged";
        return (restart);
    }
}
sub vcl_hash {
    hash_data(req.url);
    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }
    if (req.http.cookie) {
        hash_data(req.http.cookie);
    }
    if (req.http.Accept-Encoding ~ "gzip") {
        hash_data("gzip");
    } elseif (req.http.Accept-Encoding ~ "deflate") {
        hash_data("deflate");
    }
}

測試&分析

1. 在不使用緩存模塊的狀況下

vi /etc/nginx/sites-available/default

#location ~ .*\.(gif|jpg|png|css|js|flv|ico|swf|html)$ {
#    proxy_set_header Host $host;
#    proxy_set_header X-Real-IP $remote_addr;
#    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#    proxy_pass http://cache;
#}

先用chrome瀏覽器訪問查看請求頭

curl-1

咱們再使用curl,在服務器上執行如下命令

curl -k -v 'http://192.168.99.1/Public/Home/images/t_navigation_logo.png' -H 'Pragma: no-cache' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: zh,en;q=0.8,zh-CN;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Connection: keep-alive' --compressed

發現有輸出內容。
而後,反選disable cache

curl-2

而後在服務器上執行如下命令

curl  -k -v 'http://192.168.99.1/Public/Home/images/t_navigation_logo.png' -H 'If-None-Match: "57c6b733-1962"' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: zh,en;q=0.8,zh-CN;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Connection: keep-alive' -H 'If-Modified-Since: Wed, 31 Aug 2016 10:53:39 GMT' --compressed

發現只返回了頭部信息,然而沒有內容返回

而後咱們比較兩個命令 發現區別就在-H 'Pragma: no-cache'-H 'If-Modified-Since: Wed, 31 Aug 2016 10:53:39 GMT' -H 'If-None-Match: "57c6b733-1962"'

57c6b733-1962這串字符對應的是服務器響應給瀏覽器的ETag部分的內容,而後咱們修改一下部分的內容

curl  -k -v 'http://192.168.99.1/Public/Home/images/t_navigation_logo.png' -H 'If-None-Match: "57c6b733-1234"' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: zh,en;q=0.8,zh-CN;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Connection: keep-alive' -H 'If-Modified-Since: Wed, 31 Aug 2016 10:53:39 GMT' --compressed

在服務器端執行一下。發現有內容返回,因此這個ETag至關於token,它不是由nginx隨便生成的,且跟請求連接應是一一對應的,用來標識緩存的,當服務器返回的狀態爲304的時候,這時候咱們瀏覽器會直接找到本地的緩存數據

2. 在使用緩存模塊的狀況下

vi /etc/nginx/sites-available/default

location ~ .*\.(gif|jpg|png|css|js|flv|ico|swf|html)$ {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://cache;
}

用瀏覽器查看響應頭

發現X-Cache:MISS from 192.168.99.1.這表示緩存未命中,而後咱們刷新X-Cache:HIT from 192.168.99.1,這時候發現已經命中了。

對於已經命中的資源文件,咱們若是將其刪除會出現什麼效果呢,答案是,其依然能夠訪問,除非重啓或者將緩存清除
可是對PURGE顯然是不對外公開的,如下是服務器端用curl清除varnish緩存的命令

curl -v -k -X PURGE http://localhost/Public/Home/css/t_navigation.css

結語

varnish是一款內存類型的緩存軟件,而非nginx擴展proxy_cache那種物理緩存類型的軟件,存取速度比較快,可是也有弊端,重啓後全部緩存得重寫。無論怎麼說,什麼架子都適用的場景,要想知足業務需求仍是得搗鼓透徹,而我也只是將我想到的給實現出來,畢竟資源和精力都是有限的,也就隨便玩玩,諸位看客看看就好,別太認真,知道怎麼回事兒就行。

相關文章
相關標籤/搜索