Nginx 对于单个或多个WordPress 程序配置详解(WP官方)

2020-10-22 0 2,336 百度已收录

W3的总缓存规则W3总缓存规则

根据WordPress配置,W3 Total Cache对基于磁盘的缓存存储使用不同的目录结构。

缓存验证检查将保持常见,如下所示:

1个
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18岁
19
20
21
22
23
#W3 TOTAL CACHE CHECK
set $cache_uri $request_uri;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
        set $cache_uri 'null cache';
}  
if ($query_string != "") {
        set $cache_uri 'null cache';
}  
# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
        set $cache_uri 'null cache';
}  
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
        set $cache_uri 'null cache';
}
#ADD mobile rules from WP SUPER CACHE section above
#APPEND A CODE BLOCK FROM BELOW...

对于普通WordPress(无多站点),
请使用以下命令:

1个
2
3
4
# Use cached or actual file if they exists, otherwise pass request to WordPress
location / {
        try_files /wp-content/w3tc/pgcache/$cache_uri/_index.html $uri $uri/ /index.php?$args ;
}  

对于带有子目录的多站点,
请使用以下命令:

1个
2
3
4
5
6
7
8
9
10
11
12
13
14
if ( $request_uri ~* "^/([_0-9a-zA-Z-]+)/.*" ){
        set $blog $1;
}
set $blog "${blog}.";
if ( $blog = "blog." ){
        set $blog "";
}
# Use cached or actual file if they exists, otherwise pass request to WordPress
location / {
        try_files /wp-content/w3tc-$blog$host/pgcache$cache_uri/_index.html $uri $uri/ /index.php?$args ;
}

对于具有子域/域映射的多站点,
请使用以下命令:

1个
2
3
location / {
        try_files /wp-content/w3tc-$host/pgcache/$cache_uri/_index.html $uri $uri/ /index.php?$args;
}

笔记

  • Nginx可以自动处理gzip和浏览器缓存,因此最好将该部分留给nginx。
  • W3 Total Cache Minify规则将与上述配置一起使用,没有任何问题。

Nginx fastcgi_cache Nginx的fastcgi_cache

Nginx可以在自己的一端执行缓存以减少服务器上的负载。当您想使用Nginx的内置fastcgi_cache时,最好使用fastcgi_cache_purge模块编译nginx 。当页面被编辑时,它将帮助nginx清除页面的缓存。在WordPress方面,您需要安装Nginx Helper之类的插件才能利用fastcgi_cache_purge功能。

Config如下所示:

在服务器{…}块之外的http {…}块中定义一个Nginx缓存区域

1个
2
3
4
#move next 3 lines to /etc/nginx/nginx.conf if you want to use fastcgi_cache across many sites
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:500m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;

对于WordPress网站配置,在server {..}块中添加一个缓存检查块,如下所示

1个
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18岁
19
20
#fastcgi_cache start
set $no_cache 0;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
        set $no_cache 1;
}  
if ($query_string != "") {
        set $no_cache 1;
}  
# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
        set $no_cache 1;
}  
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $no_cache 1;
}

然后更改PHP处理块

只需将其添加到以下php块中即可。请注意,这行fastcgi_cache_valid 200 60m;告诉nginx仅缓存200个响应(正常页面),这意味着不缓存重定向。这对于多语言站点非常重要,在该站点中,如果未实现,nginx会以一种语言缓存主URL,而不是根据用户的语言将用户重定向到其各自的内容。

1个
2
3
4
5
fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 60m;

这样就变成了这样的东西

1个
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18岁
location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }
    # This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default)
    include fastcgi.conf;
    fastcgi_index index.php;
#    fastcgi_intercept_errors on;
    fastcgi_pass php;
    fastcgi_cache_bypass $no_cache;
    fastcgi_no_cache $no_cache;
    fastcgi_cache WORDPRESS;
    fastcgi_cache_valid 200 60m;
}

最后添加一个有条件清除的位置

1个
2
3
4
5
6
7
location ~ /purge(/.*) {
        # Uncomment the following two lines to allow purge only from the webserver
        #allow 127.0.0.1;
    #deny all;
        fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}

如果收到“未知指令“ fastcgi_cache_purge””错误,请检查您的Nginx安装是否具有fastcgi_cache_purge模块。

性能表现,静态文件的多站点多站点中静态文件的性能更高

默认情况下,在多站点设置中,静态文件请求将php带入图片即ms-files.php文件。使用NginxMap{..}指令可以获得更好的性能。

在您的站点的Nginx配置中,在server{..}块上方,添加如下部分:

1个
2
3
4
5
6
7
map $http_host $blogid {
    default               0;
    example.com           1;
    site1.example.com     2;
    site1.com             2;
}

它只是站点名称和博客ID的列表。您可以使用Nginx帮助器来获得这样的站点名称/博客ID对列表。该插件还将生成一个map.conf文件,您可以将其直接包含在map {}部分中,如下所示:

1个
2
3
4
5
map $http_host $blogid {
    default               0;
    include /path/to/map.conf ;
}

创建一个map{..}部分之后,您只需要在Nginx配置中再做一个更改,以便/files/首先使用nginx处理对请求的请求map{..}

1个
2
3
4
location ~ ^/files/(.*)$ {
          try_files /wp-content/blogs.dir/$blogid/$uri /wp-includes/ms-files.php?file=$1 ;
          access_log off; log_not_found off; expires max;
 }

注释笔记

  • 每当创建,删除新站点或将其他域映射到现有站点时,Nginx帮助程序都会自动更新map.conf文件,但您仍然需要手动重新加载Nginx配置。您以后可以随时执行此操作。到那时,将仅使用php-fpm提供新站点的文件。
  • 此方法不会生成任何符号链接。因此,跟随符号链接的意外删除或备份脚本不会有任何问题。
  • 对于大型网络,这将很好地扩大规模,因为将只有一个map.conf文件。

注释笔记

最后但重要的几个注意事项:整个安装过程假定站点的根是博客,并且将引用的所有文件都位于主机上。如果将博客放在/ blog等子目录中,则必须修改规则。也许有人可以遵循这些规则,并可以使用以下规则:

1个
set $wp_subdir "/blog";

指令在主“服务器”块中,并自动应用于通用WP规则。

警告警告

  • 全局限制文件中的错字可能会造成漏洞。要测试您的“上载”目录是否真正受到保护,请创建一个包含某些内容的PHP文件(例如:<?php phpinfo();?>),然后将其上传到“上载”目录(或其子目录之一),然后尝试从浏览器访问(执行)它。
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

栗子博客 网站 Nginx 对于单个或多个WordPress 程序配置详解(WP官方) https://www.lizi.tw/web/18310.html

建筑工地上施工员,闲暇时弄个博客打发时间,

常见问题
  • 1、杰齐1.7仅适用于PHP5.2 2、需Zend支持 3、尽量使用宝塔面板 4、尽量使用Windows 系统,关关对Linux支持不太友好。
查看详情

相关文章

评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Nginx 对于单个或多个WordPress 程序配置详解(WP官方)-海报

分享本文封面