Fork me on GitHub

Summary: Seravo.com heavily utilises Nginx in both our WordPress environment and routing layer. Here’s a short description of our Nginx use

Background

Traditionally, most hosting sites have been using Apache, bundled with PHP as their web server of choice. In the last few years however, many have migrated from Apache to Nginx due to performance benefits and better flexibility.

Default settings

Nginx is already pre-configured for WordPress use in Seravo. No need to follow guides online on how to set up Nginx with WordPress.

Our default Nginx configuration includes:

  • Strong HTTPS settings (see example)
  • A Let’s Encrypt certificate for your public domains
  • HTTP cache with stale cache configured
  • Automatic expiration headers for static content
  • The Pagespeed module.
  • User configurable API
  • Gzip
  • And lots more…

Configuration

In most generic Apache hosting platforms, you have the ability to use .htaccess files for adding custom rules to your web server. There are multiple reasons why this is not optimal, just to mention one, is the inherently slower execution time for all page loads.

Instead, we give our users access to their nginx configuration via a configuration directory, containing .conf files read by Nginx on startup. The configuration is from the path /data/wordpress/nginx/*.conf.

Note: Under the hood, your custom Nginx configuration is included like this:

server {
  listen 80 default_server;
  server_name your-site.com;
  ...

  include /data/wordpress/nginx/*.conf;

  ...
}

How to create your own rules

The file /data/wordpress/nginx/examples.conf includes a few examples to start with, disabled by comments. Do not edit it directly but leave it as a reference and create your own custom.conf file.

$ cd /data/wordpress/nginx
$ nano custom.conf

Put your own rules into custom.conf. You can copy stuff from examples.conf, remove the comments from the beginning of the line and edit the rules to match your site’s needs.

Restarting Nginx

After making changes to your nginx configuration, please reload the configuration by running

wp-restart-nginx

If there are any errors wp-restart-nginx will warn you about them and refuse to restart before the issue is fixed.

Examples

Forcing HTTPS

To force all your users to connect via https, you may redirect them to the https side via some Nginx configuration.

The preferred method to force HTTPS is via a custom variable.

# Force redirect http -> https
set $force_https 1;

Redirects

Basic redirects

Redirects from http://your-site.com/original -> https://example.com/new/url/

rewrite ^/original(.*)$ https://example.com/new/url$1 permanent;

Rewrite all old *.html files to WordPress pages with pretty URLS:

rewrite ^/([0-9a-z-]+)\.html /$1/ permanent;

Please use something like Rexexpal to test your regular expressions and please use curl -IL to test your redirects without regular browser cache interfering.

Redirecting domains

If you have multiple domains for your site and want to only use one of them:

if ($host ~ "old-subdomain.your-old-site.com") {
  return 301 https://your-site.com$request_uri;
}

Server two different sites from the same domains

Sometimes it can make sense to have two separate sites served from the same domain, as it will contribute to a better user experience and better search engine ranking for the sites content, compared to having the site split to separate domains or subdomains. For example most of a site example.com could be WordPress, but the section example.com/store could be Magento.

This can be achieved using Nginx proxying. Create for example a file names /data/wordpress/nginx/store-proxy.conf with the contents:

location /store/ {
  proxy_ssl_server_name on;
  proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  proxy_pass https://store.example.com/;
}

The PageSpeed module

PageSpeed sniffs your html documents and rewrites them to be faster. This process is very site specific, so test everything carefully before enabling PageSpeed in production. You can find all filters in Google documentation.

Turn pagespeed on

pagespeed on;

Add some PageSpeed filters

# This does a lot so test which of these you really need
pagespeed EnableFilters rewrite_css,sprite_images,combine_css,inline_css,flatten_css_imports,inline_javascript,combine_javascript,inline_google_font_css,canonicalize_javascript_libraries,rewrite_images,recompress_images;