1

我正在寻找一种在网络服务器上长期(几天/几周)缓存 404 的解决方案。我当前的设置是 NGINX,带有 memcached_pa​​ss 代理和 PHP-FPM 来提供未缓存的页面(PHP 也将内容写入 memcached)。网络上的爬虫似乎很喜欢我的页面,每天会产生几千个 404 请求。所有这些都直接命中 PHP,因为我无法将 404 响应标头信息与 memcached 中的内容一起缓存,因此 memcached_pa​​ss 查找总是失败。

如何缓存所有返回 404 的请求?Nginx 的 HTTPProxModule 是我正在寻找的吗?还是我应该选择清漆?

从我目前的角度来看,我并不热衷于更改我的整个设置并从 nginx 中删除 memcached_pa​​ss 指令。到目前为止它非常简洁,因为 php 决定是否可以(应该)将请求缓存在 memcached 中。必要时刷新缓存也很容易。

我当前的 NGINX 配置文件:

server {
    listen       80;
            server_name  _;


            gzip  on;
            gzip_http_version 1.0;
            gzip_vary on;
            gzip_comp_level 6;
            gzip_proxied any;
            gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    location / {
                    gzip  on;
        default_type  "text/html; charset=utf-8";
                    charset         utf-8;
                    add_header  Content-Encoding    gzip;

         if ($request_method = GET)
        {
                expires      max;
                set $memcached_key $http_host$request_uri;
                memcached_pass     127.0.0.1:11211;
                error_page         404 = @fallback;
                #error_page 502 = @fallback;
                break;
        }

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

        if (!-e $request_filename) {
            rewrite  ^/(.*)$  /index.php?q=$1  last;
            break;
        }

    }



    location @fallback {
                    internal;
        root   /var/www/html/;
        index  index.php index.html;

        if (!-e $request_filename) {
            rewrite  ^/(.*)$  /index.php?q=$1  last;
            break;
        }


    }

    location ~ \.php$ {
        root   /var/www/html/;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
                 include /etc/nginx/fastcgi_params;
    }


}

Nginx 或 Varnish 的示例配置会很棒。

谢谢!:)

4

1 回答 1

0

使用 fastcgi_cache_valid 指令:http ://wiki.nginx.org/HttpFcgiModule#fastcgi_cache_valid

于 2011-10-14T12:41:03.093 回答