Varnish+Nginx 配置----Varnish

2024-03-31 12:38
文章标签 配置 nginx varnish

本文主要是介绍Varnish+Nginx 配置----Varnish,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近项目引入反向代理和缓存,熟悉了一下Squid、Apache、Valish、Nginx,根据项目实际进行选择,客观来说,采用Linux系统部署最好,也没有什么难度,但实际情况必须采用Windows系统(本着方案要结合现实的原则,研究要以Windows平台为主)。


一  Varnish启动:

e:
cd e:\varnish\bin
set PATH=%CD%;%PATH%
varnishd -a :7575 -T :11212 -f /etc/varnish_cst_cfg.vcl -s file,E:/var/varnish/tg/cache/jjtg,1024M -p thread_pool_max=5000 -p thread_pool_min=100 
REM pause
二  Varnish配置:
# This is a basic VCL configuration file for varnish.  See the vcl(7)
# man page for details on VCL syntax and semantics.
# 
# Default backend definition.  Set this to point to your content
# server.backend tgweb {.host = "192.168.45.45";.port = "7574";.connect_timeout = 20s;.first_byte_timeout = 20s;.between_bytes_timeout = 20s;}#允许刷新缓存的规则
#acl purgeAllow {
#     #只能本机进行刷新
#     "localhost";
#}# Below is a commented-out copy of the default VCL logic.  If you
# redefine any of these subroutines, the built-in logic will be
# appended to your code.sub vcl_recv {#判断请求主机,跳转到相应后端服务器if(req.http.host ~ "^(.*)(tg.stockjyb.com:7575)"){set req.backend=tgweb;}else{error 408 "Hostname not found"; }#grace缓存过期仍存放# 若backend是健康的,则仅grace 5s,如果backend不健康,则grace 1m。# 这里,5s的目的是为了提高高并发时的吞吐率;# 1m的目的是,backend挂了之后,还能继续服务一段时间,期望backend挂的不要太久。。。if (req.backend.healthy) {set req.grace = 5s;} else {set req.grace = 1m;}#刷新缓存的处理#if (req.request == "PURGE"){#    if(!client.ip ~ purgeAllow) {#            error 405 "Not allowed.";#    }#    #转到hit或者miss处理#    return (lookup);#}#移除一些特定格式的cookieif (req.url ~ "^(.*)\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz|js|css|html|htm)($|\?)" ) {#移除cookie,以便能缓存到varnishunset req.http.cookie;}#Accept-Encoding 是浏览器发给服务器,声明浏览器支持的编码类型的#修正客户端的Accept-Encoding头信息#防止个别浏览器发送类似 deflate, gzipif (req.http.Accept-Encoding) {if (req.url ~ "^(.*)\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz)($|\?)" ) {remove req.http.Accept-Encoding;}else if (req.http.Accept-Encoding ~ "gzip"){set req.http.Accept-Encoding = "gzip";} else if (req.http.Accept-Encoding ~ "deflate"){set req.http.Accept-Encoding = "deflate";} else if (req.http.Accept-Encoding ~ "sdch"){#chrome新增加的压缩set req.http.Accept-Encoding = "sdch";}else {remove req.http.Accept-Encoding;}}#首次访问增加X-Forwarded-For头信息,方便后端程序获取客户端ipif (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.request != "GET" &&#    req.request != "HEAD" &&#    req.request != "PUT" &&#    req.request != "POST" &&#    req.request != "TRACE" &&#    req.request != "OPTIONS" &&#    req.request != "DELETE") {#    /* Non-RFC2616 or CONNECT which is weird. */#    return (pipe);#}if (req.request != "GET" && req.request != "HEAD") {/* We only deal with GET and HEAD by default */return (pass);}if (req.http.Authorization) {/* Not cacheable by default */return (pass);}#js,css文件都有Cookie,不能每次都去后台服务器去取#if (req.http.Cookie) {#    /* Not cacheable by default */#    return (pass);#}#如果请求的是动态页面直接转发到后端服务器if (req.url ~ "^(.*)\.(aspx|asmx|ashx)($|.*)") {return (pass);}return (lookup);}sub vcl_pipe {# Note that only the first request to the backend will have# X-Forwarded-For set.  If you use X-Forwarded-For and want to# have it set for all requests, make sure to have:# set bereq.http.connection = "close";# here.  It is not set by default as it might break some broken web# applications, like IIS with NTLM authentication.return (pipe);}#放过,让其直接去后台服务器请求数据sub vcl_pass {return (pass);}sub vcl_hash {hash_data(req.url);if (req.http.host) {hash_data(req.http.host);} else {hash_data(server.ip);}#支持压缩的要增加,防止发送给不支持压缩的浏览器压缩的内容if(req.http.Accept-Encoding){hash_data(req.http.Accept-Encoding);}return (hash);}#缓存服务器lookup查找命中:hitsub vcl_hit {#刷新缓存的请求操作,设置TTL为0,返回处理结果代码#if (req.request == "PURGE") {#     set obj.ttl = 0s;#     error 200 "Purged.";# }//#缓存服务器命中后(查找到了)return (deliver);}#缓存服务器lookup查找没有命中:misssub vcl_miss {#刷新缓存的请求操作,#if (req.request == "PURGE") {#    error 404 "Not in cache.";#}//#缓存服务器没有命中(去后台服务器取)return (fetch);}#从后台服务器取回数据后,视情况是否进行缓存sub vcl_fetch {#如果请求的是动态页面直接发转发#动态请求回来的,一定要放在前面处理if (req.url ~ "^(.*)\.(aspx|asmx|ashx)($|.*)") {set beresp.http.Cache-Control="no-cache, no-store";unset beresp.http.Expires;return (deliver);}# 仅当该请求可以缓存时,才设置beresp.grace,若该请求不能被缓存,则不设置beresp.graceif (beresp.ttl > 0s) {set beresp.grace = 1m;}  if (beresp.ttl <= 0s ||beresp.http.Set-Cookie ||beresp.http.Vary == "*") {/** Mark as "Hit-For-Pass" for the next 2 minutes*/set beresp.ttl = 120 s;#下次请求时不进行lookup,直接passreturn (hit_for_pass);}#设置从后台服务器获得的特定格式文件的缓存TTLif (req.url ~ "^(.*)\.(pdf|xls|ppt|doc|docx|xlsx|pptx|chm|rar|zip)($|\?)")     {#移除服务器发送的cookie unset beresp.http.Set-Cookie;#加上缓存时间set beresp.ttl = 30d;return (deliver);}else if(req.url ~ "^(.*)\.(bmp|jpeg|jpg|png|gif|svg|png|ico|txt|css|js|html|htm)($|\?)"){#移除服务器发送的cookie unset beresp.http.Set-Cookie;#加上缓存时间set beresp.ttl = 15d;return (deliver);}else if(req.url ~ "^(.*)\.(mp3|wma|mp4|rmvb|ogg|mov|avi|wmv|mpeg|mpg|dat|3pg|swf|flv|asf)($|\?)"){#移除服务器发送的cookie unset beresp.http.Set-Cookie;#加上缓存时间set beresp.ttl = 30d;return (deliver);}#从后台服务器返回的response信息中,没有缓存的,不缓存if (beresp.http.Pragma ~"no-cache" || beresp.http.Cache-Control ~"no-cache" || beresp.http.Cache-Control ~"private") {return (deliver);}return (deliver);}#缓存服务器发送到客户端前调用sub vcl_deliver {#下面是添加一个Header标识,以判断缓存是否命中。if (obj.hits > 0) {set resp.http.X-Cache = "HIT from TG.varnish-cache.jjcj.com";#set resp.http.X-Varnish = "HIT from TG.varnish-cache.jjcj.com";} else {set resp.http.X-Cache = "MISS from TG.varnish-cache.jjcj.com";#set resp.http.X-Varnish = "MISS from TG.varnish-cache.jjcj.com";}#去掉不是必须的headerunset resp.http.Vary;unset resp.http.X-Powered-By;unset resp.http.X-AspNet-Version;return (deliver);}sub vcl_error {set obj.http.Content-Type = "text/html; charset=utf-8";set obj.http.Retry-After = "5";synthetic {"<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head><title>"} + obj.status + " " + obj.response + {"</title></head><body><h1>Error "} + obj.status + " " + obj.response + {"</h1><p>"} + obj.response + {"</p><h3>Guru Meditation:</h3><p>XID: "} + req.xid + {"</p><hr><p>Varnish cache server</p></body></html>"};return (deliver);}sub vcl_init {return (ok);}sub vcl_fini {return (ok);}

这篇关于Varnish+Nginx 配置----Varnish的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/864358

相关文章

windos server2022的配置故障转移服务的图文教程

《windosserver2022的配置故障转移服务的图文教程》本文主要介绍了windosserver2022的配置故障转移服务的图文教程,以确保服务和应用程序的连续性和可用性,文中通过图文介绍的非... 目录准备环境:步骤故障转移群集是 Windows Server 2022 中提供的一种功能,用于在多个

windos server2022里的DFS配置的实现

《windosserver2022里的DFS配置的实现》DFS是WindowsServer操作系统提供的一种功能,用于在多台服务器上集中管理共享文件夹和文件的分布式存储解决方案,本文就来介绍一下wi... 目录什么是DFS?优势:应用场景:DFS配置步骤什么是DFS?DFS指的是分布式文件系统(Distr

关于Maven中pom.xml文件配置详解

《关于Maven中pom.xml文件配置详解》pom.xml是Maven项目的核心配置文件,它描述了项目的结构、依赖关系、构建配置等信息,通过合理配置pom.xml,可以提高项目的可维护性和构建效率... 目录1. POM文件的基本结构1.1 项目基本信息2. 项目属性2.1 引用属性3. 项目依赖4. 构

解决systemctl reload nginx重启Nginx服务报错:Job for nginx.service invalid问题

《解决systemctlreloadnginx重启Nginx服务报错:Jobfornginx.serviceinvalid问题》文章描述了通过`systemctlstatusnginx.se... 目录systemctl reload nginx重启Nginx服务报错:Job for nginx.javas

龙蜥操作系统Anolis OS-23.x安装配置图解教程(保姆级)

《龙蜥操作系统AnolisOS-23.x安装配置图解教程(保姆级)》:本文主要介绍了安装和配置AnolisOS23.2系统,包括分区、软件选择、设置root密码、网络配置、主机名设置和禁用SELinux的步骤,详细内容请阅读本文,希望能对你有所帮助... ‌AnolisOS‌是由阿里云推出的开源操作系统,旨

mysql-8.0.30压缩包版安装和配置MySQL环境过程

《mysql-8.0.30压缩包版安装和配置MySQL环境过程》该文章介绍了如何在Windows系统中下载、安装和配置MySQL数据库,包括下载地址、解压文件、创建和配置my.ini文件、设置环境变量... 目录压缩包安装配置下载配置环境变量下载和初始化总结压缩包安装配置下载下载地址:https://d

gradle安装和环境配置全过程

《gradle安装和环境配置全过程》本文介绍了如何安装和配置Gradle环境,包括下载Gradle、配置环境变量、测试Gradle以及在IntelliJIDEA中配置Gradle... 目录gradle安装和环境配置1 下载GRADLE2 环境变量配置3 测试gradle4 设置gradle初始化文件5 i

SpringCloud配置动态更新原理解析

《SpringCloud配置动态更新原理解析》在微服务架构的浩瀚星海中,服务配置的动态更新如同魔法一般,能够让应用在不重启的情况下,实时响应配置的变更,SpringCloud作为微服务架构中的佼佼者,... 目录一、SpringBoot、Cloud配置的读取二、SpringCloud配置动态刷新三、更新@R

MySQL中my.ini文件的基础配置和优化配置方式

《MySQL中my.ini文件的基础配置和优化配置方式》文章讨论了数据库异步同步的优化思路,包括三个主要方面:幂等性、时序和延迟,作者还分享了MySQL配置文件的优化经验,并鼓励读者提供支持... 目录mysql my.ini文件的配置和优化配置优化思路MySQL配置文件优化总结MySQL my.ini文件

C#读取本地网络配置信息全攻略分享

《C#读取本地网络配置信息全攻略分享》在当今数字化时代,网络已深度融入我们生活与工作的方方面面,对于软件开发而言,掌握本地计算机的网络配置信息显得尤为关键,而在C#编程的世界里,我们又该如何巧妙地读取... 目录一、引言二、C# 读取本地网络配置信息的基础准备2.1 引入关键命名空间2.2 理解核心类与方法