您现在的位置是:首页 > cms教程 > WordPress教程WordPress教程
使用Nginx缓存实现加速WordPress站点
薛敬文2023-07-07WordPress教程已有人查阅
导读博客开通以来,主要记录学习和使用过程中遇到的问题及解决方案。文章风格偏向自娱自乐,因此访问量较少,一台1核1G的vps足以支撑网站的正常运行。

博客开通以来,主要记录学习和使用过程中遇到的问题及解决方案。文章风格偏向自娱自乐,因此访问量较少,一台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 .tlanyan.me tlanyan.me; # 请换成自己的域名
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name .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/ /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登录用户的角色
将下面的代码添加到当前主题functions.php中:在主题模板适当位置添加调用代码:再配合以下的WordPress用户信息函数:
wordpress-4.4.1数据库表结构解析
wordpress-4.4.1.zip 安装包SQL结构 :wp_commentmeta :文章评论额外信息表。CREATE TABLE IF NOT EXISTS `wp_commentmeta` (`meta_id` bigint(20) unsigned NOT NULL A
wordpress是什么语言开发的
wordpress基于PHP语言开发的。WordPress是一个以PHP和MySQL为平台的自由开源的博客软件和内容管理系统。WordPress具有插件架构和模板系统。
wordpress开启多用户/多站点/多域名模式
刷新网站后台,访问 工具 > 配置网络,根据自己的需要选择“子域名”或者“子目录”模式,设置好网站信息,然后点击“安装”
相关源码
-
(PC+WAP)蓝色钢材加工建筑装修施工材料网站模板下载为钢材加工企业设计的PbootCMS响应式模板,采用PC+WAP双端适配技术,数据实时同步。简洁大气的蓝色工业风格设计,突出钢材加工行业特性,其他制造业用户更换图文内容即可快速应用。查看源码 -
(自适应响应式)房产合同知识产权企业管理pbootcms模板下载本模板基于PbootCMS系统开发,为知识产权服务、法律咨询及企业合同管理等行业设计。采用严谨专业的布局风格,突出法律文书与知识产权服务行业特色,适合展示各类法律服务和知识产权相关内容。查看源码 -
(自适应响应式)pbootcms食品零食店日化用品网站源码下载基于PbootCMS内核开发的响应式模板,为食品零食、日化用品等行业量身定制。该模板通过可视化设计展现产品特色,帮助企业快速搭建专业官网查看源码 -
(自适应)居家生活日用品纸盘纸盒纸杯卫生纸巾生产厂家pbootcms模板为纸品生产企业打造的现代化展示平台,自动适应各种设备屏幕,确保浏览体验一致,完善的SEO功能,提升网站曝光度,基于PbootCMS构建,源码开放可定制。查看源码 -
(自适应响应式)运动健身瑜伽俱乐部网站pbootcms源码下载为健身瑜伽俱乐部设计的响应式网站模板,采用PbootCMS内核开发,可快速搭建专业级企业官网。模板默认适配运动健身行业视觉风格,用户可通过替换图文内容灵活应用于其他服务行业。查看源码 -
(PC+WAP)红色家装设计智能家居家具建材pbootcms网站源码下载本模板基于PbootCMS系统开发,为智能家居、家装设计及家具建材行业设计。采用现代化布局风格,突出家居设计行业特色,适合展示各类家居产品、设计方案和建材信息。查看源码
| 分享笔记 (共有 篇笔记) |

