设为首页 收藏本站
查看: 505|回复: 0

[经验分享] Varnish 生产环境下配置优化

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-8-3 15:04:48 | 显示全部楼层 |阅读模式
后端服务器健康检查

# vim /etc/varnish/health_check.vcl
probe backend_healthcheck {
    .interval = 5s;
    .timeout = 3s;
    .window = 10;
    .threshold = 8;
  
    .request =
    "GET /favicon.ico HTTP/1.1"
    "Host: v5.ele.me"
    "Connection: close"
    "Accept-Encoding: foo/bar";
}



#后端服务器地址池配置
vim /etc/varnish/backends.vcl
import directors;
include "health_check.vcl";
  
## 实体机(restapi、v5、r、m)
backend 102_app_07 {
    .host = "10.11.11.145";
    .port = "80";
  
    .first_byte_timeout = 9s;
    .connect_timeout = 3s;
    .between_bytes_timeout = 1s;
  
    .probe = backend_healthcheck;
} ##102_app_07
  
backend 102_app_08 {
    .host = "10.10.11.146";
    .port = "80";
  
    .first_byte_timeout = 9s;
    .connect_timeout = 3s;
    .between_bytes_timeout = 1s;
  
    .probe = backend_healthcheck;
} ##102_app_08
  
backend 102_app_09 {
    .host = "10.10.11.147";
    .port = "80";
  
    .first_byte_timeout = 9s;
    .connect_timeout = 3s;
    .between_bytes_timeout = 1s;
  
    .probe = backend_healthcheck;
} ##102_app_09
  
backend 102_app_10 {
    .host = "10.10.11.148";
    .port = "80";
  
    .first_byte_timeout = 9s;
    .connect_timeout = 3s;
    .between_bytes_timeout = 1s;
  
    .probe = backend_healthcheck;
} ##102_app_10
  
backend 110_app_01 {
    .host = "10.10.11.41";
    .port = "80";
  
    .first_byte_timeout = 9s;
    .connect_timeout = 3s;
    .between_bytes_timeout = 1s;
   
    .probe = backend_healthcheck;
} ##110_app_01
  
backend 110_app_02 {
    .host = "10.10.11.42";
    .port = "80";
  
    .first_byte_timeout = 9s;
    .connect_timeout = 3s;
    .between_bytes_timeout = 1s;
  
    .probe = backend_healthcheck;
} ##110_app_02
  
backend 110_app_03 {
    .host = "10.10.11.43";
    .port = "80";
  
    .first_byte_timeout = 9s;
    .connect_timeout = 3s;
    .between_bytes_timeout = 1s;
  
    .probe = backend_healthcheck;
} ##110_app_03
   
backend 110_app_04 {
    .host = "10.10.11.44";
    .port = "80";
  
    .first_byte_timeout = 9s;
    .connect_timeout = 3s;
    .between_bytes_timeout = 1s;
  
    .probe = backend_healthcheck;
} ##110_app_04
  
## 负载均衡池
sub vcl_init {
    new web = directors.random();
  
    ## 实体机
    web.add_backend(102_app_07, 4);
    web.add_backend(102_app_08, 4);
    web.add_backend(102_app_09, 4);
    web.add_backend(102_app_10, 4);
    web.add_backend(110_app_01, 4);
    web.add_backend(110_app_02, 4);
    web.add_backend(110_app_03, 4);
    web.add_backend(110_app_04, 4);
}



缓存规则主配置
# vim /etc/varnish/default.vcl
vcl 4.0;
  
import std;
include "backends.vcl";
  
acl allow_purge_cache {
    "127.0.0.1";
    "10.0.0.0"/8;
    "172.0.0.0"/8;
}
  
sub vcl_recv {
    if (req.method == "PURGE") {
        if (!client.ip ~ allow_purge_cache) {
           return (synth(405, "Not Allowed."));
        }
         
        return (purge);
    }
  
    set req.backend_hint = web.backend();
  
    if (req.url ~ "\.(php|asp|aspx|jsp|do|ashx|shtml)($|\?)") {
        return (pass);
    }
  
    if (req.url ~ "\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)") {
        unset req.http.cookie;
        return (hash);
    }
  
    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.http.Cache-Control ~ "(?i)no-cache") {
       if (!(req.http.Via || req.http.User-Agent ~ "(?i)bot" || req.http.X-Purge)) {
           return (purge);
       }
    }
  
    if (req.method != "GET" &&
        req.method != "HEAD" &&
        req.method != "PUT" &&
        req.method != "POST" &&
        req.method != "TRACE" &&
        req.method != "OPTIONS" &&
        req.method != "PATCH" &&
        req.method != "DELETE") {
        return (pipe);
    }
  
    if (req.method != "GET" && req.method != "HEAD") {
        return (pass);
    }
  
    if (req.http.Authorization) {
        return (pass);
    }
  
    if (req.http.Accept-Encoding) {
        if (req.url ~ "\.(bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)$") {
            unset req.http.Accept-Encoding;
        } elseif (req.http.Accept-Encoding ~ "gzip") {
            set req.http.Accept-Encoding = "gzip";
        } elseif (req.http.Accept-Encoding ~ "deflate") {
            set req.http.Accept-Encoding = "deflate";
        } else {
            unset req.http.Accept-Encoding;
        }
    }
  
    if (req.http.Upgrade ~ "(?i)websocket") {
        return (pipe);
    }
  
    if (!std.healthy(req.backend_hint)) {
        unset req.http.Cookie;
    }
  
    if (req.http.x-pipe && req.restarts > 0) {
        unset req.http.x-pipe;
        return (pipe);
    }
  
    return (hash);
}
  
