nginx探索(11)使用nginx的fastcgi_cache模块实现后端数据缓存(centos7环境下)

说明:接下来我们用nginx的ngx_cache_purge模块的fastcgi_cache实现动态数据缓存功能。

1.搭建一台nginx服务器,这台服务器做为fastcig_cache缓存服务器和动态数据php服务器ip为192.168.241.205,当客户端访问本地php文件时fastcgi_cache模块在本地保存成缓存文件,当此资源再次被请求时服务器会从本地缓存中读取资源数据返回给客服端浏览器。

2.在nginx服务器上新建两个文件夹保存缓存文件和临时文件(这两个文件夹必须在同一磁盘分区)

mkdir -p /data0/fastcgi_cache_path
mkdir -p /data0/fastcgi_temp_path
3.下载nginx源码文件、pcre源码包、ngx_cache_purge缓存模块源码包,编译并安装

yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel

wget https://ftp.pcre.org/pub/pcre/pcre-8.45.tar.gz

wget https://nginx.org/download/nginx-1.17.1.tar.gz

wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz

安装pcre包

ulimit -SHn 65535
tar zxvf pcre-8.45.tar.gz

cd pcre-8.45/

./configure
 make
 make install

解压ngx_cache_purge模块源码包到/home/ngx_cache_purge

tar zxvf ngx_cache_purge-2.3.tar.gz

安装并编译nginx

tar zxvf nginx-1.17.1.tar.gz

cd nginx-1.17.1/

./configure --user=root --add-module=/home/ngx_cache_purge-2.3 --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --with-pcre-jit --with-http_ssl_module --with-http_v2_module --with-http_sub_module --with-stream --with-stream_ssl_module

make && make install

注意安装编译nginx时加入了--add-module=/home/ngx_cache_purge-2.3

安装完后开始配置nginx的nginx.conf,配置如下:

#user  nobody;
worker_processes  1;

error_log  /home/logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        /run/nginx.pid;


events {
    use epoll;
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #log_format testlog '$host - $uri';
    #access_log  logs/access.log  main;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    sendfile        on;
    #tcp_nopush     on;
    tcp_nodelay on;
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #proxy_temp_path /data0/proxy_temp_path;
    fastcgi_temp_path /data0/fastcgi_temp_path;
    fastcgi_cache_path /data0/fastcgi_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=10g;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        access_log /home/logs/serverphp.access.log combined;
        location ~ /purge(/.*) {
         allow 192.168.241.205;
         allow 192.168.241.0/24;

         deny all;
         fastcgi_cache_purge cache_one 127.0.0.1:9000;
         #access_log /home/logs/cache.log testlog;
        }

        location /
        {
         index index.php index.html;
        }


        location ~ .*\.(php|php5)$
        {
          add_header X-Cache $upstream_cache_status;
          fastcgi_cache cache_one;
          fastcgi_cache_valid 200 1m;
          fastcgi_cache_valid 301 302 1m;
          fastcgi_cache_valid any 1m;

          fastcgi_cache_key 127.0.0.1:9000;
          root /home/server205;
          fastcgi_pass 127.0.0.1:9000;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
        }


        access_log off;
  }

}
配置完之后安装php环境,具体安装步骤详见https://blog.csdn.net/u014415344/article/details/117820458

测试:第一次访问,没有缓存

第二次访问有缓存了:

清除缓存:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值