WordPress İçin Varnish Ayarları

By | 27 Ağustos 2013

Bu yazıda WordPress’in düzgün çalışması için Varnish ayarlarında yapmamız gereken değişikliklere bakacağız.

sudo nano /etc/varnish/default.vcl

ile Varnish ayar dosyasını açalım. Aşağıdaki örnek ayar dosyasındaki ayarları uygulayabilirsiniz.

Bu ayar dosyası aşağıdaki özellikleri ekler:

  1. Tema, yazı önizleme sayfasının düzgün çalışmasını sağlar
  2. Kuracağımız eklentilerle gerektiği zaman Varnish önbelliğinin boşaltılması için izin sağlar
  3. Giriş yapılmış kullanıcılara (admin) asla önbellekten sayfa vermez.
  4. Siteye giren ziyaretçilerin gerçek IP adreslerini nginx’e iletir.
# 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 = "127.0.0.1";
    .port = "8080";
}

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

#Drop any cookies sent to WordPress.
sub vcl_recv {

# Always cache the following file types for all users.
if ( req.url ~ "(?i)\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm)(\?[a-z0-9]+)?$" ) {
unset req.http.cookie;
}

# Don't serve cached pages to logged in users
if ( req.http.cookie ~ "wordpress_logged_in" || req.url ~ "vaultpress=true" ) {
return( pass );
}

# Allow posts preview
        if (req.url ~ "preview=true") {
                return(pass);
        }

        if (!(req.url ~ "wp-(login|admin)")) {
                unset req.http.cookie;
        }

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

# Handle compression correctly. Different browsers send different
# "Accept-Encoding" headers, even though they mostly all support the same
# compression mechanisms. By consolidating these compression headers into
# a consistent format, we can reduce the size of the cache and get more hits.
# @see: http://varnish.projects.linpro.no/wiki/FAQ/Compression
if ( req.http.Accept-Encoding ) {

	if ( req.http.Accept-Encoding ~ "gzip" ) {
	# If the browser supports it, we'll use gzip.
	set req.http.Accept-Encoding = "gzip";
	}

	else if ( req.http.Accept-Encoding ~ "deflate" ) {
	# Next, try deflate if it is supported.
	set req.http.Accept-Encoding = "deflate";
	}

	else {
	# Unknown algorithm. Remove it and send unencoded.
	unset req.http.Accept-Encoding;
	}

}

if (req.http.host == "huse.in") {
        set req.http.X-Forwarded-For = client.ip;
        return(pass);
}

}

# Drop any cookies WordPress tries to send back to the client.
sub vcl_fetch {
        if (!(req.url ~ "wp-(login|admin)")) {
                unset beresp.http.set-cookie;
        }

        if (req.http.host == "huse.in") {
        return(hit_for_pass);
        }
}

 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);
        set bereq.http.connection = "close";
        if (req.http.X-Forwarded-For) {
                set bereq.http.X-Forwarded-For = req.http.X-Forwarded-For;
        } else {
                set bereq.http.X-Forwarded-For = regsub(client.ip, ":.*", "");
          }

 }
#
 sub vcl_pass {
#     return (pass);

        set bereq.http.connection = "close";
        if (req.http.X-Forwarded-For) {
                set bereq.http.X-Forwarded-For = req.http.X-Forwarded-For;
        } else {
          set bereq.http.X-Forwarded-For = regsub(client.ip, ":.*", "");
          }
 }

 sub vcl_hash {
     hash_data(req.url);
     if (req.http.host) {
         hash_data(req.http.host);
     } else {
         hash_data(server.ip);
     }
     return (hash);
 }

 sub vcl_hit {
#     return (deliver);
if (req.request == "PURGE") {
                purge;
                error 200 "Purged.";
        }
 }
#
  sub vcl_miss {
#     return (fetch);

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