IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    WordPress中使用Nginx的fastcgi_cache

    酷特尔发表于 2015-12-20 10:27:01
    love 0

    纯分享,Wordpress中有很多缓存类的插件,博主这边已经很久没使用过任何缓存插件了。前段时间更新了系统版本,以及wordpress版本也已经更新到最新的4.4上面。每次更新都是个蛋碎的事情,一定要修改一些代码才觉得自在的强迫症。最近折腾博客的频率又大了些,下面就分享下Nginx中如何利用fastcgi_cache缓存模块来缓存wordpress博客。

    废话就不多说了,什么编译nginx,配置nginx虚拟主机的这边就不多说哈。具体来看下以iloxp.com为示例的配置吧。下面是nginx.conf配置中的 http{…} block部分配置:

    http {
     include mime.types;
     default_type application/octet-stream;
     sendfile on;
     tcp_nopush on;
     keepalive_timeout 65;
     gzip on;
     gzip_min_length 1k;
     gzip_buffers 4 16k;
     gzip_http_version 1.0;
     gzip_comp_level 2;
     gzip_types text/plain application/x-javascript text/css application/xml;
     gzip_vary on;
     fastcgi_connect_timeout 300;
     fastcgi_send_timeout 300;
     fastcgi_read_timeout 300;
     fastcgi_buffer_size 32k;
     fastcgi_buffers 4 32k;
     fastcgi_busy_buffers_size 32k;
     fastcgi_temp_file_write_size 32k;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_connect_timeout 5;
     proxy_read_timeout 60;
     proxy_send_timeout 5;
     proxy_buffer_size 16k;
     proxy_buffers 4 64k;
     proxy_busy_buffers_size 128k;
     proxy_temp_file_write_size 128k;
     server_names_hash_bucket_size 64;
     fastcgi_cache_path /dev/shm/nginx-cache levels=1:2 keys_zone=iloxp:30m inactive=20m;
     fastcgi_cache_key "$scheme$request_method$host$request_uri";
     fastcgi_cache_use_stale error timeout invalid_header http_500;
    }

    首先,上面红色字体中,配置一个fastcgi_cache的缓存zone,用于存储缓存文件,这里我直接放到内存中,即Linux系统下的/dev/shm/nginx-cache 这个目录下。

    然后配置虚拟主机的 server{…} block。见下面相关部分配置:

    server {
    ...
    # fastcgi_cache 缓存开始,设置值为 0
    set $no_cache 0;
    # 对于有post或者query的请求则跳过缓存,设置值为 1
    if ($request_method = POST) {
     set $no_cache 1;
    } 
    if ($query_string != "") {
     set $no_cache 1;
    } 
    # 配置部分url的请求跳过缓存,设置值为 1
    if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
     set $no_cache 1;
    } 
    # 配置登录用户和填写了评论者完整信息的用户跳过缓存,设置值为 1
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
     set $no_cache 1;
    } 
    location ~ \.php$ {
     root /data/www/iloxp;
     fastcgi_pass unix:/dev/shm/php5.sock;
     fastcgi_index index.php;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     include fastcgi_params;
     fastcgi_cache_bypass $no_cache;
     fastcgi_no_cache $no_cache;
     fastcgi_cache iloxp;#对应http{...}block中的zone
     fastcgi_cache_valid 200 10m;#缓存http请求状态码为200的内容
     }
    ...
    }

    大概就是这样了,测试的效果还是挺不错,fastcgi_cache_valid 200 10m,这里可以设置为缓存时间更长些,例如:fastcgi_cache_valid 200 60m;60分钟。对于缓存的清楚配置,这里就没有做了。因为缓存的时间短,所以,也没配置cache_purge。

     



沪ICP备19023445号-2号
友情链接