基于流量来源的 Nginx 规则?

基于流量来源的 Nginx 规则?

我正在使用带有 WP Super Cache 的 wordpress,这是我的 nginx 规则:

server {
    server_name  website.com;
    location / {
        root   /var/www/html/website.com;
        index  index.php;
           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;
    }
}

但是如果访问者来自某些网站,我需要取消缓存。(例如:来自 google.com 和 yahoo.com)

答案1

nginx 模块可能会做你想做的事。

答案2

你可以试试

server {
    server_name  website.com;
    location / {
        root   /var/www/html/website.com;
        index  index.php;
           if ($http_referer ~* (|google.com|google.co|yahoo.com|) ) {
                   rewrite . /index.php last;
           }
           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;
    }
}

相关内容