Nginx web application configurations

Overview

POSSIBLE OUTDATED INFORMATION!
DreamHost does not directly support any of the features described on this page and is not responsible for keeping this content updated or accurate. Use at your own risk!

Many web applications such as WordPress and MediaWiki are originally configured to work with Apache. Since .htaccess files are not supported with Nginx, there are some adjustments you can make to get your applications running properly. In all the examples below, make sure to follow these steps.

In the following examples, username would be your Shell user and example.com your website.

Configuration steps

  1. View the Local configuration files article to create your local /nginx/example.com directory.
  2. Create your .conf file in this /nginx/example.com directory.

    The .conf file can be named anything. It just needs to end in .conf.

  3. Add content to the .conf file.
  4. Reload Nginx by navigating to the VPS or Dedicated page.
    reload http button
  5. Click the Reload HTTP button.
  6. Wait 5 minutes for it to rebuild the configuration file.

You could also reload Nginx via SSH.

A list of examples for various web applications can be found here:

WordPress

Create a wordpress.conf file in your /home/username/nginx/example.com/ directory.

This is where you'll put all your Nginx config rules.

If you are going to share this WordPress configuration across multiple sites, create a default file. Then, create symlinks for each of your sites. For example:

  1. Make a default directory for your WordPress file:
    [server]$ mkdir -p /home/username/nginx/default-wp/
    
  2. In this /default-wp directory, create wordpress.conf file. View the 'Creating and editing a file via SSH' article for instructions on creating the file.
    [server]$ nano /home/username/nginx/default-wp/wordpress.conf
  3. Save and close the file to return to your shell.
  4. Create symlinks from this directory to each of your sites' config directories:
    [server]$ ln -s /home/username/nginx/default-wp/ /home/username/nginx/example.com
    [server]$ ln -s /home/username/nginx/default-wp/ /home/username/nginx/example2.com
    

Permalinks

The most basic set of WordPress rules that get you up and running immediately are these:

#######################
# Permalinks

if (!-e $request_filename) {
  rewrite ^.*$ /index.php last;
}

This gets all basic WordPress functionality going in terms of permalinks.

An alternate approach, which does not use 'if', is as follows:

#######################
# Permalinks

try_files $uri $uri/ /index.php?$args;

Multisite

If you're using Multisite, you need slightly more advanced rules:

######################
# Permalinks

if (!-e $request_filename) {
 rewrite ^/files(.*) /wp-includes/ms-files.php?file=$1 last;

 rewrite ^(/[^/]+)?(/wp-.*) $2 last;
 rewrite ^(/[^/]+)?(/.*.php) $2 last;

 rewrite ^.*$ /index.php last;
}

This provides basic functionality for Multisite.

W3 Total Cache

The W3 Total Cache plugin for WordPress automatically recognizes that it's running under Nginx and creates a default Nginx configuration file which you can copy into your /nginx/<domain name>/ directory.

WP Super Cache

If you're using the WP Super Cache plugin, then use the following in place of the permalink rules above:

#######################
# WP Super Cache

# if the requested file exists, return it immediately
if (-f $request_filename) {
  break;
}

set $supercache_file '';
set $supercache_uri $request_uri;

if ($request_method = POST) {
  set $supercache_uri '';
}

# Using pretty permalinks, so bypass the cache for any query string
if ($query_string) {
  set $supercache_uri '';
}

if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
  set $supercache_uri '';
}

# if we haven't bypassed the cache, specify our supercache file
if ($supercache_uri ~ ^(.+)$) {
  set $supercache_file /wp-content/cache/supercache/$http_host$1/index.html;
}

# only rewrite to the supercache file if it actually exists
if (-f $document_root$supercache_file) {
  rewrite ^(.*)$ $supercache_file break;
}

# all other requests go to WordPress
if (!-e $request_filename) {
  rewrite ^.*$ /index.php last;
}

Nginx search functionality

Unfortunately, this configuration currently causes site searches that contain spaces to break. With Apache, when you search for "some thing" that gets converted to "search/node/some+thing". When this is processed by nginx the "+" gets encoded so you end up with "search/node/some%20thing". Thus, your search ends up looking for the string "some+thing" rather than "some thing". There are currently no known workarounds that were forthcoming.

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?