Nginx as reverse proxy cache for wordpress and apache

1. Install Nginx

* Install from PPAPPA Installation
* Install using self build packagesInstallation mit selbstgebauten Paket

2. General Nginx Settings

Edit „/etc/nginx/nginx.conf“ and modify or insert the following settings:

[...]
http {
  [...]
  # Gzip Settings
  gzip on;
  gzip_disable "msie6";
  gzip_buffers 32 8k;
  gzip_comp_level   6;
  gzip_http_version 1.0;
  gzip_min_length   0;
  gzip_types        text/html text/css image/x-icon application/x-javascript application/javascript text/javascript application/atom+xml application/xml ;

  # Proxy Settings
  proxy_temp_path /var/lib/nginx/proxy;
  proxy_connect_timeout 30;
  proxy_read_timeout 120;
  proxy_send_timeout 120;
  proxy_cache_key "$scheme://$host$request_uri";
  [...]
}
[...]

3. Create Site

Create „/etc/nginx/sites-available/www.mogilowski.net“ with following content:

Note: Replace „www.mogilowski.net“ with your site and replace „mogfilecache“ with a unique name for the filecache for this site.

proxy_cache_path  /var/lib/nginx/cache  levels=1:2   keys_zone=mogfilecache:180m  max_size=500m;

server {
        proxy_cache_valid 200 3h;

        listen 80;

        server_name www.mogilowski.net;

        access_log /var/log/nginx/mogilowski.proxied.log;
        error_log /var/log/nginx/mogilowski.proxied.log crit;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        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 mogfilecache;
            proxy_pass http://127.0.0.1:81;
        }

        location ~* wp\-.*\.php|wp\-admin {
            proxy_pass http://127.0.0.1:81;
        }

	location ~* \.(jpg|png|gif|jpeg|css|js|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx)$ {
            proxy_cache_valid 200 6h;
            expires max;
            proxy_cache mogfilecache;
            proxy_pass http://127.0.0.1:81;
        }

        location ~* \/[^\/]+\/(feed|\.xml)\/? {
            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 1h;
            proxy_cache mogfilecache;
            proxy_pass http://127.0.0.1:81;
        }

        # No access to .htaccess files.
        location ~ /\.ht {
            deny  all;
        }

}

Note: In this example configuration the apache webserver runs on port 81 on the localhost. But it can also be a different machine on the internet. Just use „http://YOUR_APACHE_SERVER:80“ instead.

4. Enable site

ln -s /etc/nginx/sites-available/www.mogilowski.net /etc/nginx/sites-enabled/www.mogilowski.net

Note: Please remove the default site from sites-enabled.

5. Purge Cache (optional)

If you have build your own nginx package with the „proxy_cache_purge“ module, you can add this to your server config. (http://labs.frickle.com/nginx_ngx_cache_purge/)

server {
    [...]
    location ~ /purge(/.*) {
                allow                   127.0.0.1;
                deny                    all;
                proxy_cache_purge       mogfilecache   "$scheme://$host$request_uri";
        }
    [...]
}

You can use this together with this plugin: http://wordpress.org/extend/plugins/nginx-proxy-cache-purge

6. Links

* http://wp-performance.com/2010/10/nginx-reverse-proxy-cache-wordpress-apache/
* http://wiki.nginx.org/Wordpress
* http://wordpress.org/extend/plugins/nginx-proxy-cache-integrator/
* http://www.djm.org.uk/wordpress-nginx-reverse-proxy-caching-setup/
* http://zachbrowne.com/2011/how-to-build-the-fastest-wordpress-website-with-apache-ngnix-reverse-proxy-php-cgi-google-pagespeed/

This Post Has 3 Comments

Schreibe einen Kommentar

eMail-Benachrichtigung bei weiteren Kommentaren.
Auch möglich: Abo ohne Kommentar.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.