Best Apache and Nginx configs for speeding up website page load time

Google have announced that page load time would be one of the ranking factors a lot time ago. Now this information is available in official webmaster manual. Page load time can be tasted on Google PageSpeed insights page.

In most cases, result of 80/100 can be scored simply by editing the web server settings by enabling deflate and caching mods. Under the cut there are .htaccess file and nginx configs, I’ve been using to optimize page load time for a long time. Make sure you also follow other recommendations provided by Google, because they can simnifically decrease page load time and thus improve your site visitors expirience.

If these configs do not satisfy you with performance you should consider using special modules developed by google, available both for nginx and apache at the google PageSpeed module page.



AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascrip

<IfModule mod_deflate.c>
#The following line is enough for .js and .css
AddOutputFilter DEFLATE js css
AddOutputFilterByType DEFLATE text/plain text/xml application/xhtml+xml text/css application/xml application/rss+xml application/atom_xml application/x-javascript application/x-httpd-php application/x-httpd-fastphp text/html

#The following lines are to avoid bugs with some browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>

# BEGIN EXPIRES
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault “access plus 10 days”
ExpiresByType text/css “access plus 1 week”
ExpiresByType text/plain “access plus 1 month”
ExpiresByType image/gif “access plus 1 month”
ExpiresByType image/png “access plus 1 month”
ExpiresByType image/jpeg “access plus 1 month”
ExpiresByType application/x-javascript “access plus 1 month”
ExpiresByType application/javascript “access plus 1 week”
ExpiresByType application/x-icon “access plus 1 year”
</IfModule>
# END EXPIRES


<IfModule mod_headers.c>
<FilesMatch "\.(js|css|xml|gz)$">
Header append Vary Accept-Encoding
</FilesMatch>
<FilesMatch "\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "public"
</FilesMatch>
<FilesMatch "\.(css)$">
Header set Cache-Control "public"
</FilesMatch>
<FilesMatch "\.(js)$">
Header set Cache-Control "private"
</FilesMatch>
<FilesMatch "\.(x?html?|php)$">
Header set Cache-Control "private, must-revalidate"
</FilesMatch>
</IfModule>

This .htaccess can be used to score 78/100 from scratch at Google PageSpeed insights test.

And the following lines should be added to nginx file config at /etc/nginx/nginx.conf:


gzip on;
gzip_vary on;
# # Disable gzip for certain browsers.
gzip_disable "MSIE [1-6]\.";
gzip_proxied any;
gzip_http_version 1.1;
gzip_min_length 0;
gzip_comp_level 6;
# make sure gzip does not lose large gzipped js or css files
# # see http://blog.leetsoft.com/2007/07/25/nginx-gzip-ssl.html
gzip_buffers 16 8k;
gzip_types text/plain text/html text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;

And to your website config (f.e. /etc/nginx/sites-enabled/default) you should add these lines in your server section to enable browser caching:


location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}

location ~* \.(pdf)$ {
expires 30d;
}

P.S. You can also get .htaccess file with the direct link: https://malev.ru/htaccess.txt

 

Kirill