Loading...
Real Time Concepts

Example Nginx Configurations

WordPress

WordPress is incredibly simple and you need to modify very little. These configurations assume you are behind some kind of load balancer and will set the IP properly.

nginx.conf:

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

sendfile on;
keepalive_timeout 15;

gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text
/javascript;

upstream php5-fpm-sock {
server unix:/var/run/php5-fpm.sock;
}
set_real_ip_from 10.23.45.0/24;
real_ip_header X-Forwarded-For;
include /etc/nginx/vhost.d/*.conf;
}

Most importantly is to make sure the PHP upstream is set, and the Real_ip is set.

Vhost:

server {
listen 80;
server_name domain.com;
root /var/www/vhosts/domain.com/content;
access_log /var/log/nginx/domain.access.log;
error_log /var/log/nginx/domain.error.log error;

location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$uri&args;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php5-fpm-sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

#Set Headers on Images and other Static Assets
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 1y;
log_not_found off;
}
}

#This is the block to redirect from www to not wwww.

server {
listen 80;
server_name www.domain.com;
rewrite ^ http://domain.com$request_uri? permanent;
}

This will redirect the domain from www.domain.com->domain.com, as well as set expires on static content and pass PHP requests (securely) to PHP FPM to manage.

WordPress w/ SuperCache

Here’s a sample configuration for customers using WordPress SuperCache.  (http://wordpress.org/extend/plugins/wp-super-cache/)

server {
listen 80;

server_name example.com www.example.com;
root html/example.com;

access_log logs/example.access.log;
error_log logs/example.error.log;

location / {
gzip_static on;
gzip_disable “MSIE [1-6]\.”;

default_type text/html;

if (-f $request_filename) { break; }

set $supercache_file ”;
set $supercache_uri $request_uri;

if ($request_method = POST) { set $supercache_uri ”; }
if ($query_string) { set $supercache_uri ”; }
if ($http_cookie ~* “comment_author_|wordpress|wp-postpass_” ) {
set $supercache_uri ”;
}

if ($supercache_uri ~ ^(.+)$) {
set $supercache_file /wp-content/cache/supercache/$http_host/$1index.html;
}

if (-f $document_root$supercache_file) { rewrite ^ $supercache_file break; }

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

location ~* \.(ico|css|js|gif|jpe?g|png)$ {
expires max;
add_header Pragma public;
add_header Cache-Control “public, must-revalidate, proxy-revalidate”;
}

include php.conf;

location = /favicon.ico { access_log off; log_not_found off; }
location ~ /\. { deny all; access_log off; log_not_found off; }
}

php.cfg

location ~ \.php {
try_files $uri =404;

fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;

fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

# If using a unix socket…
# fastcgi_pass unix:/tmp/php5-fpm.sock;

# If using a TCP connection…
fastcgi_pass 127.0.0.1:9000;
}

Drupal

server {
listen 80;
server_name example.org;
location / {
root /path/to/drupal;
index index.php;
error_page 404 = @drupal;
}
location @drupal {
rewrite ^(.*)$ /index.php?q=$1 last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php5-fpm-sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Magento

server {
root /home/magento/web/;
index index.php;
server_name magento.example.com;
location / {
index index.html index.php;
try_files $uri $uri/ @handler;
expires 30d;
}
location ~ /(app|includes|lib|media/downloadable|pkginfo|report/config.xml|var)/ { internal; }
location /var/export/ { internal; }
location /. { return 404; }
location @handler { rewrite / /index.php; }
location ~* .php/ { rewrite ^(.*.php)/ $1 last; }
location ~* .php$ {
if (!-e $request_filename) { rewrite / /index.php last; }
expires off;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param MAGE_RUN_CODE default;
fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params;
}
}

FYI: This magento config will return a default 404 if there is no default store configured. If you are seeing this, try commenting out this line:

#fastcgi_param MAGE_RUN_CODE default
Leave a Reply

Your email address will not be published. Required fields are marked *