3

Nginx web application configurations – DreamHost Knowledge Base

 3 years ago
source link: https://help.dreamhost.com/hc/en-us/articles/217477738-Nginx-web-application-configurations
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

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/username/nginx/example.com/

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

[server]$ nano /home/username/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 (e.g., https://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 (e.g., https://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 https://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/username/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/username/example.com/local;
        autoindex on;
    }
### Dreamhost analog stats
if ($request_uri ~* ^/(stats|failed_auth\.html).*$)
{
	break;
}

Increase FastCGI Timeout

Put this in a .conf file as described above:

fastcgi_read_timeout 120;

Sending Mass Email Settings

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK