Jump to content

Nginx reverse proxy cache to work with processwire


adrianmak
 Share

Recommended Posts

Story:

https://processwire.com/talk/topic/9250-what-should-i-do-to-make-pw-to-work-with-a-reverse-proxy-cache/

After a few days of reading documentation, I got processwire to work with nginx reverse proxy cache.
This is not a tutorial on how to install server software. A working nginx config copy will be posted on next reply.
 

Ubuntu 14.04.1 x64

Nginx 1.4.6

apache 2.4.7

mysql 5.6.19

php 5.6.6 (php5-fpm)

A new copy of content will be fetched instantly.

A screencast of this demo

  • Like 2
Link to comment
Share on other sites

nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        include /etc/nginx/cache.conf;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        gzip on;
        gzip_disable "msie6";

        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 2;

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

cache.conf

proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=one:100m
           inactive=60m
           loader_threshold=300
           loader_files=200
           max_size=200m;
proxy_cache_key "$scheme$proxy_host$uri$is_args$args";

/etc/nginx/sites-available/default

server {
        listen 80 default_server;

        proxy_cache one;
        add_header X-Cache-Status $upstream_cache_status;
        proxy_cache_valid any 1m;
        proxy_cache_min_uses 3;
        proxy_ignore_headers Set-Cookie;
        proxy_ignore_headers "Cache-Control" "Expires";

        root /var/www/html;
        index index.php index.html index.htm;

        server_name dev.local.net;

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

        location ~ \.php$ {
                proxy_pass http://127.0.0.1:8080;
                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_set_header X-Forwarded-Proto $scheme;
        }

        location ~ /\. {
                deny all;
        }
}

A modified basic-page.php of pw installation profile

<?php
$lastModified=($page->last_modified);
$etag = md5($page->body);
$ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_S
INCE'] : false);
$etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH'])
: false);

header("Last-Modified: ".gmdate("D, d M Y H:i:s", $lastModified)." GMT");
header("Etag: $etag");
header('Cache-Control: public');

//check if page has changed. If not, send 304 and exit
if ((@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])===$lastModified) || $etagHeader == $et
ag) {
       header("HTTP/1.1 304 Not Modified");
       exit;
}

//$content = "This is a demo";
// basic-page.php template file

// Primary content is the page's body copy
$content = $page->body;
//$content .= $etag;

// If the page has children, then render navigation to them under the body.
// See the _func.php for the renderNav example function.

if($page->hasChildren) $content .= renderNav($page->children, 0, 'summary');

// if the rootParent (section) page has more than 1 child, then render
// section navigation in the sidebar

if($page->rootParent->hasChildren > 1) {
        $sidebar = renderNav($page->rootParent, 3) . $page->sidebar;
}
  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...