Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm using WordPress with WP Super Cache. I want visitors who come from Google (That inlcudes all country specific referrers like google.co.in, google.co.uk and etc.) to see uncached contents.

There are my nginx rules which are not working the way I want:

server {
    server_name  website.com;
    location / {
        root   /var/www/html/website.com;
        index  index.php;
           if ($http_referer ~* (www.google.com|www.google.co) ) {
                   rewrite . /index.php break;
           }
           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;
           }
    }
    location ~ \.php$ {
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index   index.php;
            fastcgi_param   SCRIPT_FILENAME /var/www/html/website.com$fastcgi_script_name;
            include         fastcgi_params;
    }
}

What should I do to achieve my goal?

share|improve this question

3 Answers 3

this may works for $http_referer:

       if ($http_referer ~* (www.google.com|www.google.co) ) {
               break;
       }
       if (!-e $request_filename) {
               rewrite . /index.php break;
       }
share|improve this answer
    
this also not works –  juana Sep 2 '10 at 13:59

I'm not familiar with WP Supercache, but if you just need to rewrite to index.php to avoid the cache it shouldn't be too difficult.

Your existing filter is not comprehensive since it only checks for google.com and google.co. According to this list, there are many TLDs that Google uses that won't match, such as google.de, google.fr, etc.

The following filter should limit you to referrers that begin with www.google and end with any combination of 2-3 character TLD.

if ($http_referer ~* ^www.google.[a-z]{2,3}(.[a-z]{2})?$ ) {
    # do whatever you need to do here to avoid caching
}
share|improve this answer

You're almost there.

First, the WP Super Cache rules are very messy. They really need to be redesigned from the ground up, but that's a project for another day.

To get this working, don't return immediately, instead set the $supercache_uri = '' as all of the other checks do. For instance:

if ($http_referer ~* (www.google.com|www.google.co) ) {
    set $supercache_uri '';
}

This needs to appear AFTER the point where $supercache_uri is originally set, and not at the beginning where you have it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.