67

Nginx配置fastcgi cache

 5 years ago
source link: https://www.tlanyan.me/nginx-config-fastcgi-cache/?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

转载请注明文章出处: https://tlanyan.me/nginx-config-fastcgi-cache/

在尝试Ghost中提到过托管博客的VPS由于内存不足导致服务宕机。今天意识到可以通过Nginx缓存避免请求落到PHP和MySQL端,提升整体性能。此外博客发布后很少改动,读多写少,是使用缓存(或静态化)的好场景。

缓存机制

Nginx提供了基于代理(proxy)和基于fastcgi的两种缓存方式,两者指令集十分相似,使用上差别不大。由于本站博客采用Nginx+PHP-FPM架构,故而使用基于fastcgi的缓存,即fastcgi cache。

配置

fastcgi cache的机制是将后端CGI服务返回的页面缓存起来,后续请求到来时直接返回页面,省去与后端服务通信及生成页面的消耗。fastcgi cache与squid、varnish、CDN等原理类似,由于减少了后端请求,原则上性能会比WordPress的各种缓存插件高出许多。

fastcgi cache隶属于fastcgi模块,而fastcgi模块又归属于http模块,所以与缓存相关的配置应当都出现在这两个及下属配置块中(主要是http, server和location)。

根据官方文档,博客站点采用的缓存配置如下:

http {
  ...
  fastcgi_connect_timeout 30s;
  # 全局设置, fastcgi_cache_path只能出现在http配置块中
  # fastcgi_cache_path设置全局的缓存路径、文件大小等,可多次出现设置不同的缓存池
  fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=WORDPRESS:10m inactive=10m max_size=500m;
  fastcgi_cache_key $scheme$request_method$host$request_uri;
  # 设置使用过期缓存的情形:后端错误、超时等
  fastcgi_cache_use_stale error timeout invalid_header http_500 http_503;
  ...
  server {
    ...
    # 缓存策略指示变量
    set $skip_cache 0;
    # 缓存策略
    if ($query_string != "") {
      set $skip_cache 1;
    }
    if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|sitemap(_index)?.xml") {
      set $skip_cache 1;
    }
    # 登录用户或发表评论者
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
      set $skip_cache 1;
    }
    location ~ \.php$ {
      ...
      # 使用的keyzone,这里使用http中定义的WORDPRESS
      fastcgi_cache WORDPRESS;
      # 是否跳过缓存
      fastcgi_cache_bypass $skip_cache;
      fastcgi_no_cache $skip_cache;
      # 后端返回成功的情况下缓存10分钟
      fastcgi_cache_valid 200 10m;
      # 添加header字段,指示缓存命中状态(调试使用)
      add_header X-Cache $upstream_cache_status;
    }
  }
}
 

上述配置做了以下工作:

  1. 设置缓存池信息(fastcgi_cache_path);
  2. 设置缓存key(fastcgi_cache_key);
  3. 过期文件处理(fastcgi_cache_use_stale)
  4. 缓存策略(fastcgi_cache_bypass/fastcgi_no_cache)
  5. 后端请求成功时缓存时间(fastcgi_cache_valid)
  6. 增加了调试信息头(add_header)

重启Nginx,然后访问页面,查看页面的header状态和响应时间。可以看到,请求一次过后(次数可通过 fastcgi_cache_min_uses 设置),后续请求都从缓存请求(X-Cache值为HIT),速度提升非常明显。对于管理后台页面, x-cache 的值为 BYPASS ,正是我们需要的。

其他

开源版的Nginx不能自动清除缓存,当文章变动或者有新评论时不能立即更新页面。要达到此目的,可使用商业版Nginx或者通过开源插件解决,详情查看 此文

参考

  1. http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html
  2. https://www.mf8.biz/wordpress-fastcgi-cache/

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK