CentOS :: Setting Varnish, Nginx for WordPress

1 min read

:: How to setting Varnish and Nginx for WordPress cms in Linux CentOS
+ Enable Varnish and Nginx repository

rpm --nosignature -i //repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release-3.0-1.noarch.rpm
rpm -Uvh //nginx.org/packages/rhel/6/noarch/RPMS/nginx-release-rhel-6-0.el6.ngx.noarch.rpm

 
+ Installing Varnish and Nginx

yum install varnish nginx php-fpm

 
+ Configure Nginx for WordPress
make sure change Nginx port services to 8080.

vi /etc/nginx/conf.d/blackonsole.org.conf

setup some like this:

server {
   listen 8080;
   server_name blackonsole.org;
   root /var/www/html;
   index index.php;

   access_log /var/log/nginx/blackonsole.org.access_log;
   error_log /var/log/nginx/blackonsole.org.error_log;

   # permalink
   location / {
      try_files $uri $uri/ /index.php?$args;
   }

   # php-script handler
   location ~ \.php$ {
      fastcgi_index index.php;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
      include /etc/nginx/fastcgi_params;
   }
}

 
+ Configure Varnish daemon

vi /etc/sysconfig/varnish

setting DAEMON_OPTS configuration like this:

DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -u varnish -g varnish \
             -S /etc/varnish/secret \
             -s file,/var/lib/varnish/varnish_storage.bin,2G"

 
+ Configure Varnish for Nginx and WordPress

vi /etc/varnish/default.vcl

setup like this:

backend default {
  .host = "127.0.0.1";
  .port = "8080";
  .connect_timeout = 600s;
  .first_byte_timeout = 600s;
  .between_bytes_timeout = 600s;
}

sub vcl_recv {
  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);
  }

  # Remove has_js and Google Analytics cookies.
  set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+)=[^;]*", "");
 
  # Remove a ";" prefix, if present.
  set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
  # Remove empty cookies.
  if (req.http.Cookie ~ "^\s*$") {
    unset req.http.Cookie;
  }

  if (req.http.Authorization || req.http.Cookie) {
    # Not cacheable by default */
    return (pass);
  }

  # Skip the Varnish cache for install, update, and cron
  if (req.url ~ "install\.php|update\.php|cron\.php") {
    return (pass);
  }
  
  # Let's have a little grace
  set req.grace = 30s;

  return (lookup);
}

 
+ Starting Varnish and Nginx

/etc/init.d/nginx start
/etc/init.d/varnish start

 
+ Testing

curl -I blackonsole.org

you will got result some like this:

HTTP/1.1 200 OK
Server: nginx/1.4.1
Content-Type: text/html; charset=UTF-8
X-Powered-By: PHP/5.3.23
Set-Cookie: PHPSESSID=etubvjn80o5ar99sdferS22322ss; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
X-Pingback: //blackonsole.org/xmlrpc.php
Content-Length: 82521
Accept-Ranges: bytes
Date: Mon, 15 Jul 2013 02:50:39 GMT
X-Varnish: 1951812197
Age: 0
Via: 1.1 varnish
Connection: keep-alive

see on these line:

Server: nginx/1.4.1
X-Varnish: 1951812197

 
:: Links
+ Google
+ Varnish
+ Nginx

One Reply to “CentOS :: Setting Varnish, Nginx…”

Leave a Reply

Your email address will not be published. Required fields are marked *