Split your Nginx config files up, in order to host multiple WordPress domains

If you are hosting multiple domain on your VPS, then you don’t want to have to copy and paste all the configuration lines every time. Instead keep each domain’s config file small and compact. This makes it easy to see what is going on, makes it easier to change a setting across the board (e.g. increasing the cache period), and in my opinion makes it easier to build a script to do the job for you.

To this end, I thought I’d share an example Nginx WordPress configuration, split into 4 parts that allows you to easily add another domain.

1. The main Nginx server config file (nginx.conf)

user www-data;
worker_processes 4;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
  worker_connections  1024;
}

http {
  server_names_hash_max_size 512;
  server_names_hash_bucket_size 128;

  index index.php index.html index.htm;

  include       mime.types;
  default_type  application/octet-stream;

  # Configure FastCGI cache
  fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=WORDPRESS:15m inactive=15m;

  access_log /var/log/nginx/access.log main;
  sendfile   on;

  keepalive_timeout  65;

  gzip  on;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  client_max_body_size       25m;
  client_body_buffer_size    256k;

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}

2. Handling static resources (css, js, images and so on)

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

location ~ /favicon.ico {
  log_not_found off;
  access_log    off;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
  expires       30m;
  access_log    off;
  log_not_found on;
}

location ~ /\.ht {
  deny  all;
}

3. FastCGI handling (and caching)

try_files $uri =404;

set $nocache "";
if ($http_cookie ~ (comment_author_.*|wordpress_logged_in.*|wp-postpass_.*)) {
  set $nocache "Y";
}

fastcgi_pass  unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param CONTENT-LENGTH  $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO       $fastcgi_script_name;
include fastcgi_params;

fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_key       $request_method$host$request_uri;
fastcgi_cache           WORDPRESS;
fastcgi_cache_valid     200 301 302 10m;
fastcgi_cache_valid     404 30m;
fastcgi_ignore_headers  Expires Cache-Control;
fastcgi_cache_bypass    $nocache;
fastcgi_no_cache        $nocache;

4. Example domain

server {
  listen      178.79.142.110:80;
  server_name www.maanas.co maanas.co;

  error_log  /var/log/nginx/maanas/www.maanas.co-error.log;
  access_log /var/log/nginx/maanas/www.maanas.co-access.log;

  root  /home/maanas/sites/www.maanas.co;

  include static-resources.conf;

  location / {
    try_files $uri $uri/ /index.php?$args;
  }

  location ~ \.php$ {
    include wordpress-fastcgi-cache.conf;
  }

  include purge.conf;
}

The 4th config file is small but pertinent. Only the information about the domain itself is in here.

Advertisements

5 thoughts on “Split your Nginx config files up, in order to host multiple WordPress domains

  1. Hi,

    Thanks for posting this. My 14th nginx.config I am trying today is yours, and hoping for the best. Every site seems to copy off each other without bothering to try any of the code themselves. Yours looks more original so I am keeping my fingers crossed.

    Error: nginx: [emerg] unknown log format “main” in /etc/ngnx/nginx.iconf:23 access_log /var/log/nginx/access.log main;

    -(removed) damn , and i had high hopes too. (NOT!)

    nginx: [emerg] open() “/etc/nginx/purge.conf” failed (2: No such file or directory) in /etc/nginx/sites-enabled/default:20 (you had more includes than files to include i guess.)

    -(removed) what’s next?

    service nginx restart
    * Stopping Web Server nginx [ OK ]
    * Starting Web Server nginx [ OK ]

    Well no kidding. You just made the top 5 out of 14 since 9 of them never could even restart nginx.

    So you are in the final round of —nginx.conf game! I got my money on an infinite loop or server crash coming next.

    …w0w! I see a website… Perma links sort of work on root domain multisite, but do far it randomly kicks me off the other network blogs back to the root block. But definately good. At least now I have a point to start climbing, Thank’s a lot

    Nick

    • Can you post your ngnix.conf
      It seems you are loading the ngnix.conf from etc. Have you built ngnix on your own or using some repository.

      • Hi I used auto build script by Vlad on git. Yours did fine until I tried to incorporate Varnish into it. (Got Greedy), and screwed the whole thing up. But what you have up there definately worked with multisite on subdomain. I don’t know enough to compile anything myself since I don’t have time to study any of this and prefer hacking my way around which wastes more time.

      • Hey, Can you write an article for what to compile with nginx for sub domain multisite. Everything I come across is either 2 years old or has a lot of {…} or pseudo code which is completely useless to anyone except people who already know the subject anyway? Thanks for posting this by the way without assuming your readers are all experts.

      • You do not need nginx rebuild. You need to setup WILD CARD DNS with ngnix to work to establish subdomain WordPress Nework.


        server {
        # Replace this port with the right one for your requirements
        listen 80 default_server; #could also be 1.2.3.4:80

        # Multiple hostnames separated by spaces. Replace these as well.
        server_name star.yourdomain.com *.yourdomain.com; # Alternately: _

        root /PATH/TO/WEBROOT;

        error_page 404 errors/404.html;
        access_log logs/star.yourdomain.com.access.log;

        index index.php index.html index.htm;

        # static file 404's aren't logged and expires header is set to maximum age
        location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log off;
        expires max;
        }

        location ~ \.php$ {
        include fastcgi_params;
        fastcgi_intercept_errors on;
        # By all means use a different server for the fcgi processes if you need to
        fastcgi_pass 127.0.0.1:YOURFCGIPORTHERE;
        }

        location ~ /\.ht {
        deny all;
        }
        }

        Ref: http://wiki.nginx.org/ServerBlockExample

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s