Local Nginx Config Which Supports WordPress, Drupal, and Dynamic Domains

By , Chief Executive Officer
January 11th, 2013, 11:34 am

I recently upgraded my laptop to the current top-spec MacBook Air, which has been a big improvement. I took the opportunity to reconfigure my local web server, and came up with a configuration that has already saved me quite a bit of time, since I touch so many projects that come through our company. This configuration would work with just about any PHP application, but it’s specifically tested against WordPress (3.3+), Drupal 6, and Drupal 7. Drupal 7 and WordPress take the same arguments to index.php; Drupal 6 just needs ?q=$uri passed in.

When I want to convert a checked-out PHP app into a local dev server, I just link it to the ~/www folder (ln -s ~/code/alleyinteractive/$PROJECT ~/www/$PROJECT) and add an entry to /etc/hosts (127.0.0.1 $PROJCT.dev)

I don’t have to restart Nginx for this to work because of its slick regular expression matching on hostnames. This config could use a little more tuning; I’d like error logging to be broken out per-host, for instance. I also suspect I could use ‘try_files’ to detect Drupal 6 and only serve ‘?q=$uri’ in that case, since I don’t love the extraneous variable going into WordPress. I’m using MacPorts with php53, fcgi, and nginx, but this would work just as well with any other distribution. And in any case, if you use this, be sure to replace my details with yours (unless your username is also austin).

worker_processes 2;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name ~^(?<sub>.+)\.dev$;
root /Users/austin/www/$sub;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args&q=$uri;
}
location ~ \.php$ {
fastcgi_pass unix:/tmp/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}

view raw
nginx.conf
hosted with ❤ by GitHub