1、禁用文章修订版,在 wp-config.php文件中添加:
1 |
<pre class="wrap:true lang:default decode:true "> define('WP_POST_REVISIONS', false); |
再用WP clean up插件清理下sql 2018推荐用wp-sweep
2、禁用自动保存,修改下面连个,注释掉,测试版本4.7.1
1 2 3 |
<pre class="wrap:true lang:default decode:true ">wp-admin/post-new.php 直接注释掉 wp-admin/post.php 注释掉 在if ( 'attachment' !== $post_type );《---加个分号 如://wp_enqueue_script('autosave'); |
插件安装
2017年:(我的VPS配置的是nginx/1.10.2+PHP 7.1.0+memcached 1.4.34 开启fastcgi_cache):
查Nginx是否安装ngx_cache_purge
2019年: 我配置nginx1.17.1+mysql 8+php 7.3.7 用的是腾讯云1G1核1m 三年版 php装了opcache 其它全部没装。wp的插件上么加速也全没装,因需要装了个Jetpack,如果你非要装就装有Database Cache(数据库缓存) Object Cache (对象缓存)这类加速的就行了,内存小我没装memcached。
1 |
<pre class="wrap:true lang:default decode:true ">nginx -V 2>&1 | grep -o ngx_cache_purge |
为了方便我建议到:https://caohuan.com/1-service-in.html 下个安装包,也推荐用oneinstack
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<pre class="wrap:true lang:default decode:true "> cd /root/oneinstack/src wget http://nginx.org/download/nginx-1.17.1.tar.gz(请根据自己需要的版本修改链接地址,下文中的目录也要作相应的修改) tar xzf nginx-1.17.1.tar.gz git clone https://github.com/google/ngx_brotli.git git clone https://github.com/FRiCKLE/ngx_cache_purge/ cd /root/oneinstack/src/ngx_brotli git submodule update -–init cd /root/oneinstack/src/ngx_cache_purge git submodule update -–init cd /root/oneinstack/src/nginx-1.17.1 nginx -V #查看nginx编译参数,最后加上--add-module=../ngx_cache_purge-2.3 |
1 |
<pre class="wrap:true lang:default decode:true ">./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.1.1b --with-pcre=../pcre-8.43 --with-pcre-jit --with-ld-opt=-ljemalloc --add-module=../ngx_brotli --add-module=../ngx_cache_purge |
make #编译
make install #安装
nginx -V 2>&1 | grep -o ngx_cache_purge
nginx -V 2>&1 | grep -o ngx_brotli
# 显示ngx_cache_purge 和 ngx_brotli表示已经安装成功
测试
nginx -t
无错使nginx配置生效
1 |
<pre class="wrap:true lang:default decode:true ">nginx -t reload 或 service nginx reload |
开启cache要在nginx.conf和你的虚拟机server添加,下面这段加在server前面
1 2 3 4 5 6 7 8 9 10 |
<pre class="wrap:true lang:default decode:true "> #例:fastcgi_cache_path /tmp/nginx-cache levels=1:2 keys_zone=WORDPRESS:500m inactive-1d max_size=2g; #设置 web fastcgi_cache_path可设定为内存,levels目录字符数,缓存区名称为 WORDPRESS,内存缓存空间大小为 500MB(小网站内存小可设小),自动清楚超过 1 天没有被访问的缓存数据,硬盘缓存空间大小为 2GB. fastcgi_cache_path /dev/shm/nginx-cache levels=1:2 keys_zone=WORDPRESS:30m inactive=32m max_size=64m; fastcgi_temp_path /dev/shm/nginx-temp; #这目录必须同上保持一致 fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_use_stale error timeout invalid_header http_500; fastcgi_ignore_headers Cache-Control Expires Set-Cookie; fastcgi_cache_min_uses 1;#访问一次生成缓存 |
把brotil配置在gzip的后面吧
1 2 3 4 5 6 7 8 |
<pre class="wrap:true lang:default decode:true "> brotli on; #启用 brotli_comp_level 8; #压缩等级,默认6,最高11,太高的压缩水平可能需要更多的CPU brotli_buffers 16 8k; #请求缓冲区的数量和大小 brotli_min_length 20; #指定压缩数据的最小长度,只有大于或等于最小长度才会对其压缩。这里指定20字节 brotli_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml text/html application/json image/svg application/font-woff application/vnd.ms-fontobject application/vnd.apple.mpegurl image/x-icon image/jpeg image/gif image/png image/bmp; #指定允许进行压缩类型 brotli_static always; #是否允许查找预处理好的、以.br结尾的压缩文件,可选值为on、off、always brotli_window 512k; #窗口值,默认值为512k |
然后在 你的虚拟机部分,添加在伪静态之前吧:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<pre class="wrap:true lang:default decode:true "> set $skip_cache 0; 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; } |
然后我的php部分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<pre class="wrap:true lang:default decode:true "> location ~ [^/]\.php(/|$) { #fastcgi_pass remote_php_ip:9000; try_files $uri =404; fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"; fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache; add_header X-Cache "$upstream_cache_status From $host"; fastcgi_cache WORDPRESS; add_header Nginx-Cache "$upstream_cache_status"; add_header Last-Modified $date_gmt; add_header X-Frame-Options SAMEORIGIN; add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection "1; mode=block"; fastcgi_cache_valid 200 301 302 1d; etag on; } |
重启系统后shm内存中nginx-cache文件夹会丢失,为了使重启生效(自动创建文件夹),修改/etc/init.d/nginx的make_dirs下一行(大概52行)添加:
1 |
<del><pre class="wrap:true lang:default decode:true ">[ ! -d '/dev/shm/nginx-cache' ] && { mkdir /dev/shm/nginx-cache; chown -R ${user}.$user /dev/shm/nginx-cache; } |
3. WordPress安装Nginx Helper插件或 NGINX Cache Optimizer插件
修改wordpress网站根目录wp-config.php添加如下行(dev/shm是centos内存):
1 |
<pre class="wrap:true lang:default decode:true ">define('RT_WP_NGINX_HELPER_CACHE_PATH','/dev/shm/nginx-cache'); |
可参考原文:https://blog.linuxeye.com/439.html
然后建立一个PHP时间文件,代码:
1 |
<pre class="wrap:true lang:default decode:true ">< ? php echo time(); ? > |
测试,缓存了显示的结果不变:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<pre class="wrap:true lang:default decode:true ">[root@default ~]# curl https://caohuan.com/time.php;echo 1488160160 [root@default ~]# curl https://caohuan.com/time.php;echo 1488160160 [root@default ~]# curl https://caohuan.com/time.php;echo 1488160160 [root@default ~]# curl https://caohuan.com/time.php;echo 1488160160 [root@default ~]# curl https://caohuan.com/time.php;echo 1488160160 [root@default ~]# curl -X GET -I https://caohuan.com HTTP/1.1 200 OK Server: nginx ...... X-Cache: MISS From caohuan.com X-FastCGI-Cache: MISS [root@default ~]# curl -X GET -I https://caohuan.com HTTP/1.1 200 OK Server: nginx ...... X-Cache: HIT From caohuan.com X-FastCGI-Cache: HIT [root@default ~]# curl -X GET -I https://caohuan.com HTTP/1.1 200 OK Server: nginx ...... X-Cache: HIT From caohuan.com X-FastCGI-Cache: HIT |
1 |
<pre class="wrap:true lang:default decode:true ">关键的参数在这里:--prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.0.2j --with-pcre=../pcre-8.39 --with-pcre-jit --with-ld-opt=-ljemalloc --add-module=../ngx_cache_purge-2.3 |
1、我在php7,我打开debug安装WP Super Cache是有错误的,换了wp fastest cache
2、开启nginx的fastcgi_cache,有memcached我这小站也够用的,VPS有不是wordpress所以有是连个一起分着用下。安装插件NGINX Cache Optimizer(这个插件会在wp-content加入object-cache.php但如果你的memcached不工作了这个文件要删除不然网站500),或者可以装一个nginx helper,这个没前者的功能多,两个都可以管理fastcgi_cache缓存,但前者多了个memcached。
2019年:我不再安装加速插件,现在的cpu和php版本已经不需要了这些了。可能我站小,机器大的缘故吧。
vhost配置:
在附件,提供本站正在用的vhost.conf 和nginx.conf,和几篇实用的配置文章,地址不提供了,网站有时站长不做了,技术就流失了,保存下来长久点。
2017-02-19新增nginx无错gzip
1 |
<pre class="wrap:true lang:default decode:true ">gzip_types text/xsl text/xsd text/x-json text/x-js text/richtext image/webp image/tiff image/bmp font/ttf font/otf font/opentype font/eot audio/wav audio/ogg application/x-web-app-manifest+json application/x-shockwave-flash application/x-msdownload application/xml application/xhtml+xml application/x-font-otf application/vnd.oasis.opendocument.text application/vnd.oasis.opendocument.spreadsheet application/vnd.oasis.opendocument.graphics application/vnd.oasis.opendocument.formula application/vnd.oasis.opendocument.database application/vnd.oasis.opendocument.chart application/vnd.ms-write application/vnd.ms-project application/vnd.ms-powerpoint application/vnd.ms-opentype application/vnd.ms-excel application/vnd.ms-access application/rss+xml application/pdf application/msword application/java application/font-woff2 application/font-woff application/atom+xml; |
2019-6-7把国外vps搬回腾讯云,再调整本一些地方,测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<pre class="wrap:true lang:default decode:true ">[root@VM_0_16_centos ch]# curl https://caohuan.com/time.php;echo 1559881815 2019-06-07 [root@VM_0_16_centos ch]# curl https://caohuan.com/time.php;echo 1559881815 2019-06-07 [root@VM_0_16_centos ch]# curl https://caohuan.com/time.php;echo 1559881815 2019-06-07 [root@VM_0_16_centos ch]# curl -X GET -I https://caohuan.com HTTP/1.1 200 OK Server: nginx Date: Fri, 07 Jun 2019 04:30:55 GMT Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive Vary: Accept-Encoding X-Cache-Enabled: True Set-Cookie: nCacheBypass=0; expires=Fri, 07-Jun-2019 03:27:53 GMT; Max-Age=0; path=/ Link: ; rel="https://api.w.org/" Strict-Transport-Security: max-age=63072000; includeSubdomains; preload X-Cache: HIT From caohuan.com Nginx-Cache: HIT Last-Modified: Friday, 07-Jun-2019 04:30:55 GMT X-Frame-Options: SAMEORIGIN X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block |
如果你的主机有多个独立的网站,你可以把nginx.conf的这部分添加到虚拟机内:
1 2 3 4 5 6 7 8 9 |
<pre class="wrap:true lang:default decode:true ">http { fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m; fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_use_stale error timeout invalid_header http_500; fastcgi_ignore_headers Cache-Control Expires Set-Cookie; add_header rt-Fastcgi-Cache $upstream_cache_status; ... } |
---2019.06.24
估计很多人也注意到了没开域名证书时,你装了br和gzip共存,在header的显示还是
Accept-Encoding: gzip
开域名证书后浏览器不同显示就不同
Accept-Encoding: gzip, deflate, br
或者
Accept-Encoding: br