Nginx web application configurations

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:

  1. View the Local configuration files section 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. Make sure to reload Nginx for the changes to take effect.

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/YOURUSER/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;
}

FeedBurner

If you have your blog's RSS feed setup using FeedBurner, then add the following lines above the permalink or supercache lines mentioned previously:

#######################
# FeedBurner

if ($http_user_agent !~ FeedBurner) {
  rewrite ^/comment/feed/ http://feeds.feedburner.com/your-comment-feed last;
  rewrite ^/feed/ http://feeds.feedburner.com/your-feed last;
}

You must replace the "your-comment-feed" and "your-feed" bits with the appropriate FeedBurner URLs you get when you add your feeds to its system.

Drupal

Before you set up Drupal, create a drupal.conf file (the name doesn't really matter, but generally you want to at least give it a .conf extension) in the following location:

/home/YOURUSER/nginx/example.com/

View the 'Creating and editing a file via SSH' article for instructions on creating the file.

[server]$ nano /home/YOURUSER/nginx/example.com/drupal.conf

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

Top Level Domain Install

If your Drupal is installed at the top level of your domain (i.e., http://www.example.com/ is your Drupal install), then add these rules:

#######################
# Drupal Clean URLs (Top Level Domain)

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

Subdirectory Install

If your Drupal is installed in a subdirectory (i.e., http://www.example.com/drupal/ is your Drupal install), then add these rules:

#######################
# Drupal Clean URLs (Subdirectory)

if (!-e $request_filename) {
rewrite ^/drupal$ /drupal/ permanent;
rewrite ^/drupal/(.*)$ /drupal/index.php?q=$1 last;
rewrite ^/(.*)$ /index.php?q=$1 last;
}

Replace occurrences of "drupal" in the above with whatever your subdirectory is named.

Enable Clean URLs

If you haven't already, you must log into your Drupal admin user and go to the Administer -> Site Configuration -> Clean URLs area and turn Clean URLs on. If you have trouble finding it, then try browsing to http://www.example.com/index.php?q=admin/settings/clean-urls. If you get an error message saying Clean URLs aren't supported, make sure you remembered to reload your nginx configuration as discussed earlier in the article.

Gotchas

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.

MediaWiki

MediaWiki is a popular wiki software developed by the founders of Wikipedia which continues to use and maintain the software today. It is available for free and is an open-source project. The following describes a successful move from MediaWiki 1.17.0 site (using Clean URLs) on Apache to Nginx. First, create an mediawiki.conf file in the appropriate directory:

/home/YOURUSER/nginx/example.com/mediawiki.conf

In that file add the following contents:

location / {
        try_files $uri $uri/ /index.php;
    }
	
location /cache {
        deny  all;
    }
location ~* ^.+\.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
        access_log off;
    }
location /dumps {
        root /home/YOURUSER/example.com/local;
        autoindex on;
    }
### Dreamhost analog stats
if ($request_uri ~* ^/(stats|failed_auth\.html).*$)
{
	break;
}

phpBB

Increase FastCGI Timeout

Put this in a .conf file as described above:

fastcgi_read_timeout 120;

Source: https://www.phpbb.com/community/viewtopic.php?f=46&t=1778165#p10746575

Sending Mass E-Mail Settings

If you want to send mass emails with SMTP authentication, you must use POP-BEFORE-SMTP as the "Authentication method for SMTP".

Did this article answer your questions?

Article last updated .