{"id":242,"date":"2015-11-24T10:11:17","date_gmt":"2015-11-24T10:11:17","guid":{"rendered":"http:\/\/cloudkul.com\/blog\/?p=242"},"modified":"2017-06-14T11:34:33","modified_gmt":"2017-06-14T11:34:33","slug":"speedup-nginx-add-ngx_pagespeed-nginx","status":"publish","type":"post","link":"https:\/\/cloudkul.com\/blog\/speedup-nginx-add-ngx_pagespeed-nginx\/","title":{"rendered":"Speedup Your Nginx"},"content":{"rendered":"<p>Pagespeed speeds up your site and reduces page load time. Ngx_pagespeed is an open-source module that automatically optimizes web server&#8217;s performance by reducing the resource size, load time of pages and associated assets (CSS, JavaScript, images) without requiring that you modify your existing content or data flow.<\/p>\n<p>By Default , Nginx does not support dynamic loading of modules available in other web servers such as Apache. Therefore, we have to rebuild it from source to enable this module.<\/p>\n<p>In this blog, we will speed up nginx by adding\u00a0ngx_pagespeed module on ubuntu 14.04. Here we go !!<\/p>\n<p>First of all, we have to update the list of the available packages, run the following command :-<\/p>\n<p><span class=\"lang:default decode:true crayon-inline\">sudo apt-get update<\/span><\/p>\n<p>Install Nginx along with all the dependencies :-<\/p>\n<p><span class=\"lang:default decode:true crayon-inline \">sudo apt-get build-dep nginx<\/span><\/p>\n<p>Next, we have to download the Nginx source package in home directory (change location accordingly) with the following command :-<\/p>\n<p><span class=\"lang:default decode:true crayon-inline \">cd ~<\/span><\/p>\n<p><span class=\"lang:default decode:true crayon-inline \">sudo apt-get source nginx<\/span><\/p>\n<p>Run &#8216;ls&#8217; command to see the content, the result should look like this :-<\/p>\n<p><span class=\"lang:default decode:true crayon-inline \">nginx-1.4.6 nginx_1.4.6-1ubuntu3.3.debian.tar.gz nginx_1.4.6-1ubuntu3.3.dsc nginx_1.4.6.orig.tar.gz<\/span><\/p>\n<p><span style=\"line-height: 1.5\">To start adding the <strong>ngx_pagespeed<\/strong> module, you first need to go to the modules folder within the extracted folder nginx-1.4.6 :-<\/span><\/p>\n<p><span class=\"lang:default decode:true crayon-inline \">cd nginx-1.4.6\/debian\/modules<\/span><\/p>\n<p>In this directory, download the latest ngx_pagespeed source archive from its Github repository with the command:-<\/p>\n<p><span class=\"lang:default decode:true crayon-inline \">sudo wget https:\/\/github.com\/pagespeed\/ngx_pagespeed\/archive\/master.zip<\/span><\/p>\n<p>Unzip it :-<\/p>\n<p><span class=\"lang:default decode:true crayon-inline\">unzip master.zip<\/span><\/p>\n<p>This will create a new directory called <strong>ngx_pagespeed-master<\/strong> inside your ~\/nginx-1.4.6\/debian\/modules directory.For convenience rename this directory to just ngx_pagespeed :-<\/p>\n<p><span style=\"line-height: 1.5\"><span class=\"lang:default decode:true crayon-inline\">sudo mv ngx_pagespeed-master ngx_pagespeed <\/span>\u00a0<\/span><\/p>\n<p><span style=\"line-height: 1.5\">Download the PageSpeed Optimization Libraries (psol) which are required for the compilation. Run the following commands :-<\/span><\/p>\n<p><span class=\"lang:default decode:true crayon-inline\">cd ngx_pagespeed<\/span><\/p>\n<p><span class=\"lang:default decode:true crayon-inline \">sudo wget https:\/\/dl.google.com\/dl\/page-speed\/psol\/1.9.32.6.tar.gz<\/span><\/p>\n<p><span style=\"line-height: 1.5\"><span class=\"lang:default decode:true crayon-inline \">sudo tar -xzvf 1.9.32.6.tar.gz <\/span>\u00a0<\/span><\/p>\n<p><span style=\"line-height: 1.5\">At this point we are ready to customize the compilation rules and include ngx_pagespeed in the installation. For this purpose edit the file <strong>~\/nginx-1.4.6\/debian\/rules<\/strong> :-<\/span><\/p>\n<p><span class=\"lang:default decode:true crayon-inline \">sudo nano ~\/nginx-1.4.6\/debian\/rules<\/span><\/p>\n<p>At the end of the <strong>light_configure_flags<\/strong> configuration block, add the line:-<\/p>\n<p><span class=\"lang:default decode:true crayon-inline \">&#8211;add-module=$(MODULESDIR)\/ngx_pagespeed \u00a0\\<\/span><\/p>\n<div class=\"panel panel-info\">\n<div class=\"panel-heading\">Note<\/div>\n<div class=\"panel-body\">\n<p>\nNote:- Please don&#8217;t forget to add\u00a0a backslash (\\) at the end of the row.\n<\/p>\n<\/div>\n<\/div>\n<p>The scenario should look like this :-<\/p>\n<pre class=\"lang:default decode:true\">config.status.light: config.env.light \r\n       cd $(BUILDDIR_light) &amp;&amp; .\/configure \\ \r\n          $(common_configure_flags) \\ \r\n          --with-http_gzip_static_module \\ \r\n          --without-http_browser_module \\\r\n          --without-http_geo_module \\\r\n          --without-http_limit_req_module \\ \r\n          --without-http_limit_zone_module \\\r\n          --without-http_memcached_module \\ \r\n          --without-http_referer_module \\\r\n          --without-http_scgi_module \\ \r\n          --without-http_split_clients_module \\ \r\n          --without-http_ssi_module \\ \r\n          --without-http_userid_module \\ \r\n          --without-http_uwsgi_module \\ \r\n          --add-module=$(MODULESDIR)\/nginx-echo \\ \r\n          --add-module=$(MODULESDIR)\/ngx_pagespeed \\\r\n          &gt;$@\r\n          touch $@<\/pre>\n<div class=\"panel panel-info\">\n<div class=\"panel-heading\">Note<\/div>\n<div class=\"panel-body\">\n<p>\nThere are\u00a0five different scenarios for building Nginx&#8217;s packages: core, full, light, extras and naxsi.You could add the same line to the other build scenarios too if you find a different Nginx setup more convenient.\n<\/p>\n<\/div>\n<\/div>\n<p>Next,we have to increase the source package version. For this edit the changelog file :-<\/p>\n<p><span class=\"lang:default decode:true crayon-inline \">sudo nano \u00a0~\/nginx-1.4.6\/debian\/changelog<\/span><\/p>\n<p>Add a custom tag such as <strong>pagespeed<\/strong> at the very first line of \u00a0changelog file containing the version number like this :-<\/p>\n<p><span class=\"lang:default decode:true crayon-inline\">nginx (1.4.6-1ubuntu3.3-pagespeed) trusty-proposed; urgency=medium<\/span><\/p>\n<p>Now you have customized the build to include the ngx_pagespeed module, you are ready to build Nginx.Go to the directory ~\/nginx-1.4.6\/ and\u00a0run the command to build the new custom<strong> Nginx binary packages\u00a0<\/strong>:-<\/p>\n<pre class=\"lang:default decode:true \">cd ~\/nginx-1.4.6\/<\/pre>\n<pre class=\"lang:default decode:true \">sudo dpkg-buildpackage -b<\/pre>\n<p>After the build completes, see the content of root.You should find a lot of .deb packages.\u00a0The ones you need are called <strong>nginx-common_1.4.6-1ubuntu3.3-pagespeed_all.deb<\/strong> (containing the common Nginx files) and <strong>nginx-light_1.4.6-1ubuntu3.3-pagespeed_amd64.deb<\/strong> (containing your custom light server).<\/p>\n<p>To install your custom Nginx with pagespeed module run the command :-<\/p>\n<p><span class=\"lang:default decode:true crayon-inline \">sudo dpkg -i nginx-common_1.4.6-1ubuntu3.3-pagespeed_all.deb nginx-light_1.4.6-1ubuntu3.3-pagespeed_amd64.deb<\/span><\/p>\n<p>You now have Nginx installed. The next step is to enable the ngx_pagespeed module.Before enabling the module, you have to create a folder, where it will cache the files for your website :-<\/p>\n<p><span class=\"lang:default decode:true crayon-inline \">sudo mkdir -p \/var\/ngx_pagespeed_cache<\/span><\/p>\n<p>Change the ownership of this folder to the nginx user :-<\/p>\n<p><span class=\"lang:default decode:true crayon-inline \">sudo chown -R www-data:www-data \/var\/ngx_pagespeed_cache<\/span><\/p>\n<p>Add the following lines to the http block of nginx&#8217;s main configuration file \/etc\/nginx\/nginx.conf :-<\/p>\n<pre class=\"lang:default decode:true \">##\r\n# Pagespeed Settings\r\n##\r\n\r\npagespeed on;\r\npagespeed FileCachePath \/var\/ngx_pagespeed_cache;<\/pre>\n<p>The scenario should look like this :-<\/p>\n<pre class=\"lang:default decode:true\">...\r\nhttp {\r\n\r\n##\r\n# Basic Settings\r\n##\r\n\r\nsendfile on;\r\ntcp_nopush on;\r\ntcp_nodelay on;\r\nkeepalive_timeout 65;\r\ntypes_hash_max_size 2048;\r\n# server_tokens off;\r\n\r\n# server_names_hash_bucket_size 64;\r\n# server_name_in_redirect off;\r\n\r\n##\r\n# Pagespeed Settings\r\n##\r\n\r\npagespeed on;\r\npagespeed FileCachePath \/var\/ngx_pagespeed_cache;\r\n...<\/pre>\n<p>Also, you need to add pagespeed configuration lines to every server block file located in \/etc\/nginx\/sites-available. For example, edit the \/etc\/nginx\/sites-available\/default file :-<\/p>\n<pre class=\"lang:default decode:true \">sudo nano \/etc\/nginx\/sites-available\/default<\/pre>\n<p>Add the following to the end of the server block :-<\/p>\n<pre class=\"lang:default decode:true \"># Ensure requests for pagespeed optimized resources go to the pagespeed\r\n# handler and no extraneous headers get set.\r\nlocation ~ \"\\.pagespeed\\.([a-z]\\.)?[a-z]{2}\\.[^.]{10}\\.[^.]+\" { add_header \"\" \"\"; }\r\nlocation ~ \"^\/ngx_pagespeed_static\/\" { }\r\nlocation ~ \"^\/ngx_pagespeed_beacon\" { }<\/pre>\n<p>The above pagespeed configuration lines ensure that pagespeed will optimize every site&#8217;s resources.<\/p>\n<p>Finally, restart Nginx server :-<\/p>\n<pre class=\"lang:default decode:true \">sudo service nginx restart<\/pre>\n<p>Test the installation by\u00a0running the Nginx binary like this :-<\/p>\n<p><span style=\"line-height: 1.5\"><span class=\"lang:default decode:true crayon-inline \">sudo \/usr\/sbin\/nginx -V <\/span>\u00a0<\/span><\/p>\n<p><span style=\"line-height: 1.5\">You should see the ngx_pagespeed module listed among the other modules :-<\/span><\/p>\n<pre class=\"lang:default decode:true\">nginx version: nginx\/1.4.6\r\n...\r\n--add-module=root\/nginx-1.4.6\/debian\/modules\/ngx_pagespeed<\/pre>\n<p>After that check for the X-Page-Speed header like this :-<\/p>\n<pre class=\"lang:default decode:true\">curl -I -p http:\/\/localhost| grep X-Page-Speed<\/pre>\n<p>If the ngx_pagespeed module works fine, you should see it in the output along with its version :-<\/p>\n<pre class=\"lang:default decode:true\">X-Page-Speed: 1.9.32.6-7321<\/pre>\n<p style=\"text-align: center\"><a href=\"http:\/\/cloudkul.com\/contact\/\" target=\"_blank\">FOR ANY TYPE OF QUERY OR HELP, PLEASE CONTACT US<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pagespeed speeds up your site and reduces page load time. Ngx_pagespeed is an open-source module <a class=\"text-primary\" title=\"read more\" href=\"https:\/\/cloudkul.com\/blog\/speedup-nginx-add-ngx_pagespeed-nginx\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":5,"featured_media":320,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Speedup Your Nginx - Cloudkul<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudkul.com\/blog\/speedup-nginx-add-ngx_pagespeed-nginx\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Speedup Your Nginx - Cloudkul\" \/>\n<meta property=\"og:description\" content=\"Pagespeed speeds up your site and reduces page load time. Ngx_pagespeed is an open-source module [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudkul.com\/blog\/speedup-nginx-add-ngx_pagespeed-nginx\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloudkul\" \/>\n<meta property=\"article:published_time\" content=\"2015-11-24T10:11:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-06-14T11:34:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2015\/11\/Reduce-the-page-load-Time.png\" \/>\n\t<meta property=\"og:image:width\" content=\"848\" \/>\n\t<meta property=\"og:image:height\" content=\"422\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Naina Johari\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudkul.com\/blog\/speedup-nginx-add-ngx_pagespeed-nginx\/\",\"url\":\"https:\/\/cloudkul.com\/blog\/speedup-nginx-add-ngx_pagespeed-nginx\/\",\"name\":\"Speedup Your Nginx - Cloudkul\",\"isPartOf\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/#website\"},\"datePublished\":\"2015-11-24T10:11:17+00:00\",\"dateModified\":\"2017-06-14T11:34:33+00:00\",\"author\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/38f7cddff574c7fe989d6ca2df61fc57\"},\"breadcrumb\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/speedup-nginx-add-ngx_pagespeed-nginx\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudkul.com\/blog\/speedup-nginx-add-ngx_pagespeed-nginx\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudkul.com\/blog\/speedup-nginx-add-ngx_pagespeed-nginx\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Speedup Your Nginx\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#website\",\"url\":\"https:\/\/cloudkul.com\/blog\/\",\"name\":\"Cloudkul\",\"description\":\"Host your eCommerce Store on AWS with Optimized Performance\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cloudkul.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/38f7cddff574c7fe989d6ca2df61fc57\",\"name\":\"Naina Johari\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c7a0be3afff58963975900f809e57046?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c7a0be3afff58963975900f809e57046?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g\",\"caption\":\"Naina Johari\"},\"url\":\"https:\/\/cloudkul.com\/blog\/author\/naina-johari379\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Speedup Your Nginx - Cloudkul","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cloudkul.com\/blog\/speedup-nginx-add-ngx_pagespeed-nginx\/","og_locale":"en_US","og_type":"article","og_title":"Speedup Your Nginx - Cloudkul","og_description":"Pagespeed speeds up your site and reduces page load time. Ngx_pagespeed is an open-source module [...]","og_url":"https:\/\/cloudkul.com\/blog\/speedup-nginx-add-ngx_pagespeed-nginx\/","og_site_name":"Cloudkul","article_published_time":"2015-11-24T10:11:17+00:00","article_modified_time":"2017-06-14T11:34:33+00:00","og_image":[{"width":848,"height":422,"url":"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2015\/11\/Reduce-the-page-load-Time.png","type":"image\/png"}],"author":"Naina Johari","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/cloudkul.com\/blog\/speedup-nginx-add-ngx_pagespeed-nginx\/","url":"https:\/\/cloudkul.com\/blog\/speedup-nginx-add-ngx_pagespeed-nginx\/","name":"Speedup Your Nginx - Cloudkul","isPartOf":{"@id":"https:\/\/cloudkul.com\/blog\/#website"},"datePublished":"2015-11-24T10:11:17+00:00","dateModified":"2017-06-14T11:34:33+00:00","author":{"@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/38f7cddff574c7fe989d6ca2df61fc57"},"breadcrumb":{"@id":"https:\/\/cloudkul.com\/blog\/speedup-nginx-add-ngx_pagespeed-nginx\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudkul.com\/blog\/speedup-nginx-add-ngx_pagespeed-nginx\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/cloudkul.com\/blog\/speedup-nginx-add-ngx_pagespeed-nginx\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Speedup Your Nginx"}]},{"@type":"WebSite","@id":"https:\/\/cloudkul.com\/blog\/#website","url":"https:\/\/cloudkul.com\/blog\/","name":"Cloudkul","description":"Host your eCommerce Store on AWS with Optimized Performance","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cloudkul.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/38f7cddff574c7fe989d6ca2df61fc57","name":"Naina Johari","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c7a0be3afff58963975900f809e57046?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c7a0be3afff58963975900f809e57046?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g","caption":"Naina Johari"},"url":"https:\/\/cloudkul.com\/blog\/author\/naina-johari379\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/242"}],"collection":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/comments?post=242"}],"version-history":[{"count":36,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/242\/revisions"}],"predecessor-version":[{"id":274,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/242\/revisions\/274"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/media\/320"}],"wp:attachment":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/media?parent=242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/categories?post=242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/tags?post=242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}