sub vcl_pipe {
    if (req.http.upgrade) {
        set bereq.http.upgrade = req.http.upgrade;
    }
  
    return (pipe);
}
  
sub vcl_pass {
    if (req.method == "PURGE") {
        return (synth(502, "PURGE on a passed object."));
    }
}
  
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");
    }
}
  
sub vcl_hit {
    if (req.method == "PURGE") {
        return (synth(200, "Purged."));
    }
     
    if (obj.ttl >= 0s) {
        return (deliver);
    }
  
    if (std.healthy(req.backend_hint)) {
        if (obj.ttl + 10s > 0s) {
            return (deliver);
        } else {
            return(fetch);
        }
    } else {
        if (obj.ttl + obj.grace > 0s) {
            return (deliver);
        } else {
            return (fetch);
        }
    }
  
    return (deliver);
}
  
sub vcl_miss {
    if (req.method == "PURGE") {
        return (synth(404, "Purged."));
    }
     
    return (fetch);
}
  
sub vcl_backend_response {
    set beresp.grace = 5m;
  
    set beresp.ttl = std.duration(regsub(beresp.http.Cache-Control, ".*s-maxage=([0-9]+).*", "\1") + "s", 0s);
    if (beresp.ttl > 0s) {
        unset beresp.http.Set-Cookie;
    }
  
    if (beresp.http.Set-Cookie) {
        set beresp.uncacheable = true;
        return (deliver);
    }
  
    if (beresp.http.Cache-Control && beresp.ttl > 0s) {
        set beresp.grace = 1m;
        unset beresp.http.Set-Cookie;
    }
     
    if (beresp.http.Content-Length ~ "[0-9]{8,}") {
        set bereq.http.x-pipe = "1";
        return (retry);
    }
     
    if (bereq.url ~ "\.(php|asp|aspx|jsp|do|ashx|shtml)($|\?)") {
        set beresp.uncacheable = true;
        return (deliver);
    }
  
    if (bereq.url ~ "\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)") {
        unset beresp.http.set-cookie;
    }
     
    if (bereq.url ~ "^[^?]*\.(mp[34]|rar|tar|tgz|gz|wav|zip|bz2|xz|7z|avi|mov|ogm|mpe?g|mk[av])(\?.*)?$") {
        unset beresp.http.set-cookie;
        set beresp.do_stream = true;
        set beresp.do_gzip = false;
    }
  
    if ((!beresp.http.Cache-Control && !beresp.http.Expires) ||
        beresp.http.Pragma ~ "no-cache" ||
        beresp.http.Cache-Control ~ "(no-cache|no-store|private)") {
        set beresp.ttl = 120s;
        set beresp.uncacheable = true;
        return (deliver);
    }
  
    if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") {
        set beresp.ttl = 120s;
        set beresp.uncacheable = true;
        return (deliver);
    }
  
    if (bereq.url ~ "\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico)($|\?)") {
        set beresp.ttl = 15m;
    } elseif (bereq.url ~ "\.(gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)") {
        set beresp.ttl = 30m;
    } else {
        set beresp.ttl = 10m;
    }
  
    return (deliver);
}
  
sub vcl_purge {
    if (req.method != "PURGE") {
        set req.http.X-Purge = "Yes";
        return (restart);
    }
}
  
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;
    }
  
    unset resp.http.X-Powered-By;
    unset resp.http.Server;
  
    unset resp.http.Via;
    unset resp.http.X-Varnish;
  
    unset resp.http.Age;
}
  
sub vcl_backend_error {
    if (beresp.status == 500 ||
        beresp.status == 501 ||
        beresp.status == 502 ||
        beresp.status == 503 ||
        beresp.status == 504) {
        return (retry);
    }
}
  
sub vcl_fini {
    return (ok);
}


启动参数配置
# vim /etc/sysconfig/varnish
NFILES=131072
MEMLOCK=25165824
NPROCS="unlimited"
  
RELOAD_VCL=1
VARNISH_VCL_CONF=/etc/varnish/default.vcl
  
VARNISH_LISTEN_ADDRESS=0.0.0.0
VARNISH_LISTEN_PORT=6081
  
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=6082
  
VARNISH_SECRET_FILE=/etc/varnish/secret
  
VARNISH_MIN_THREADS=240
VARNISH_MAX_THREADS=4800
VARNISH_THREAD_TIMEOUT=120
  
VARNISH_STORAGE_SIZE=24G
VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"
  
VARNISH_TTL=120
  
DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \
        -f ${VARNISH_VCL_CONF} \
        -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \
        -t ${VARNISH_TTL} \
        -p thread_pools=24 \
        -p thread_pool_min=${VARNISH_MIN_THREADS} \
        -p thread_pool_max=${VARNISH_MAX_THREADS} \
        -p thread_pool_timeout=${VARNISH_THREAD_TIMEOUT} \
        -u varnish -g varnish \
        -S ${VARNISH_SECRET_FILE} \
        -s ${VARNISH_STORAGE} \
        -p timeout_idle=60 \
        -p timeout_linger=1 \
        -p http_resp_hdr_len=16k \
        -p http_max_hdr=256 \
        -p http_req_hdr_len=16k \
        -p lru_interval=120 \
        -p listen_depth=8192
         
         
         
启动脚本调整
# vim /etc/init.d/varnish

exec="/usr/sbin/varnishd"
修改为
exec="/usr/bin/numactl --interleave all /usr/sbin/varnishd"



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-93779-1-1.html 上篇帖子: RedHat6.1配置在线yum资源 下篇帖子: DNS服务编译安装,及配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表