宿主/Service

使用 Nginx 缓存加速 WordPress 博客站点

阿里云

通常博客类型的小站,主要记录学习和使用过程中遇到的问题及解决方案。文章风格偏向自娱自乐,因此访问量较少,一台 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 部分):

  1. # 文件: /etc/nginx/nginx.conf
  2.  
  3. # For more information on configuration, see:
  4.  
  5. #   * Official English Documentation: http://nginx.org/en/docs/
  6.  
  7. #   * Official Russian Documentation: http://nginx.org/ru/docs/
  8.  
  9.  
  10.  
  11. user nginx;
  12.  
  13. worker_processes auto;
  14.  
  15. error_log /var/log/nginx/error.log;
  16.  
  17. pid /run/nginx.pid;
  18.  
  19.  
  20.  
  21. # Load dynamic modules. See /usr/share/nginx/README.dynamic.
  22.  
  23. include /usr/share/nginx/modules/*.conf;
  24.  
  25.  
  26.  
  27. events {
  28.  
  29.     worker_connections 1024;
  30.  
  31. }
  32.  
  33.  
  34.  
  35. http {
  36.  
  37.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  38.  
  39.                       '$status $body_bytes_sent "$http_referer" '
  40.  
  41.                       '"$http_user_agent" "$http_x_forwarded_for" "$request_time"';
  42.  
  43.  
  44.  
  45.     access_log  /var/log/nginx/access.log  main buffer=32k flush=30s;
  46.  
  47.  
  48.  
  49.     server_tokens       off;
  50.  
  51.     client_max_body_size 100m;
  52.  
  53.  
  54.  
  55.     sendfile            on;
  56.  
  57.     tcp_nopush          on;
  58.  
  59.     tcp_nodelay         on;
  60.  
  61.     keepalive_timeout   65;
  62.  
  63.     types_hash_max_size 2048;
  64.  
  65.  
  66.  
  67.     include             /etc/nginx/mime.types;
  68.  
  69.     default_type        application/octet-stream;
  70.  
  71.  
  72.  
  73.     # ssl配置
  74.  
  75.     ssl_protocols TLSv1.2 TLSv1.3;
  76.  
  77.     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;
  78.  
  79.     ssl_ecdh_curve secp384r1;
  80.  
  81.     ssl_prefer_server_ciphers on;
  82.  
  83.     ssl_session_cache shared:SSL:10m;
  84.  
  85.     ssl_session_timeout 10m;
  86.  
  87.     ssl_session_tickets off;
  88.  
  89.     ssl_stapling on; # Requires nginx >= 1.3.7
  90.  
  91.     ssl_stapling_verify on; # Requires nginx => 1.3.7
  92.  
  93.     add_header Strict-Transport-Security "max-age=63072000; preload";
  94.  
  95.     #add_header X-Frame-Options DENY;
  96.  
  97.     add_header X-Frame-Options SAMEORIGIN;
  98.  
  99.     add_header X-Content-Type-Options nosniff;
  100.  
  101.     add_header X-XSS-Protection "1; mode=block";
  102.  
  103.  
  104.  
  105.     # 请按照自己的需求更改
  106.  
  107.     fastcgi_cache_path /var/cache/nginx/22vd levels=1:2 keys_zone=22vd:10m inactive=30m use_temp_path=off; 
  108.  
  109.     fastcgi_cache_key $request_method$scheme$host$request_uri;
  110.  
  111.     # note: can also use HTTP headers to form the cache key, e.g.
  112.  
  113.     #fastcgi_cache_key $scheme$request_method$host$request_uri$http_x_custom_header;
  114.  
  115.     #fastcgi_cache_lock on;
  116.  
  117.     fastcgi_cache_use_stale error timeout invalid_header updating http_500;
  118.  
  119.     fastcgi_cache_valid 200 301 302 10h;
  120.  
  121.     fastcgi_cache_valid 404 10m;
  122.  
  123.     fastcgi_ignore_headers Expires Set-Cookie Vary;
  124.  
  125.  
  126.  
  127.     # gzip 配置
  128.  
  129.     gzip on;
  130.  
  131.     gzip_min_length  1k;
  132.  
  133.     gzip_buffers     4 16k;
  134.  
  135.     gzip_comp_level 7;
  136.  
  137.     gzip_types
  138.  
  139.         text/css
  140.  
  141.         text/plain
  142.  
  143.         text/javascript
  144.  
  145.         application/javascript
  146.  
  147.         application/json
  148.  
  149.         application/x-javascript
  150.  
  151.         application/xml
  152.  
  153.         application/xml+rss
  154.  
  155.         application/xhtml+xml
  156.  
  157.         application/x-font-ttf
  158.  
  159.         application/x-font-opentype
  160.  
  161.         application/vnd.ms-fontobject
  162.  
  163.         image/svg+xml
  164.  
  165.         image/x-icon
  166.  
  167.         application/rss+xml
  168.  
  169.         application/atom_xml
  170.  
  171.         image/jpeg
  172.  
  173.         image/gif
  174.  
  175.         image/png
  176.  
  177.         image/icon
  178.  
  179.         image/bmp
  180.  
  181.         image/jpg;
  182.  
  183.     gzip_vary on;
  184.  
  185.  
  186.  
  187.     # Load modular configuration files from the /etc/nginx/conf.d directory.
  188.  
  189.     # See http://nginx.org/en/docs/ngx_core_module.html#include
  190.  
  191.     # for more information.
  192.  
  193.     include /etc/nginx/conf.d/*.conf;
  194.  
  195. }

以及用于 WordPress 站点的网站配置文件(/etc/nginx/conf.d/22vd.conf):

  1. server {
  2.  
  3.     listen 80;
  4.  
  5.     listen [::]:80;
  6.  
  7.     server_name www.22vd.coom 22vd.com; # 请换成自己的域名
  8.  
  9.     rewrite ^(.*) https://$server_name$1 permanent;
  10.  
  11. }
  12.  
  13.  
  14.  
  15. server {
  16.  
  17.     listen       443 ssl http2;
  18.  
  19.     listen       [::]:443 ssl http2;
  20.  
  21.     server_name www.22vd.coom 22vd.com; # 请换成自己的域名
  22.  
  23.     charset utf-8;
  24.  
  25.     ssl_certificate /etc/nginx/conf.d/22vd.pem;  # 请换成自己的证书和密钥
  26.  
  27.     ssl_certificate_key /etc/nginx/conf.d/22vd.key;
  28.  
  29.  
  30.  
  31.     set $host_path "/var/www/22vd";  # 请改成自己的路径
  32.  
  33.     access_log  /var/log/nginx/22vd.access.log  main buffer=32k flush=30s;
  34.  
  35.     error_log /var/log/nginx/22vd.error.log;
  36.  
  37.  
  38.  
  39.     root   $host_path;
  40.  
  41.  
  42.  
  43.     # 缓存标记
  44.  
  45.     set $skip_cache 0;
  46.  
  47.     if ($query_string != "") {
  48.  
  49.         set $skip_cache 1;
  50.  
  51.     }
  52.  
  53.     if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|sitemap(_index)?.xml") {
  54.  
  55.         set $skip_cache 1;
  56.  
  57.     }
  58.  
  59.     # 登录用户或发表评论者
  60.  
  61.     if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
  62.  
  63.         set $skip_cache 1;
  64.  
  65.     }
  66.  
  67.  
  68.  
  69.     location = / {
  70.  
  71.         index  index.php index.html;
  72.  
  73.         try_files /index.php?$args /index.php?$args;
  74.  
  75.     }
  76.  
  77.  
  78.  
  79.     location / {
  80.  
  81.         index  index.php index.html;
  82.  
  83.         try_files $uri $uri/ /index.php?$args;
  84.  
  85.     }
  86.  
  87.     location ~ ^/\.user\.ini {
  88.  
  89.             deny all;
  90.  
  91.     }
  92.  
  93.  
  94.  
  95.     location ~ \.php$ {
  96.  
  97.         try_files $uri =404;
  98.  
  99.         fastcgi_index index.php;
  100.  
  101.         fastcgi_pass   127.0.0.1:9000;
  102.  
  103.         fastcgi_cache 22vd;
  104.  
  105.         fastcgi_cache_valid 200 301 302 30m;
  106.  
  107.         fastcgi_cache_valid 404 10m;
  108.  
  109.         fastcgi_cache_bypass $skip_cache;
  110.  
  111.         fastcgi_no_cache $skip_cache;
  112.  
  113.         fastcgi_cache_lock on;
  114.  
  115.         include fastcgi_params;
  116.  
  117.         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  118.  
  119.     }
  120.  
  121.     location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar|jpeg)$ {
  122.  
  123.         expires max;
  124.  
  125.         access_log off;
  126.  
  127.         try_files $uri =404;
  128.  
  129.     }
  130.  
  131. }

使用 Nginx 缓存加速 WordPress 博客站点

已有 233 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!