Varnish 缓存未针对 Wordpress 进行优化。我做错了什么?

Varnish 缓存未针对 Wordpress 进行优化。我做错了什么?

我正在php5-fpm, nginx, varnish为我的高流量 WordPress 运行一个设置。

但我不确定我是否拥有最佳的清漆设置,因为我的清漆varnishstat从未超过 85%

Hitrate avg:     0.8661   0.8399   0.8389

这是我的设置:

/etc/默认/清漆

# Maximum number of open files (for ulimit -n)
NFILES=131072
# Maximum locked memory size (for ulimit -l)
# Used for locking the shared memory log in memory.  If you increase log size,
# you need to increase this number as well
MEMLOCK=82000
DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -p http_req_size=64000 \
             -s malloc,5G"

我的 VCL 文件:/etc/varnish/default.vcl

backend default {
    .host = "127.0.0.1";
    .port = "8080";
        .connect_timeout = 60s;
        .first_byte_timeout = 60s;
        .between_bytes_timeout = 60s;
#       .max_connections = 800;
}


acl purge {
        "localhost";
}

sub vcl_recv {
        set req.grace = 6h;

  # 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.
  set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_[_a-z]+|has_js)=[^;]*", "");
  # 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);
}

# 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 = 6h;
    # Get rid of User Agent to improve caching
        if (beresp.http.Vary ~ "User-Agent") {
                set beresp.http.Vary = regsub(beresp.http.Vary, ",? *User-Agent *", "");
                set beresp.http.Vary = regsub(beresp.http.Vary, "^, *", "");
                if (beresp.http.Vary == "") {
                        unset beresp.http.Vary;
                }
        }
        if (!(req.url ~ "wp-(login|admin)") && !req.http.cookie ~ "wordpress_logged_in" ) {
                unset beresp.http.set-cookie;
                set beresp.ttl = 96h;
        }

}

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

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

答案1

你的 VCL 配置没问题,只要你的命中率大于 0.70

相关内容