0

I am using php based blog on NGINX server with FastCGI cache. Without this cache, it is easy to update pageviews with every page load for a specific url. When FastCGI cache is in action, pageviews aren't updated.

I am thinking to implement pixel based tracking, so when that page isn't in cache and gets a cache MISS, the request will hit the real page and then I can update pageviews with help of access logs(24 hr) of pixel.

Will this be good or can anyone suggest a better workaround to achieve this?

g13
  • 61
  • 5
  • You would typically have a pixel tracking asset that has a response header such as `Cache-Control: private, no-cache, no-store, max-age=0` and then you should get a hit for every page load. To display the counter, you could set on your page a header such as `Cache-Control: public, max-age=600 s-max-age=600`, or better, use a bit sy Javascript to pull the data using AJAX. – Cameron Kerr May 07 '15 at 14:27
  • Thanks @CameronKerr for reply, but what if javascript is disabled by visitors, those pageviews will get excluded. Is there any nginx module that can do this work ? – g13 May 07 '15 at 14:33
  • It having people disable Javascript is a problem for you (it is just a hit-counter, right?) then you could use an iframe of just wait for the page to expire and get updated. How many people disable Javascript these days, anyway? – Cameron Kerr May 07 '15 at 14:39
  • Mostly those who srape data from site disable javascript. If possible I want to keep my eye on this. – g13 May 07 '15 at 15:04
  • In which case they probably wouldn't be using a browser, but something like curl or some library, and they wouldn't be pulling assets, most likely. If you omit the Cache-Control header, you could still use the other caching-related headers. You'd end up with a much slower site though. Why not just give them a nice way to programmatically get content and account though that? – Cameron Kerr May 07 '15 at 15:11
  • Ok, I got you. So, you mean that I should stick to pixel tracking and show counter throught ajax and there is not any other way to track page views while on caching? – g13 May 07 '15 at 15:24

1 Answers1

1

First of all you need to check if your nginx has cache purge module with nginx -V 2>&1 | grep nginx-cache-purge If yes then add following block to your nginx conf.

location ~ /purge(/.*) {
  fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}

now if you want to update any page, purge its cache by visiting url suppose http://example.com/purge/abcd/abc.php it will purge cache for page http://example.com/abcd/abc.php

Harshad
  • 121
  • 2