wp的效率一直让我头疼,试着用nginx的fastcgi_cache模块来加速,理论上效果应该是不错的。
首先创建缓存目录:
mkdir /cache_fastcgi
然后在nginx主配置的http段加入缓存区的设置:
fastcgi_cache_path /cache_fastcgi levels=1:2 keys_zone=wp_fastcgi:10m inactive=2h max_size=1g; fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_valid 200 302 1h; fastcgi_cache_min_uses 1; fastcgi_cache_use_stale error timeout invalid_header http_500; fastcgi_ignore_headers Cache-Control Expires Set-Cookie; fastcgi_temp_path /tmp/fcgi_cache_tmp;
keys_zone=wp_fastcgi:10m 是给他取名wp_fastcgi,使用内存上限为10m
inactive=2h 是默认的过期时间
max_size=1g 是最大使用的磁盘容量,因为这个缓存只针对fastcgi,在这里也就是php的动态页面,所以具体内容不会太多,只要设置的别太小就行
fastcgi_cache_min_uses 1; 是访问多少次就进行缓存,设为1就是只要访问过这个地址则直接缓存
站点配置:
#######################################################
# www.tingtao.org
server {
listen 80;
server_name www.tingtao.org;
keepalive_timeout 120;
listen 443 ssl;
ssl_certificate /var/www/ca/www.tingtao.org/Nginx/1_www.tingtao.org_bundle.crt;
ssl_certificate_key /var/www/ca/www.tingtao.org/Nginx/2_www.tingtao.org.key;
##############################################
error_log /dev/null;
access_log /dev/null;
root /var/www/www.tingtao.org;
set $skip_cache 0;
#post访问不缓存
if ($request_method = POST) {
set $skip_cache 1;
}
#动态查询不缓存
if ($query_string != "") {
set $skip_cache 1;
}
#后台等特定页面不缓存
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|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 {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php7-fpm-www.tingtao.org.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/www.tingtao.org$fastcgi_script_name;
include fastcgi_params;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param PHP_ADMIN_VALUE "cgi.fix_pathinfo=1";
fastcgi_param PHP_ADMIN_VALUE "include_path= .:/usr/share/php/";
fastcgi_param PHP_ADMIN_VALUE "open_basedir= $document_root/:/tmp:/usr/share/php/";
fastcgi_param PHP_ADMIN_VALUE "upload_max_filesize= 50M";
fastcgi_param PHP_ADMIN_VALUE "max_execution_time= 30";
fastcgi_param PHP_ADMIN_VALUE "max_input_time= 60";
fastcgi_param PHP_ADMIN_VALUE "memory_limit= 128M";
fastcgi_param PHP_ADMIN_VALUE "output_buffering= 4096";
fastcgi_param PHP_ADMIN_VALUE "disable_functions= system,exec,shell_exec,passthru,error_log,dl,sys_getloadavg,pfsockopen,openlog,syslog,readlink,symlink,link,leak,popen,escapeshellcmd,proc_close,proc_get_status,proc_nice,proc_open,proc_terminate,escapeshellarg,pcntl_exec,show_source,highlight_file,ini_restore,apache_child_terminate,apache_get_modules,apache_get_version,apache_getenv,apache_note,apache_setenv,virtual,mb_send_mail,set_time_limit,max_execution_time,php_uname,disk_free_space,diskfreespace,stream_copy_to_stream";
fastcgi_param PHP_ADMIN_VALUE "allow_url_fopen= off";
fastcgi_param PHP_ADMIN_VALUE "expose_php= Off";
fastcgi_param PHP_ADMIN_VALUE "display_errors= Off";
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort on;
fastcgi_read_timeout 180;
add_header Fastcgi-Cache $upstream_cache_status;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache wp_fastcgi;
fastcgi_cache_valid 200 301 302 2h;
}
location ~ /purge(/.*) {
fastcgi_cache_purge wp_fastcgi "$scheme$request_method$host$1";
}
location / {
#定义首页索引文件的名称
index index.php index.html index.htm;
#下面这行和后面的跟wordpress有关
try_files $uri $uri/ /index.php?$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
}
与我以往的配置相比只增加了几行
add_header Fastcgi-Cache $upstream_cache_status; 是增加一个http头来观察缓存状态的,具体到每一个页面的url
其他就是增加了一些不需要缓存的规则,对wp来说,直接复制即可
fastcgi_cache_valid这个,大概所有的fastcgi_cache相关设置的有效作用域是http/server/location,我没严格验证,但按照常规来说是会有覆盖行为的,所以可以每个站点进行细节设置
关于proxy和fastcgi_cache的选择及组合:
cache是针对具体的php请求的,而proxy是一切,所以可以灵活的组合运用。
如果是单机环境,那么用cache是比较合理的,因为静态文件不论是否过proxy都要从硬盘读取,所以显得多此一举。
如果是前后端模式,那么前端用proxy后端用cache是比较合理的,理论上负载能力和性能最大化。如果一定要无敌性能,那么再和varnish组合一下,php页面走cache,其他文件全走内存,前端再做点小改动把部分https流量也引导至varnish的http,绝对666
