### [怎么使用Nginx缓存加速WordPress站点](https://www.prosaas.cn/article/9022) **Published:** 2020-06-06T09:11:51 **Author:** 5201314 **Excerpt:** 下面由WordPress建站教程栏目给大家介绍WordPress REST API的基本使用,希望对需要的朋友… 下面由**WordPress建站教程**栏目给大家介绍WordPress REST API的基本使用,希望对需要的朋友有所帮助! ![](//zhiqipro.oss-cn-beijing.aliyuncs.com/2023/12/20231212131900898.jpg) 博客开通以来,主要记录学习和使用过程中遇到的问题及解决方案。文章风格偏向自娱自乐,因此访问量较少,一台1核1G的vps足以支撑网站的正常运行。 后来本站引入三个页面,这三个页面应该对有上外网需求的网友很有帮助,也给本站带来了很大的流量。本站用的WordPress程序,尝试过安装各种缓存插件(super cache, w3 total cache等)加速运行,但是低配的vps依然难以支持这么大的访问量。通过日志可以看到随着访问量的增加,php-fpm进程增多,Mysql的连接和线程增多,接着出现OOM,然后系统kill掉占用内存最大的Mysql进程,于是网站进入503宕机模式。 买更好的vps能解决访问量大的问题,但是要花更多的钱。做为一个技术宅,首先想到的当然是如何榨干现有机器来支撑大流量。做过的尝试包括切换到比WordPress性能更好的Ghost,参考:尝试Ghost 。但是相对于WordPress,Ghost的生态远没有那么成熟,最终放弃了。 左思右想下,终极解决办法是用Nginx缓存,最初的文章可参考:Nginx配置fastcgi cache。fastcgi\_cache的好处是大部分用户的请求不用后端php-fpm打交道,直接发送缓存的静态页面,速度上甩各种WordPress插件好几条街!相比之下wordpress的各种插件还要执行php,也避免不了访问数据库,弱爆了! 自从使用了nginx缓存,网站平稳运行,再也没有出现过宕机的现象。同时vps的cpu和内存占用率直线下降,再也无需担心vps的配置问题,感觉再来10倍流量博客也撑得住! 因为nginx稳如狗的体验,所以现在对于博客类读多写少的产品都是强推nginx缓存(fastcgi缓存或者proxy缓存)。鉴于可能帮到一些网友,现贴出 /etc/nginx/nginx.conf 配置文件供网友参考(包含ssl设置和gzip部分): \# 文件: /etc/nginx/nginx.conf # For more information on configuration, see: # \* Official English Documentation: http://nginx.org/en/docs/ # \* Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker\_processes auto; error\_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/\*.conf; events { worker\_connections 1024; } http { log\_format main '$remote\_addr - $remote\_user \[$time\_local\] "$request" ' '$status $body\_bytes\_sent "$http\_referer" ' '"$http\_user\_agent" "$http\_x\_forwarded\_for" "$request\_time"'; access\_log /var/log/nginx/access.log main buffer=32k flush=30s; server\_tokens off; client\_max\_body\_size 100m; sendfile on; tcp\_nopush on; tcp\_nodelay on; keepalive\_timeout 65; types\_hash\_max\_size 2048; include /etc/nginx/mime.types; default\_type application/octet-stream; # ssl配置 ssl\_protocols TLSv1.2 TLSv1.3; ssl\_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384; ssl\_ecdh\_curve secp384r1; ssl\_prefer\_server\_ciphers on; ssl\_session\_cache shared:SSL:10m; ssl\_session\_timeout 10m; ssl\_session\_tickets off; ssl\_stapling on; # Requires nginx >= 1.3.7 ssl\_stapling\_verify on; # Requires nginx => 1.3.7 add\_header Strict-Transport-Security "max-age=63072000; preload"; #add\_header X-Frame-Options DENY; add\_header X-Frame-Options SAMEORIGIN; add\_header X-Content-Type-Options nosniff; add\_header X-XSS-Protection "1; mode=block"; # 请按照自己的需求更改 fastcgi\_cache\_path /var/cache/nginx/tlanyan levels=1:2 keys\_zone=tlanyan:10m inactive=30m use\_temp\_path=off; fastcgi\_cache\_key $request\_method$scheme$host$request\_uri; # note: can also use HTTP headers to form the cache key, e.g. #fastcgi\_cache\_key $scheme$request\_method$host$request\_uri$http\_x\_custom\_header; #fastcgi\_cache\_lock on; fastcgi\_cache\_use\_stale error timeout invalid\_header updating http\_500; fastcgi\_cache\_valid 200 301 302 10h; fastcgi\_cache\_valid 404 10m; fastcgi\_ignore\_headers Expires Set-Cookie Vary; # gzip 配置 gzip on; gzip\_min\_length 1k; gzip\_buffers 4 16k; gzip\_comp\_level 7; gzip\_types text/css text/plain text/javascript application/javascript application/json application/x-javascript application/xml application/xml+rss application/xhtml+xml application/x-font-ttf application/x-font-opentype application/vnd.ms-fontobject image/svg+xml image/x-icon application/rss+xml application/atom\_xml image/jpeg image/gif image/png image/icon image/bmp image/jpg; gzip\_vary on; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx\_core\_module.html#include # for more information. include /etc/nginx/conf.d/\*.conf; } 以及用于WordPress站点的网站配置文件(/etc/nginx/conf.d/tlanyan.conf): server { listen 80; listen \[::\]:80; server\_name www.tlanyan.me tlanyan.me; # 请换成自己的域名 rewrite ^(.\*) https://$server\_name$1 permanent; } server { listen 443 ssl http2; listen \[::\]:443 ssl http2; server\_name www.tlanyan.me tlanyan.me; # 请换成自己的域名 charset utf-8; ssl\_certificate /etc/nginx/conf.d/tlanyan.pem; # 请换成自己的证书和密钥 ssl\_certificate\_key /etc/nginx/conf.d/tlanyan.key; set $host\_path "/var/www/tlanyan"; # 请改成自己的路径 access\_log /var/log/nginx/tlanyan.access.log main buffer=32k flush=30s; error\_log /var/log/nginx/tlanyan.error.log; root $host\_path; # 缓存标记 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 = / { index index.php index.html; try\_files /index.php?$args /index.php?$args; } location / { index index.php index.html; try\_files $uri $uri/ /index.php?$args; } location ~ ^/\\.user\\.ini { deny all; } location ~ \\.php$ { try\_files $uri =404; fastcgi\_index index.php; fastcgi\_pass 127.0.0.1:9000; fastcgi\_cache tlanyan; fastcgi\_cache\_valid 200 301 302 30m; fastcgi\_cache\_valid 404 10m; fastcgi\_cache\_bypass $skip\_cache; fastcgi\_no\_cache $skip\_cache; fastcgi\_cache\_lock on; include fastcgi\_params; fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name; } location ~ \\.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar|jpeg)$ { expires max; access\_log off; try\_files $uri =404; } } 上述配置对最新版的Nginx测试有效,详细配置指令请参考Nginx官方文档。 更多WordPress技术文章,请访问**WordPress**栏目! 以上就是怎么使用Nginx缓存加速WordPress站点的详细内容,更多请关注知企PROSAAS其它相关文章! **Categories:** WordPress ---