{"id":6228,"date":"2021-03-18T15:53:15","date_gmt":"2021-03-18T10:23:15","guid":{"rendered":"https:\/\/www.skynats.com\/?p=6228"},"modified":"2024-12-11T15:50:41","modified_gmt":"2024-12-11T10:20:41","slug":"speed-up-nginx-with-varnish-cache-on-centos7","status":"publish","type":"post","link":"https:\/\/www.skynats.com\/blog\/speed-up-nginx-with-varnish-cache-on-centos7\/","title":{"rendered":"How to Speed Up Nginx with Varnish Cache on CentOS 7"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Varnish Cache is an open-source, high-performance HTTP accelerator designed for speeding up web servers.\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In short, this blog will show you how to install and use&nbsp;<strong>Varnish Cache<\/strong>&nbsp;as a front-end to the Nginx HTTP server in&nbsp;<strong>CentOS 7<\/strong>. Need any assistance regarding installation and configuration, you can take our technical team support to been done on your behalf under our <a href=\"https:\/\/www.skynats.com\/linux-server-management\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"color:#026f9e\" class=\"has-inline-color\">server management services<\/span><\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Install Nginx Web Server on CentOS 7<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By using the YUM package manager you can install the Nginx HTTP server from the default CentOS repositories.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># yum install nginx<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"602\" height=\"355\" src=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-14.png\" alt=\"\" class=\"wp-image-6230\" srcset=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-14.png 602w, https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-14-300x177.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Install Nginx on CentOS 7<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once done with the installation, start enabling the Nginx service automatically at system boot.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># systemctl start nginx\n# systemctl enable nginx\n# systemctl status nginx<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"602\" height=\"328\" src=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-15.png\" alt=\"\" class=\"wp-image-6231\" srcset=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-15.png 602w, https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-15-300x163.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Start and Enable Nginx at Boot<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By using the following commands to modify system firewall rules to permit inbound packets on port 80.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># firewall-cmd --zone=public --permanent --add-port=80\/tcp\n# firewall-cmd --reload\n# firewall-cmd --zone=public --permanent --add-port=8080\/tcp\n# firewall-cmd \u2013reload\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"602\" height=\"80\" src=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-16.png\" alt=\"\" class=\"wp-image-6232\" srcset=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-16.png 602w, https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-16-300x40.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Install Varnish Cache on CentOS 7<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For the varnish cache 6 latest version&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The pre-compiled RPM packages available for the latest version of&nbsp;<strong>Varnish Cache 6<\/strong>&nbsp;(i.e&nbsp;<strong>6.5<\/strong>&nbsp;at the time of writing), therefore you are requested to add the official Varnish Cache repository.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;Enable the EPEL repository in order to install several dependency packages.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># yum install -y epel-release<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, install&nbsp;<strong>pygpgme<\/strong>, the package that handles the signatures and yum-utils, a collection of useful utilities that extend yum&#8217;s native features in various ways.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># yum install pygpgme yum-utils<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create a file named&nbsp;<strong>\/etc\/yum.repos.d\/varnishcache_varnish5.repo<\/strong>&nbsp;that contains the repository configuration.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># vi \/etc\/yum.repos.d\/varnishcache_varnish65.repo<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Important<\/strong>: Ensure to replace el and 7 in the config below with your Linux distribution and version:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;varnishcache_varnish65]\nname=varnishcache_varnish65\nbaseurl=https:\/\/packagecloud.io\/varnishcache\/varnish65\/el\/7\/$basearch\nrepo_gpgcheck=1\ngpgcheck=0\nenabled=1\ngpgkey=https:\/\/packagecloud.io\/varnishcache\/varnish65\/gpgkey\nsslverify=1\nsslcacert=\/etc\/pki\/tls\/certs\/ca-bundle.crt\nmetadata_expire=300<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Update local yum cache and then install varnish cache package by running the command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># yum -q makecache -y --disablerepo='*' --enablerepo='varnishcache_varnish65'\n# yum install varnish <\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"277\" src=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-17.png\" alt=\"\" class=\"wp-image-6233\" srcset=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-17.png 602w, https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-17-300x138.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Once the varnish cache has been installed, and the main executable will be installed as&nbsp;<strong>\/usr\/sbin\/varnishd<\/strong>&nbsp;and varnish configuration files are located in&nbsp;<strong>\/etc\/varnish\/<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\/etc\/varnish\/default.vcl<\/strong>\u00a0\u2013 this is the main varnish configuration file, it has been written by using vanish configuration language(VCL).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Now start the varnish service, by enabling automatically start during system boot, ensure it&#8217;s running well by verifying the status.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># systemctl start varnish\n# systemctl enable varnish\n# systemctl status varnish<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"302\" src=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-18.png\" alt=\"\" class=\"wp-image-6234\" srcset=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-18.png 602w, https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-18-300x150.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Confirm that varnish has successfully installed on your system.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ which varnishd\n$ varnishd -V<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Sample Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>varnishd (varnish-6.5.1 revision 1dae23376bb5ea7a6b8e9e4b9ed95cdc9469fb64)\nCopyright (c) 2006 Verdens Gang AS\nCopyright (c) 2006-2020 Varnish Software<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Configure Nginx to Work With Varnish Cache<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;Configure Nginx to work with Varnish Cache, where you can see the Nginx listens on port&nbsp;<strong>80&nbsp;<\/strong>as default. later, you need to change the default Nginx port to&nbsp;<strong>8080.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Open the Nginx config file&nbsp;<strong>\/etc\/nginx\/nginx.conf<\/strong>&nbsp;and find default listen&nbsp;<strong>80<\/strong>&nbsp;and change it to&nbsp;&nbsp;<strong>8080<\/strong>&nbsp;in the server block.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># vi \/etc\/nginx\/nginx.conf<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"295\" src=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-19.png\" alt=\"\" class=\"wp-image-6235\" srcset=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-19.png 602w, https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-19-300x147.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: Do it in all server block configuration files (usually created under&nbsp;<strong>\/etc\/nginx\/conf.d\/<\/strong>) for websites that serve under the varnish.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Later, find the parameter&nbsp;<strong>ExecStart&nbsp;<\/strong>by opening the varnish service configuration file that displaces the port and then tries to change the port from 6081 to 80.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># systemctl edit --full varnish<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The line displaces as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ExecStart=\/usr\/sbin\/varnishd -a :80 -f \/etc\/varnish\/default.vcl -s malloc,256m<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"466\" src=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-20.png\" alt=\"\" class=\"wp-image-6236\" srcset=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-20.png 602w, https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2021\/03\/image-20-300x232.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Next, set up Nginx as a backend server for Varnish proxy, in the&nbsp;<strong>\/etc\/varnish\/default.vcl<\/strong>&nbsp;configuration file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># vi \/etc\/varnish\/default.vcl <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Define host IP and port from the backend section<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>backend default {\n    .host = \"127.0.0.1\";\n    .port = \"8080\";\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Set this to point to your actual content server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Restart Ngnix HTTPD once all the necessary configuration has been done. You can see the above changes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># systemctl daemon-reload\n# systemctl restart nginx\n# systemctl restart varnish<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Test Varnish Cache on Nginx<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, test the varnish cache is enabled and working well with the Nginx service by using the following command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># curl -I http:\/\/localhost<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>HTTP\/1.1 200 OK\nServer: nginx\/1.16.1\nDate: Wed, 06 Jan 2021 09:24:18 GMT\nContent-Type: text\/html\nContent-Length: 4833\nLast-Modified: Fri, 16 May 2014 15:12:48 GMT\nETag: \"53762af0-12e1\"\nX-Varnish: 2\nAge: 0\nVia: 1.1 varnish (Varnish\/6.5)\nAccept-Ranges: bytes\nConnection: keep-alive<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conclusion<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This blog will explain to you how to install and setup varnish cache for the Nginx HTTP server on centOS 7. If you need any assistance,&nbsp;our&nbsp;<a href=\"https:\/\/www.skynats.com\/contact-us\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"color:#036f9e\" class=\"has-inline-color\">technical team<\/span><\/a>&nbsp;will be available at any time to help you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Varnish Cache is an open-source, high-performance HTTP accelerator designed for speeding up web servers.\u00a0 In short, this blog will show you how to install and use&nbsp;Varnish Cache&nbsp;as a front-end to the Nginx HTTP server in&nbsp;CentOS 7. Need any assistance regarding installation and configuration, you can take our technical team support to been done on your [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,208,239,285,68,286],"tags":[529,528],"class_list":["post-6228","post","type-post","status-publish","format-standard","hentry","category-blog","category-centos-7","category-linux","category-nginx","category-server-management","category-varnish-cache","tag-install-nginx-web-server-on-centos-7","tag-install-varnish-cache-on-centos-7"],"_links":{"self":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/6228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/comments?post=6228"}],"version-history":[{"count":0,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/6228\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/media?parent=6228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/categories?post=6228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/tags?post=6228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}