- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为使用非常定制的 Wordpress 安装的站点编写一个好的 vcl 文件,但我仍然是该领域的新手。因此,如果有人能指出我犯的错误,我将非常感激。
我正在尝试使用具有通用规则的 default.vcl
和具有域特定规则的每个域 vcl 文件。问题是我试图指示 Varnish 不要缓存页面和弹出 ajax 框的某些路径。而不是缓存我通过登录cookie捕获的登录用户。
我将粘贴我的完整配置。
另外,如果包含附加的 vcl 文件,我对 vcl 文件的线性执行有点困惑。 “包含”的位置是否重要?
我想自己调查这个案例,但是使用 Varnish 日志记录有点复杂。有没有办法可以找到哪些规则在特定页面上起作用?
我的 default.vcl
vcl 4.0;
import std;
backend domain1 {
.host = "11.111.11.1";
.port = "8001";
.connect_timeout = 600s;
.first_byte_timeout = 600s;
.between_bytes_timeout = 600s;
.max_connections = 800;
}
backend domain2 {
.host = "222.22.22.2";
.port = "8002";
.connect_timeout = 600s;
.first_byte_timeout = 600s;
.between_bytes_timeout = 600s;
.max_connections = 800;
}
acl purge {
"localhost";
"127.0.0.1";
"79.124.64.16";
"193.107.37.45";
}
sub vcl_recv {
if (req.http.host ~ "^(?i)domain1.com" || req.http.host ~ "^.*\.domain1.com" ) {
set req.backend_hint = domain1;
}
elseif (req.http.host == "^.*\.domain2.com" || req.http.host == "^.*\.domain2-alias.com") {
set req.backend_hint = domain2;
}
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;
}
}
#Pass through cache PhpMyAdmin
if (req.url ~ "^.*phpmyadmin.*") {
return(pass);
}
}
######## PER DOMAIN SUBROUTINES ###########
include "domain1.vcl";
include "domain2.vcl";
if (req.http.Cookie ~ "wordpress_logged_in_") {
return (pass);
}
# Normalize the header, remove the port (in case you're testing this on various TCP ports)
set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");
# Allow purging from ACL
if (req.method == "PURGE") {
# If not allowed then a error 405 is returned
if (!client.ip ~ purge) {
return(synth(405, "This IP is not allowed to send PURGE requests."));
}
# If allowed, do a cache_lookup -> vlc_hit() or vlc_miss()
return (purge);
}
# Post requests will not be cached
if (req.http.Authorization || req.method == "POST") {
return (pass);
}
# Conflicting with few lines below
# # Only deal with "normal" types
# 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") {
# /* Non-RFC2616 or CONNECT which is weird. */
# return (pipe);
# }
# Implementing websocket support (https://www.varnish-cache.org/docs/4.0/users-guide/vcl-example-websockets.html)
if (req.http.Upgrade ~ "(?i)websocket") {
return (pipe);
}
# Only cache GET or HEAD requests. This makes sure the POST requests are always passed.
if (req.method != "GET" && req.method != "HEAD") {
return (pipe);
}
# Some generic URL manipulation, useful for all templates that follow
# First remove the Google Analytics added parameters, useless for our backend
if (req.url ~ "(\?|&)(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=") {
set req.url = regsuball(req.url, "&(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "");
set req.url = regsuball(req.url, "\?(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "?");
set req.url = regsub(req.url, "\?&", "?");
set req.url = regsub(req.url, "\?$", "");
}
# Some generic cookie manipulation, useful for all templates that follow
# Remove the "has_js" cookie
set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", "");
# Remove any Google Analytics based cookies
set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "_ga=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "_gat=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "utmctr=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "utmcmd.=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "utmccn.=[^;]+(; )?", "");
# Remove DoubleClick offensive cookies
set req.http.Cookie = regsuball(req.http.Cookie, "__gads=[^;]+(; )?", "");
# Remove the Quant Capital cookies (added by some plugin, all __qca)
set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(; )?", "");
# Remove the AddThis cookies
set req.http.Cookie = regsuball(req.http.Cookie, "__atuv.=[^;]+(; )?", "");
# Remove a ";" prefix in the cookie if present
set req.http.Cookie = regsuball(req.http.Cookie, "^;\s*", "");
#Remove WP cookies
set req.http.Cookie = regsuball(req.http.Cookie, "__utma.=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "__utmb.=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "__utmc.=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "__utmt.=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "__utmz.=[^;]+(; )?", "");
# set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie.=[^;]+(; )?", "");
# set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "bp-message[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "bp-message-type[^;]+(; )?", "");
# Are there cookies left with only spaces or that are empty?
if (req.http.cookie ~ "^\s*$") {
unset req.http.cookie;
}
**#Dont cache Logged-in users
if (req.http.Cookie == "wordpress_logged_in_") {
return (pass);
}
elseifif (req.http.cookie ~ "wordpress_logged_in_") {
return (pass);
}**
# Large static files are delivered directly to the end-user without
# waiting for Varnish to fully read the file first.
# Varnish 4 fully supports Streaming, so set do_stream in vcl_backend_response()
if (req.url ~ "^[^?]*\.(7z|avi|bz2|flac|flv|gz|mka|mkv|mov|mp3|mp4|mpeg|mpg|ogg|ogm|opus|rar|tar|tgz|tbz|txz|wav|webm|xz|zip)(\?.*)?$") {
unset req.http.Cookie;
return (hash);
}
# Remove all cookies for static files
# A valid discussion could be held on this line: do you really need to cache static files that don't cause load? Only if you have memory left.
# Sure, there's disk I/O, but chances are your OS will already have these files in their buffers (thus memory).
# Before you blindly enable this, have a read here: https://ma.ttias.be/stop-caching-static-files/
if (req.url ~ "^[^?]*\.(7z|avi|bmp|bz2|css|csv|doc|docx|eot|flac|flv|gif|gz|ico|jpeg|jpg|js|less|mka|mkv|mov|mp3|mp4|mpeg|mpg|odt|otf|ogg|ogm|opus|pdf|png|ppt|pptx|rar|rtf|svg|svgz|swf|tar|tbz|tgz|ttf|txt|txz|wav|webm|webp|woff|
woff2|xls|xlsx|xml|xz|zip)(\?.*)?$") {
unset req.http.Cookie;
return (hash);
}
# Send Surrogate-Capability headers to announce ESI support to backend
# set req.http.Surrogate-Capability = "key=ESI/1.0";
if (req.http.Authorization) {
# Not cacheable by default
return (pass);
}
# Did not cache HTTP authentication and HTTP Cookie
if (req.http.Authorization || req.http.Cookie) {
# Not cacheable by default
return (pass);
}
}
# Cache all others requests
return (hash);
}
######## PER DOMAIN SUBROUTINES ###########
include "domain1.vcl";
include "domain2.vcl";
sub vcl_pipe {
# set bereq.http.Connection = "Close";
# Implementing websocket support (https://www.varnish-cache.org/docs/4.0/users-guide/vcl-example-websockets.html)
if (req.http.upgrade) {
set bereq.http.upgrade = req.http.upgrade;
}
return (pipe);
}
sub vcl_pass {
# return (pass);
}
# The data on which the hashing will take place
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
# hash cookies for requests that have them
# if (req.http.Cookie) {
# hash_data(req.http.Cookie);
# }
}
sub vcl_hit {
if (obj.ttl >= 0s) {
# A pure unadultered hit, deliver it
return (deliver);
}
# https://www.varnish-cache.org/docs/trunk/users-guide/vcl-grace.html
# When several clients are requesting the same page Varnish will send one request to the backend and place the others on hold while fetching one copy from the backend. In some products this is called request coalescing and Varni
sh does this automatically.
# If you are serving thousands of hits per second the queue of waiting requests can get huge. There are two potential problems - one is a thundering herd problem - suddenly releasing a thousand threads to serve content might sen
d the load sky high. Secondly - nobody likes to wait. To deal with this we can instruct Varnish to keep the objects in cache beyond their TTL and to serve the waiting requests somewhat stale content.
# if (!std.healthy(req.backend_hint) && (obj.ttl + obj.grace > 0s)) {
# return (deliver);
# } else {
# return (fetch);
# }
# We have no fresh fish. Lets look at the stale ones.
##if (std.healthy(req.backend_hint) {
# Backend is healthy. Limit age to 10s.
##if (obj.ttl + 10s > 0s) {
#set req.http.grace = "normal(limited)";
## return (deliver);
## } else {
# No candidate for grace. Fetch a fresh object.
## return(fetch);
## }
## } else {
# backend is sick - use full grace
## if (obj.ttl + obj.grace > 0s) {
#set req.http.grace = "full";
## return (deliver);
## } else {
# no graced object.
return (fetch);
## }
## }
# fetch & deliver once we get the result
## return (fetch); # Dead code, keep as a safeguard
}
sub vcl_miss {
# Called after a cache lookup if the requested document was not found in the cache. Its purpose
# is to decide whether or not to attempt to retrieve the document from the backend, and which
# backend to use.
return (fetch);
}
# Handle the HTTP request coming from our backend
sub vcl_backend_response {
# Called after the response headers has been successfully retrieved from the backend.
if (bereq.http.Cookie ~ "(UserID|_session)") {
set beresp.http.X-Cacheable = "NO:Got Session";
set beresp.uncacheable = true;
return (deliver);
} elsif (beresp.ttl <= 0s) {
# Varnish determined the object was not cacheable
set beresp.http.X-Cacheable = "NO:Not Cacheable";
} elsif (beresp.http.set-cookie) {
# You don't wish to cache content for logged in users
set beresp.http.X-Cacheable = "NO:Set-Cookie";
set beresp.uncacheable = true;
return (deliver);
} elsif (beresp.http.Cache-Control ~ "private") {
# You are respecting the Cache-Control=private header from the backend
set beresp.http.X-Cacheable = "NO:Cache-Control=private";
set beresp.uncacheable = true;
return (deliver);
} else {
# Varnish determined the object was cacheable
set beresp.http.X-Cacheable = "YES";
}
# Pause ESI request and remove Surrogate-Control header
if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
unset beresp.http.Surrogate-Control;
set beresp.do_esi = true;
}
# Enable cache for all static files
# The same argument as the static caches from above: monitor your cache size, if you get data nuked out of it, consider giving up the static file cache.
# Before you blindly enable this, have a read here: https://ma.ttias.be/stop-caching-static-files/
if (bereq.url ~ "^[^?]*\.(7z|avi|bmp|bz2|css|csv|doc|docx|eot|flac|flv|gif|gz|ico|jpeg|jpg|js|less|mka|mkv|mov|mp3|mp4|mpeg|mpg|odt|otf|ogg|ogm|opus|pdf|png|ppt|pptx|rar|rtf|svg|svgz|swf|tar|tbz|tgz|ttf|txt|txz|wav|webm|webp|wof
f|woff2|xls|xlsx|xml|xz|zip)(\?.*)?$") {
unset beresp.http.set-cookie;
}
# Large static files are delivered directly to the end-user without
# waiting for Varnish to fully read the file first.
# Varnish 4 fully supports Streaming, so use streaming here to avoid locking.
if (bereq.url ~ "^[^?]*\.(7z|avi|bz2|flac|flv|gz|mka|mkv|mov|mp3|mp4|mpeg|mpg|ogg|ogm|opus|rar|tar|tgz|tbz|txz|wav|webm|xz|zip)(\?.*)?$") {
unset beresp.http.set-cookie;
set beresp.do_stream = true; # Check memory usage it'll grow in fetch_chunksize blocks (128k by default) if the backend doesn't send a Content-Length header, so only enable it for big objects
}
# Sometimes, a 301 or 302 redirect formed via Apache's mod_rewrite can mess with the HTTP port that is being passed along.
# This often happens with simple rewrite rules in a scenario where Varnish runs on :80 and Apache on :8080 on the same box.
# A redirect can then often redirect the end-user to a URL on :8080, where it should be :80.
# This may need finetuning on your setup.
#
# To prevent accidental replace, we only filter the 301/302 redirects for now.
if (beresp.status == 301 || beresp.status == 302) {
set beresp.http.Location = regsub(beresp.http.Location, ":[0-9]+", "");
}
# Set 2min cache if unset for static files
if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") {
set beresp.ttl = 120s; # Important, you shouldn't rely on this, SET YOUR HEADERS in the backend
set beresp.uncacheable = true;
return (deliver);
}
# Don't cache 50x responses
if (beresp.status == 500 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504) {
return (abandon);
}
# Allow stale content, in case the backend goes down.
# make Varnish keep all objects for 6 hours beyond their TTL
set beresp.grace = 6h;
return (deliver);
}
# The routine when we deliver the HTTP request to the user
# Last chance to modify headers that are sent to the client
sub vcl_deliver {
# Called before a cached object is delivered to the client.
if (obj.hits > 0) { # Add debug header to see if it's a HIT/MISS and the number of hits, disable when not needed
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
# Please note that obj.hits behaviour changed in 4.0, now it counts per objecthead, not per object
# and obj.hits may not be reset in some cases where bans are in use. See bug 1492 for details.
# So take hits with a grain of salt
#set resp.http.X-Cache-Hits = obj.hits;
# Remove some headers: PHP version
unset resp.http.X-Powered-By;
# Remove some headers: Apache version & OS
unset resp.http.Server;
unset resp.http.X-Drupal-Cache;
unset resp.http.X-Varnish;
unset resp.http.Via;
unset resp.http.Link;
unset resp.http.X-Generator;
return (deliver);
}
sub vcl_purge {
# Only handle actual PURGE HTTP methods, everything else is discarded
if (req.method != "PURGE") {
# restart request
set req.http.X-Purge = "Yes";
return(restart);
}
}
sub vcl_synth {
if (resp.status == 720) {
# We use this special error status 720 to force redirects with 301 (permanent) redirects
# To use this, call the following from anywhere in vcl_recv: return (synth(720, "http://host/new.html"));
set resp.http.Location = resp.reason;
set resp.status = 301;
return (deliver);
} elseif (resp.status == 721) {
# And we use error status 721 to force redirects with a 302 (temporary) redirect
# To use this, call the following from anywhere in vcl_recv: return (synth(720, "http://host/new.html"));
set resp.http.Location = resp.reason;
set resp.status = 302;
return (deliver);
}
return (deliver);
}
sub vcl_fini {
# Called when VCL is discarded only after all requests have exited the VCL.
# Typically used to clean up VMODs.
return (ok);
}
domain1.vcl
vcl 4.0;
sub vcl_recv {
if (req.backend_hint == domain1) {
**#Don't store backend in case of login pop screen
if (req.url ~ "^.*wp-(login|admin).*" || req.url ~ "preview=true" || req.url ~ "^.*(login|admin).*" || req.url ~ "^.*wp-load.*") {
return (pipe);
}
# Do not cache home, search, registration pages
if (req.url ~ "\/path\/subpath\/" || req.url ~ "\/pathabc\/pathdef\/" || req.url ~ ".*register.*") {
return(pipe);
}
if (req.url ~ ".*\/members\/.*") {
return(hash);
}
}
return(hash);
}**
sub vcl_backend_response {
}
sub vcl_deliver {
}
domain2.vcl
在创建规则时没有遇到任何问题。
最佳答案
您遇到的问题是由于 req.backend_hint == domain1
(这不起作用)。您应该与站点特定 VCL 中的主机名进行比较。所以在 domain1.vcl
中它将是:
vcl 4.0;
sub vcl_recv {
if (req.http.host ~ "^(?i)domain1.com" || req.http.host ~ "^.*\.domain1.com" ) {
# site specific logic goes here ...
}
}
include
指令直接放在
vcl_recv
过程中。这些应该放在文件的最顶部或底部,具体取决于所需的优先级。
# ...
include "domain1.vcl";
include "domain2.vcl";
sub vcl_recv {
#...
vcl_recv
逻辑将优先于您在主 VCL 文件本身中拥有的逻辑执行。
vcl_recv
例程将按以下顺序执行:
vcl_recv
来自特定站点的 .vcl vcl_recv
来自 default.vcl
vcl_recv
来自 builtin.vcl
builtin.vcl
是
you can find here 的更多信息。
default.vcl
开始,一点一点地慢慢添加更多代码,以了解事情是如何工作的。盲目复制粘贴永远不会导致预期的结果,更常见的是出乎意料的 :)。
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;
}
}
}
600s
超时值可能会使您的应用程序正常工作,但如果它不适用于默认值(您肯定会看到“后端提取失败”),这是有原因的。并且 Varnish 不是修复“后端获取失败”的地方 - 您的应用程序是 :)。
set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", "");
# ...
关于wordpress - 未经指示时上光缓存wordpress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43554034/
我正在尝试复兴使用3DNow的旧Win32游戏!指令集以进行3D渲染。 在Win7-Win10等现代OS上,不允许FPADD或FPMUL之类的Win10指令,并且该程序将引发异常。 自3DNow数量!
我坐在机场这里,想出了一些我想尝试的东西,但如果 macports 下载-编译-下载-编译,我没有时间 sudo port install .但是,如果它下载了所有内容,那么我就可以在飞机上对其进行编
我使用的是 Jackson 库,而不是 2.6.3。我想在类中定义序列化方法,并且我想指示 Jackson 在序列化对象时调用此方法。 例如 public interface AClass { d
我正在制作一个自动目录,一切正常。我只需要将顶部标题指定为“粗体” jQuery(document).ready(function(){ var ToC = "" + ""; var ne
我要设置 html 对象的属性。 var property1 = 'style.visibility'; var property2 = 'style.display'; var property3
在 boost::spirit::traits::transform_attribute 中指示解析失败的正确方法是什么?我可以抛出任何旧的异常,还是它要我做的特定事情? namespace boos
我正在使用 XmlPullParser 在移动设备上通过 http 逐渐加载一些数据。 由于此类连接的速度通常可以低至 1KB/s 或更低,我想降低 PullParser 的默认缓冲区大小 8096
我正在尝试集体检查数据是否存在于各个表中。我有一个主表 A 和包含与 A 相关的数据的各种表 - 称它们为表 B、C 和 D。我想编写一个查询,对于 A 中的每个条目,指示是否有任何行在 B、C 和
当您使用 Cargo 和 rustdoc 为 Rust crate 生成文档时,我在生成的页面中看不到任何指示它适用于哪个版本的 crate。例如,看看 the log crate's documen
我有一个 CS 类,它表示 3D 坐标系,即 (x, y, z) class CS { private: double x; double y; d
我有一个用 Wordpress 制作的项目。我有在社交网络上分享的帖子。在推特上没有问题,因为我创建的推文没有图片。Facebook 允许我从要分享的链接中选择页面图像。但是 Google+ 正在挑选
问题 如何在 Scrapy 中忽略响应的内容长度? 解释 考虑这个 curl 命令" curl -u http://data.icecat.biz/export/level4/NL/files.in
我有一个测试程序,如果它可以依赖于在 Windows 上以严格的优先级顺序安排的线程,它会简单得多。我看到一个低优先级线程与高优先级线程一起运行,我想知道这是不是因为不同的线程被安排在不同的处理器内核
我正在使用 getUserMedia 函数从网络摄像头录制视频。一切正常,除了它仅以 640x480 分辨率录制,当我刚刚指定 video: true 作为约束时。 如果我按如下方式设置约束,我现在可
我有一个简单的类定义如下: class Model { constructor(props?:{}) { _extend(props, this); } } 其中构造函数接受一个对象作
我第一次在 Visual Studio 2010 beta 2 中使用 .net-4.0 中的 System.ComponentModel.Composition 试用托管扩展框架。 我一直无法让 C
我正在使用 System.CodeDom 功能在运行时编译代码,我想知道我是否可以指定一个编译器参数或其他解决方法来以英语语言显示编译器错误,而不是使用系统的默认语言语言。 但是,在 MSDN 文档中
我正在使用 XmlWriterSettings 将 Xml 写入文件。我有只有属性的元素,没有 child 。我希望它们输出为: 代替 我可以使用 XmlWriterSettings 来实现吗?
我在 sbt 中创建了一个多项目构建。这是 build.sbt 在主目录中: lazy val root = project in file(".") aggregate(data, reco, re
这里我有一个程序,可以计算一个人不同的日常事件,例如他一周踢足球的次数等。这里我有一个 switch 语句,可以计算不同事件的值。我强制这个对象指示 sort() 函数内的 dayEvents 对象。
我是一名优秀的程序员,十分优秀!