也想出现在这里? 联系我们

使用NGINX缓存加速WordPress站点的方法

作者 : 小编 本文共4437个字,预计阅读时间需要12分钟 发布时间: 2020-09-16 共1.18K人阅读
也想出现在这里? 联系我们

很多站长在优化WordPress站点的时候会使用到一些缓存插件来加速运行,但是低配的vps会很难支持很大的访问量,买更好的vps能解决访问量大的问题,但是要花更多的钱。做为一个技术宅,首先想到的当然是如何榨干现有机器来支撑大流量。今天就分享一下Nginx缓存,Nginx配置fastcgi cache。fastcgi_cache的好处是大部分用户的请求不用后端php-fpm打交道,直接发送缓存的静态页面,分享一下使用NGINX缓存加速WordPress站点的方法。

自从使用了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官方文档。

1. 本站所提供的源码模板(主题/插件)等资源仅供学习交流,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担,有部分资源为网上收集或仿制而来,若模板侵犯了您的合法权益,请来信通知我们(Email: rayer@88.com),我们会及时删除,给您带来的不便,我们深表歉意!
2. 分享目的仅供大家学习和交流,请不要用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布投稿,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务 请大家谅解!
5. 如有链接无法下载、失效或广告,请联系站长,可领回失去的金币,并额外有奖!
6. 如遇到加密压缩包,默认解压密码为"www.zyfx8.cn",如遇到无法解压的请联系管理员!
本站部分文章、资源来自互联网,版权归原作者及网站所有,如果侵犯了您的权利,请及时联系我站删除。免责声明
资源分享吧 » 使用NGINX缓存加速WordPress站点的方法

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
织梦模板使用说明
你下载的织梦模板并不包括DedeCMS使用授权,根据DedeCMS授权协议,除个人非盈利站点外,均需购买DedeCMS商业使用授权。购买地址: http://www.desdev.cn/service-dedecms.html

发表评论

Copyright 2015-2020 版权所有 资源分享吧 Rights Reserved. 蜀ICP备14022927号-1
开通VIP 享更多特权,建议使用QQ登录