【问题标题】:Varnish config advise清漆配置建议
【发布时间】:2016-05-06 23:38:36
【问题描述】:

我想知道我是否可以得到一些帮助。我正在使用 Varnish 运行 VPS。这个 VPS 主要是 WordPress,但也有一个 Joomla 网站也在运行。出于某种原因,我的 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 default {
  .host = "LIVE IP";
  .port = "8080";
  .max_connections = 800;
}

acl purge { "localhost"; "127.0.0.1"; }

sub vcl_recv {
set req.grace = 2m;

# Set X-Forwarded-For header for logging in nginx
remove req.http.X-Forwarded-For;
set req.http.X-Forwarded-For = client.ip;

# Remove has_js and CloudFlare/Google Analytics __* cookies and statcounter is_unique
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_[_a-z]+|has_js|is_unique)=[^;]*", "");
# Remove a ";" prefix, if present.
set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");

# Either the admin pages or the login
if (req.url ~ "/wp-(login|admin|cron)") {
# Don't cache, pass to backend
return (pass);
}

# Remove the wp-settings-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-1=[^;]+(; )?", "");

# Remove the wp-settings-time-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, 
"wp-settings-time-1=[^;]+(; )?", "");

# Remove the wp test cookie
set req.http.Cookie = regsuball(req.http.Cookie, 
"wordpress_test_cookie=[^;]+(;)?", "");

# Static content unique to the theme can be cached (so no user uploaded images)
# The reason I don't take the wp-content/uploads is because of cache size on bigger blogs
# that would fill up with all those files getting pushed into cache
if (req.url ~ "wp-content/themes/" && req.url ~ 
"\.(css|js|png|gif|jp(e)?g)") {
unset req.http.cookie;
}

# Even if no cookies are present, I don't want my "uploads" to be cached due to their potential size
if (req.url ~ "/wp-content/uploads/") {
return (pass);
}

# any pages with captchas need to be excluded
if (req.url ~ "^/contact/" || req.url ~ "^/links/domains-for-sale/")
{
return(pass);
}

# Check the cookies for wordpress-specific items
if (req.http.Cookie ~ "wordpress_" || req.http.Cookie ~ "comment_") {
# A wordpress specific cookie has been set
return (pass);
}

# allow PURGE from localhost
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
return (lookup);
}

# Force lookup if the request is a no-cache request from the client
if (req.http.Cache-Control ~ "no-cache") {
return (pass);
}

# Try a cache-lookup
return (lookup);

}

sub vcl_fetch {
#set obj.grace = 5m;
set beresp.grace = 2m;

}

sub vcl_hit {
if (req.request == "PURGE") {
purge;
error 200 "Purged.";
}
}

sub vcl_miss {
if (req.request == "PURGE") {
purge;
error 200 "Purged.";
}
}

我遇到的问题是我的 WP 核心文件被移动到一个名为“core”的子文件夹中,并且我将登录 URL 更改为 /administrator 而不是 wp-login.php。所以要登录,我会去 domain.com/administrator 或 domain.com/core/administrator。如果我键入没有 /core/ 的域,它将直接指向 */core/administrator。

Varnish 的工作方式不允许我登录网站的 WP-admin 端,我认为这是由于 cookie 造成的。我在 VCL 配置文本中添加了 /administrator,但它似乎不起作用。

有没有人可以帮我解决这个问题,因为我不想将所有安装都恢复到 wp-login.php。

我似乎也无法像以前那样登录 SSH 并清除缓存。老实说,我不记得以前的配置,但我确信我遗漏了一些东西,所以如果有人能够改进此代码以使其尽可能地工作(或者我是否完全错过了重要的事情,特别是与 Joomla)然后帮助将不胜感激。

我不会即时制作主题,但我确实有一些用户登录到 VPS 上托管的各种站点以添加/更改帖子,因此当我开始拉扯头发时,正确清除缓存会很棒出去!

谢谢大家。

【问题讨论】:

    标签: wordpress varnish-vcl


    【解决方案1】:

    要使 PURGE 工作,您可以尝试在“acl purge”部分添加您的 VPS 主机名。我知道我必须在我的 VPS 和 Varnish v4 上这样做。

    对于 wp 管理部分,它根本不应该是缓存,所以尝试改变这部分:

    # Either the admin pages or the login
    if (req.url ~ "/wp-(login|admin|cron)") {
    # Don't cache, pass to backend
    return (pass);
    }
    

    进入这个:

    # Either the admin pages or the login
    if (req.url ~ "/core/administrator" || req.url ~ "/administrator") {
    # Don't cache, pass to backend
    return (pass);
    }
    

    【讨论】:

    • 谢谢,现在看来可以正常工作了!我似乎确实遇到了联系表单无法与 Ninja Forms 或使用 Wordpress 的联系表单 7 一起使用的问题。有没有解决的办法?在这整个磨难之前,我从来没有遇到过问题!我必须添加没有其他页面缓存处于活动状态。
    猜你喜欢
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 2019-08-26
    • 2013-06-22
    • 1970-01-01
    • 2021-05-06
    • 2017-09-09
    • 2016-12-25
    相关资源
    最近更新 更多