Wednesday, June 12, 2013

WordPress on Apache and Nginx

This is one of good config i found and made working, if you find any idea how to improve this let me know.

Its mainly for WordPress with W3 cache, running on Apache2 and served using Nginx as reverse proxy

with fastcgi for php handling. The reverse cache system is good for handling of static data.

But i think it can be used also for Joomla or Drupal.

Static web would run very nice with this config.


#YourSite is configAlias,


server {

listen 80;

server_name www.sitename.com;

rewrite ^/(.*) http://subdomain.sitename.com/$1 permanent;

}


server {

listen 80;

server_name www.sitename.com *.sitename.com;

access_log off;

error_log off;


# Main site domain


root /var/www/siteroot;

index index.php index.html index.htm;

error_page 404 = @YourSite;

log_not_found off;


#location / {

# try_files $uri $uri/ /index.php?$args;

#}


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


location ~* \.(jpg|png|gif|jpeg|css|js|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx|ico)$ {

# Cache static-looking files for 120 minutes, setting a 10 day expiry time in the HTTP header,

# whether logged in or not (may be too heavy-handed).

proxy_cache_valid 200 120m;

expires 864000;

proxy_pass http://www.sitename.com:8080;

proxy_cache one;

}


location ~* \/[^\/]+\/(feed|\.xml)\/? {

# Cache RSS looking feeds for 45 minutes unless logged in.

if ($http_cookie ~* “comment_author_|wordpress_(?!test_cookie)|wp-postpass_” ) {

set $do_not_cache 1;

}


proxy_cache_key “$scheme://$host$request_uri $do_not_cache”;

proxy_cache_valid 200 45m;

proxy_cache one;

proxy_pass http://www.sitename.com:8080;

}


location = /50x.html {

root /var/www/siteroot;

}


# No access to .htaccess files.

location ~ /\.ht {

deny all;

}


############## WP SUPER CACHE ############


if (-f $request_filename) {

#expires max;

break;

}


if (-d $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;

}

############## WP SUPER CACHE ############


location ~ \.php$ {

fastcgi_pass 127.0.0.1:port;

fastcgi_index index.php;

include /etc/nginx/fastcgi_params;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

}


location @YourSite {

proxy_set_header Host $host;

# Set the real IP.

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_pass http://www.sitename.com:8080;

}


location / {

# If logged in, don’t cache.

if ($http_cookie ~* “comment_author_|wordpress_(?!test_cookie)|wp-postpass_” ) {

set $do_not_cache 1;

}


proxy_cache_key “$scheme://$host$request_uri $do_not_cache”;

proxy_cache one;

proxy_pass http://www.sitename.com:8080;

}


location ~* wp\-.*\.php|wp\-admin {

# Don’t static file cache admin-looking things.

proxy_pass http://www.sitename.com:8080;

}


location ~ \.php$ {

try_files $uri @YourSite;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_pass http://www.sitename.com:8080;

}

}



WordPress on Apache and Nginx

No comments:

Post a Comment