Advertisement
  1. Code
  2. Nginx
Code

Improving SEO Using Nginx

by
Difficulty:BeginnerLength:ShortLanguages:
This post is part of a series called Nginx Guide.
Working With Nginx

Search engines exist so that humans can better look for content that they desire.

Search engine optimization is about making your website easier to understand for the crawler bots! It is about making modifications to your website, to help improve your site's user experience and performance. It should be remembered that a site should be optimized first for its users. They are the consumers of the site, and are using a search engine only to get to it.

You are most likely aware of SEO techniques with many essential elements of a web page, but might not be making the most of them. Every site needs its own optimization techniques based on content, usage, etc. Let's see how all the awesome features of Nginx help improve SEO.

Setting Max-Age Expiry Header

Setting max-age header, for static content, is one of the most effective ways to speed up the website. If someone uses a site frequently, then static content like images and CSS are not requested again from the user, but used from the local cache. 

This can be easily achieved by mentioning in the location directive that serves static files:

The max parameter sets “Expires” to the value Thu, 31 Dec 2037 23:55:55 GMT. Do not forget to reload Nginx after these changes! 

You can verify the change by sending a curl request to the resource and inspecting the response headers to have a Cache-Control max-age header with the specified value. Also, if you use Chrome Developer Tools, you should see subsequent requests to that resource being returned with a 304 (Not Modified) response status.

Gzip Pre-Compression

The amount of data downloaded by browsers to render a page is steadily increasing. Given that we can't simpy move away from reducing data, the only other way is to reduce its size for network transit.

To be able to serve gzip'ed content using Nginx, you will need to recompile Nginx with the following flags:

Now, we will need to compress all the static files (using the gzip command), place them in the same directory, and make these changes to nginx config:

With this enabled, Nginx will always look for a pre-compressed file.

Removing Whitespace

HttpStripModule removes whitespace (spaces, tabs, and new lines) from HTML documents. In combination with the gzip pre-compression module above, it can speed up your websites by a substantial amount.

Etags for Static Content

Etags are unique IDs representing the current state of the URL. They are used for cache validation, and can help save substantial bandwidth. 

You can use the following setup in Nginx to enable Etags.

Profile Workers Using Google Performance Tools

Once you start using Nginx to its capacity, you'll want to look at how well Nginx is doing its job, so it's important to gather analytics about Nginx itself. For that, we can use Google Performance Tools. The module is not built with default Nginx and can be enabled by doing this:

Remember to restart Nginx after changing the config to include:

This simple change will help us in profiling Nginx workers.

WWW to Non-WWW Redirects

For crawlers, a www domain is different from non-www, i.e. tutsplus.com is different from www.tutsplus.com. Technically, they are separate entities. A search engine can detect copied content, and this can negatively affect the site's ranking. To avoid these things, it is important to decide on standard naming and use it throughout.

As an example, let's use non-www as what we want to achieve. This can be easily achieved by using a rewrite rule:

In addition to all these, you can also achieve some interesting things using modules like:

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.