Nginx实战-缓存服务(3)fastcgi_cache

2018-12-24 00:38:09 george518 ...

什么是fastcgi

说起fastcgi不得不提cgi,cgi全称是“通用网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序一般运行在网络服务器上。 CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量。如php,perl,tcl等。摘自百度百科。

那fastcgi意思就是比cgi执行速度要快速。主要快在从启动开始,就一直会有一个常驻的进程一直等待外部的请求,不用每次有请求就fork一个进程处理请求。当然,速度快了,占用的内存就会高了,因为不论有没有客人,总要有人在门口迎宾。

PHP服务使用fastcgi_cache设置缓存

#设定一个负载均衡
upstream slbserver_cache {
         server  127.0.0.1:9000 weight=1 max_fails=2  fail_timeout=2;
}

#设定缓存地址,缓存比例以及缓存名称,缓存大小,缓存时间
fastcgi_cache_path /webroot/blog_cache/ levels=1:2 keys_zone=pp_blog:10m max_size=1g inactive=60m use_temp_path=off;
server {
        listen       80;
        server_name  localhost;
    index index.html index.htm index.php;
    root /webroot/www;
    location ^/(static|Uploads) {
        expires 30d;
    }

    #/admin 相关请求不进行缓存
    if ($request_uri ~ ^/admin){
        set $nocache 1;
    }

    location ~ .*\.(php|php5)?$
    {
        fastcgi_temp_path /var/cache_temp;
        fastcgi_cache pp_blog;
        fastcgi_cache_key $scheme$request_method$host$request_uri$is_args$args;
        add_header X-Cache-Source $upstream_cache_status;
        fastcgi_pass  127.0.0.1:9000;

        fastcgi_index index.php;
        #200请求缓存60分钟
        fastcgi_cache_valid 200 60m;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_ignore_headers  Cache-Control Expires Set-Cookie;
        #设定不缓存的内容
        fastcgi_no_cache $nocache $arg_nocache $arg_comment;
        include fastcgi_params;
    }
}

相似文章