{"id":5537,"date":"2014-10-18T16:56:11","date_gmt":"2014-10-18T08:56:11","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=5537"},"modified":"2023-07-10T14:14:49","modified_gmt":"2023-07-10T14:14:49","slug":"how-to-setup-varnish-3-0-in-front-of-nginx-php5-4-php-fpm","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-setup-varnish-3-0-in-front-of-nginx-php5-4-php-fpm\/","title":{"rendered":"How to Setup Varnish 3.0 in Front of Nginx, PHP5.4 (PHP-FPM)"},"content":{"rendered":"<p>In web hosting one common challenge for server administrators is how to effectively manage the load on their servers, especially when running resource-intensive applications like WordPress. The solution? Implementing a caching HTTP reverse proxy, such as Varnish Cache, in front of your Nginx and PHP5.4 (PHP-FPM) web server.<\/p>\n<p>Varnish Cache is a popular web accelerator that can work alongside either Apache or Nginx web servers. In this tutorial, we will focus on setting up Varnish to run on port 80, while Nginx operates on port 8080. This setup can significantly speed up your website&#8217;s delivery to visitors, reduce CPU time, and minimize database requests and file lookups.<\/p>\n<p>By implementing Varnish Cache, you can enjoy several benefits. It can reduce server CPU load and time, increase website load speed, handle a large number of website visitors, and even support load balancing. For more information on the benefits and features of Varnish Cache, you can visit this <a href=\"https:\/\/webhostinggeeks.com\/blog\/varnish-mechanism-features-capabilities-advantages\/\">page<\/a>.<\/p>\n<p>This tutorial will guide you through the process of setting up Varnish 3.0 in front of an Nginx, PHP5.4 (PHP-FPM) web server. The steps have been tested on CentOS 6, CentOS 7, Oracle Linux 7, and RHEL 7. If you are looking for other proxy server software options, you can check out this <a href=\"https:\/\/webhostinggeeks.com\/best\/proxy-servers\/\">page<\/a>.<\/p>\n<h2>Step 1: Setup Nginx, PHP5.4 (PHP-FPM) as Web Server<\/h2>\n<p>Before we can install and configure Varnish, we need to ensure that Nginx and PHP5.4 (PHP-FPM) are properly set up as your web server. If you need a guide on how to do this, you can refer to this <a href=\"https:\/\/webhostinggeeks.com\/howto\/how-to-setup-nginxphp5-4-php-fpm-mysql-5-5-on-centos-6-5-vps\/\">tutorial<\/a>.<\/p>\n<h2>Step 2: Install Varnish<\/h2>\n<p>The next step is to install Varnish on your server. This involves downloading the Varnish release package and installing it using the package manager.<\/p>\n<pre>[root@vps ~]# wget https:\/\/repo.varnish-cache.org\/redhat\/varnish-3.0\/el6\/noarch\/varnish-release\/varnish-release-3.0-1.el6.noarch.rpm\r\n<\/pre>\n<pre>[root@vps ~]# rpm -Uvh varnish-release-3.0-1.el6.noarch.rpm\r\n<\/pre>\n<pre>[root@vps ~]# yum install varnish -y\r\n<\/pre>\n<h2>Step 3: Configure Varnish<\/h2>\n<p>After installing Varnish, we need to configure it to work with our Nginx web server. This involves modifying the Varnish configuration files to specify that Nginx is running as the backend server on localhost at port 8080, while Varnish will run in front of it listening on port 80.<\/p>\n<h3>Modify \/etc\/sysconfig\/varnish:<\/h3>\n<pre>[root@vps ~]# vim \/etc\/sysconfig\/varnish\r\n<\/pre>\n<p>Add the following. If your VPS running SSD disk, you can select varnish_storage as a cache method instead of memory (-s malloc):<\/p>\n<pre>\r\n..\r\n..\r\nDAEMON_OPTS=\"-a :80 \\\r\n             -T localhost:6082 \\\r\n             -f \/etc\/varnish\/default.vcl \\\r\n             -u varnish -g varnish \\\r\n             -S \/etc\/varnish\/secret \\\r\n             -p thread_pool_add_delay=2 \\\r\n             -p thread_pools=2 \\\r\n             -p thread_pool_min=400 \\\r\n             -p thread_pool_max=4000 \\\r\n             -p session_linger=50 \\\r\n             -p sess_workspace=262144 \\\r\n             -s file,\/var\/lib\/varnish\/varnish_storage.bin,512m\"\r\n..\r\n..\r\nVARNISH_LISTEN_PORT=80\r\n..\r\n..\r\n<\/pre>\n<h3>Modify \/etc\/varnish\/default.vcl:<\/h3>\n<pre>\r\n[root@vps ~]# vim \/etc\/varnish\/default.vcl\r\n<\/pre>\n<p>Add below:<\/p>\n<pre>\r\nbackend default {\r\n    .host = \"127.0.0.1\";\r\n    .port = \"8080\";\r\n    .connect_timeout = 600s;\r\n    .first_byte_timeout = 600s;\r\n    .between_bytes_timeout = 600s;\r\n    .max_connections = 800;\r\n}\r\n\r\n\r\nacl purge {\r\n\t\"127.0.0.1\";\r\n}\r\n\r\nsub vcl_recv {\r\n\r\n\r\n\t# Allow purge requests\r\n\tif (req.request == \"PURGE\") {\r\n        if (!client.ip ~ purge) {\r\n            error 405 \"Not allowed.\";\r\n        }\r\n        ban(\"req.url ~ ^\" + req.url + \" &amp;&amp; req.http.host == \" + req.http.host);\r\n        return(lookup);\r\n    }\r\n\r\n\t# Add header for sending client ip to backend\r\n\tset\treq.http.X-Forwarded-For = client.ip;\r\n\r\n\t# Normalize\tcontent-encoding\r\n\tif (req.http.Accept-Encoding) {\r\n        if (req.url ~ \"\\.(jpg|png|gif|gz|tgz|bz2|lzma|tbz)(\\?.*|)$\") {\r\n            remove req.http.Accept-Encoding;\r\n        } elsif (req.http.Accept-Encoding ~ \"gzip\") {\r\n            set req.http.Accept-Encoding = \"gzip\";\r\n        } elsif (req.http.Accept-Encoding ~ \"deflate\") {\r\n            set\treq.http.Accept-Encoding = \"deflate\";\r\n        } else {\r\n            remove req.http.Accept-Encoding;\r\n        }\r\n    }\r\n\r\n    # Remove cookies and query string for real static files\r\n    if (req.url ~ \"^\/[^?]+\\.(gif|jpg|jpeg|swf|css|js|txt|flv|mp3|mp4|pdf|ico|png|gz|zip|lzma|bz2|tgz|tbz)(\\?.*|)$\") {\r\n       unset req.http.cookie;\r\n       set req.url = regsub(req.url, \"\\?.*$\", \"\");\r\n    }\r\n\r\n    # Don't cache admin\r\n    if (req.url ~ \"((wp-(login|admin|comments-post.php|cron.php))|login)\" || req.url ~ \"preview=true\" || req.url ~ \"xmlrpc.php\") {\r\n        return (pass);\r\n    } else {\r\n    \tunset req.http.cookie;\r\n    }\r\n}\r\n\r\nsub vcl_hit {\r\n\t# purge cached objects from memory\r\n\tif (req.request == \"PURGE\") {\r\n\t\tpurge;\r\n\t\terror 200 \"Purged\";\r\n\t}\r\n}\r\n\r\nsub vcl_miss {\r\n\t# purge cached objects varients from memory\r\n\tif (req.request == \"PURGE\") {\r\n\t\tpurge;\r\n\t\terror 404 \"Purged varients\";\r\n\t}\r\n}\r\n\r\nsub vcl_fetch {\r\n\t# Dont cache admin\r\n\tif (req.url ~ \"(wp-(login|admin|comments-post.php|cron.php))|login\" || req.url ~ \"preview=true\" || req.url ~ \"xmlrpc.php\") {\r\n    \t    return (deliver);\r\n\t} else {\r\n    \t    if ( beresp.ttl &gt; 0s ) {\r\n    \t        unset beresp.http.set-cookie;\r\n    \t    }\r\n\t}\r\n}\r\n\r\n\r\nsub vcl_deliver {\r\n\r\n\t# Remove unnecessary headers\r\n\tremove resp.http.Server;\r\n\tremove resp.http.X-Powered-By;\r\n\tremove resp.http.X-Varnish;\r\n\tremove resp.http.Via;\r\n\r\n\t# DIAGNOSTIC HEADERS\r\n\tif (obj.hits &gt; 0) {\r\n\t\tset resp.http.X-Cache = \"HIT\";\r\n\t} else {\r\n\t\tset resp.http.X-Cache = \"MISS\";\r\n\t}\r\n\r\n}\r\n<\/pre>\n<h2>Step 4: Configure NGINX to work with Varnish:<\/h2>\n<h3>Configure default.conf:<\/h3>\n<pre>[root@vps ~]# vim \/etc\/nginx\/conf.d\/default.conf\r\n<\/pre>\n<p>Add the following :<\/p>\n<pre>..\r\nserver {\r\n    listen       127.0.0.1:8080;\r\n    server_name  localhost;\r\n..\r\n<\/pre>\n<h3>Configure common.conf:<\/h3>\n<pre>[root@vps ~]# vim \/etc\/nginx\/conf.d\/common.conf\r\n<\/pre>\n<pre>..\r\n..\r\n\r\nlisten 127.0.0.1:8080;\r\n..\r\n..\r\n\r\n<\/pre>\n<h3>Configure Vhost for domain example.com:<\/h3>\n<pre>\r\n[root@vps ~]# vim \/etc\/nginx\/sites-available\/example.com.conf\r\n<\/pre>\n<p>Change listen to <strong>127.0.0.1:8080;<\/strong> :<\/p>\n<pre>\r\nserver {\r\nlisten       127.0.0.1:8080;\r\n    server_name example.com;\r\n    rewrite ^\/(.*)$ http:\/\/www.example.com\/$1 permanent;\r\n}\r\n..\r\n<\/pre>\n<h3>Restart NGINX, php-fpm and Varnish:<\/h3>\n<pre>\r\n[root@vps ~]# service nginx restart; service php-fpm restart; service varnish restart\r\n<\/pre>\n<h3>Make sure NGINX listening to port 8080 and Varnish port 80:<\/h3>\n<pre>[root@vps ~]# netstat -plunt  | grep LISTEN\r\n<\/pre>\n<pre>tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3119\/varnishd\r\ntcp        0      0 127.0.0.1:8080              0.0.0.0:*                   LISTEN      3065\/nginx\r\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>This tutorial is just the beginning of your journey with Varnish Cache. There&#8217;s a lot more to explore and learn. For instance, you might want to look into other types of web servers, such as Apache, Litespeed, and more. You can find more information on these topics on this <a href=\"https:\/\/webhostinggeeks.com\/best\/web-server\/\">page<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In web hosting one common challenge for server administrators is how to effectively manage the load on their servers, especially when running resource-intensive applications like WordPress. The solution? Implementing a&#8230;<\/p>\n","protected":false},"author":6,"featured_media":5538,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[1014,1078],"tags":[2076,1605,1876,1933],"class_list":["post-5537","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nginx","category-varnish-cache","tag-cache","tag-nginx","tag-varnish","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/5537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=5537"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/5537\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/5538"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=5537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=5537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=5537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}