{"id":957,"date":"2016-12-28T11:47:46","date_gmt":"2016-12-28T06:17:46","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=957"},"modified":"2026-09-11T20:52:43","modified_gmt":"2026-09-11T15:22:43","slug":"enable-expires-headers-gzip-compression-nginx","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/enable-expires-headers-gzip-compression-nginx\/","title":{"rendered":"Enable Expires Headers and Gzip Compression on Nginx"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Nginx is a fast and lightweight web server that can be further optimized to improve website performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we will explain how to enable <strong>Expires headers<\/strong> for static files and <strong>Gzip compression<\/strong> on Nginx. These settings can help improve page loading performance by allowing browsers to cache static content and reducing the size of files transferred between the server and client.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before proceeding, make sure you have:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Nginx installed on the server.<\/li>\n\n\n\n<li>A CentOS or Ubuntu server.<\/li>\n\n\n\n<li>Root or sudo access.<\/li>\n\n\n\n<li>Nginx configuration files available under <code>\/etc\/nginx\/<\/code>.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">If Nginx is not installed, install and configure it before proceeding.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Enable Expires Headers on Nginx<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Expires headers allow browsers to cache static files for a specified period. This can reduce repeated requests to the server and improve website performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, we will configure caching for the following static file types:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ico | css | js | gif | jpg | jpeg | png<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Open the website&#8217;s Nginx configuration file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">vi \/etc\/nginx\/sites-enabled\/default<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following configuration inside the <code>server {}<\/code> block:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">location ~* \\.(?:ico|css|js|gif|jpe?g|png)$ {<br>    expires 30d;<br>    add_header Pragma public;<br>    add_header Cache-Control \"public\";<br>}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>expires 30d;<\/code> directive tells the browser to cache the specified static files for 30 days.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check the Nginx configuration for syntax errors:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">nginx -t<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the configuration test is successful, restart Nginx:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">systemctl restart nginx<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Verification<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>curl<\/code> to check whether the Expires header is being returned:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl -I http:\/\/domain.tld\/css\/assets.min.css<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should see a response similar to:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Date: Wed, 28 Dec 2016 05:51:07 GMT<br>ETag: \"585cede3-4d843\"<br>Expires: Fri, 27 Jan 2017 05:51:07 GMT<br>Last-Modified: Fri, 23 Dec 2016 09:26:59 GMT<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The presence of the <code>Expires<\/code> header confirms that browser caching has been configured.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Enable Gzip Compression<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Gzip compression reduces the size of text-based files before they are sent to the client. This can reduce bandwidth usage and improve page loading times.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Open the Nginx configuration file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">vi \/etc\/nginx\/nginx.conf<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add or update the following configuration:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">gzip on;<br>gzip_disable \"msie6\";<br>gzip_vary on;<br>gzip_types text\/plain text\/css application\/json application\/javascript text\/xml application\/xml application\/xml+rss text\/javascript image\/x-icon image\/svg+xml;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Check the Nginx configuration:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">nginx -t<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If there are no errors, restart Nginx:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">systemctl restart nginx<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Verification<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use the following command to check whether Gzip compression is enabled:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl -I -H \"Accept-Encoding: gzip\" https:\/\/domain.tld\/css\/asset.min.css<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should see:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Cache-Control: max-age=2592000<br>Cache-Control: public<br>Content-Encoding: gzip<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>Content-Encoding: gzip<\/code> header confirms that the response is being compressed using Gzip.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Enabling <strong>Expires headers<\/strong> and <strong>Gzip compression<\/strong> on Nginx can help improve website performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Expires headers allow browsers to cache static resources, reducing repeated requests to the server. Gzip compression reduces the size of supported responses, which can help reduce bandwidth usage and improve transfer times.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After making any Nginx configuration changes, always run <code>nginx -t<\/code> before restarting or reloading the service.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. What are Expires headers?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Expires headers tell the browser how long a static resource can be cached before it needs to request it again from the server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. How long are static files cached in this configuration?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The configuration uses <code>expires 30d<\/code>, so the specified static files are cached for 30 days.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. What is Gzip compression?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Gzip is a compression method that reduces the size of supported HTTP responses before they are sent to the client.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Article<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-setup-ssl-certificate-in-nginx-on-ubuntu-20-04\/\">How to setup SSL Certificate in Nginx on Ubuntu 20.04 &#8211; Pheonix Solutions<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-install-and-configure-php-with-nginx-on-centos7\/\">How to install and configure PHP with Nginx on centos7 &#8211; Pheonix Solutions<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to our experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Have a technology challenge or looking for the right solution for your business? Our team can help you with&nbsp;<strong>cloud, DevOps, development, infrastructure, design, and more<\/strong>. Feel free to reach out to our experts&nbsp;<a href=\"https:\/\/pheonixsolutions.com\/contact\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Nginx is a fast and lightweight web server that can be further optimized to improve website performance. In this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[275,276,261,271],"class_list":["post-957","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-expires","tag-gzip","tag-linux","tag-nginx","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-fr","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/957","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=957"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/957\/revisions"}],"predecessor-version":[{"id":11670,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/957\/revisions\/11670"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}