打造您的数字基石,驱动业务持续增长

以科技之钥,开启流畅的密境。我们将精心调校BBR的传输脉搏,编织Varnish Cache的静态华裳,点燃PHP OPcache的代码之火,更让Redis与Memcached在内存的星辰间翩翩起舞。再倾注堡塔加速的精心调和,终将为您献上一次如诗般行云流水的加载体验,让速度本身,成为一种艺术。

一、加载BBR模块:加载BBR模块到内核中。

sudo modprobe tcp_bbr

验证安装:使用以下命令来验证BBR模块是否成功加载:

sysctl net.ipv4.tcp_available_congestion_control

如果你看到输出中包含”bbr”,则表示BBR模块已成功加载。

启用BBR算法:使用以下命令来启用BBR拥塞控制算法:

echo "net.core.default_qdisc=fq" | sudo tee -a /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

看见输出“bbr”,BBR算法就已经成功安装和启用在你的系统的内核中。

二、安装varnish cache:

启用EPEL仓库

sudo dnf install epel-release

安装Varnish

sudo dnf install varnish

查看Varnish版本

varnishd -V

配置后端服务器,编辑 /etc/varnish/default.vcl

vcl 4.1;

# 后端服务器配置
backend default {
    .host = "127.0.0.1";
    .port = "80";
    .first_byte_timeout = 300s;
}

# ACL用于清除缓存
acl purge {
    "localhost";
    "127.0.0.1";
    "::1";
    # 添加你的服务器IP
    "192.168.1.0"/24;
}

sub vcl_recv {
    # 处理PURGE请求
    if (req.method == "PURGE") {
        if (!client.ip ~ purge) {
            return(synth(405, "Method not allowed"));
        }
        return (purge);
    }
    
    # 处理BAN请求
    if (req.method == "BAN") {
        if (!client.ip ~ purge) {
            return(synth(405, "Method not allowed"));
        }
        ban("req.http.host == " + req.http.host + " && req.url == " + req.url);
        return(synth(200, "Banned"));
    }
    
    # 只缓存GET和HEAD请求
    if (req.method != "GET" && req.method != "HEAD") {
        return (pass);
    }
    
    # 绕过管理后台和特定路径
    if (req.url ~ "^/wp-admin" ||
        req.url ~ "^/wp-login" ||
        req.url ~ "^/wp-cron" ||
        req.url ~ "^/wp-json" ||
        req.url ~ "\?add-to-cart=" ||
        req.url ~ "\?wc-ajax=") {
        return (pass);
    }
    
    # 移除Cookie以提高缓存命中率(对于静态资源)
    if (req.url ~ "\.(css|js|png|gif|jp(e?)g|woff|woff2|ttf|eot|svg)(\?.*)?$") {
        unset req.http.Cookie;
    }
    
    # 处理WordPress登录用户
    if (req.http.Cookie) {
        # 移除跟踪参数
        set req.http.Cookie = regsuball(req.http.Cookie, "(^|; ) *__[a-z]+=[^;]+;? *", "\1");
        
        # 如果Cookie包含wordpress_logged_in,绕过缓存
        if (req.http.Cookie ~ "wordpress_logged_in_") {
            return (pass);
        }
        
        # 如果Cookie只剩下空格或分号,删除Cookie头
        if (req.http.Cookie == "" || req.http.Cookie == ";") {
            unset req.http.Cookie;
        }
    }
    
    # 规范化Accept-Encoding头部
    if (req.http.Accept-Encoding) {
        if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
            unset req.http.Accept-Encoding;
        } elsif (req.http.Accept-Encoding ~ "gzip") {
            set req.http.Accept-Encoding = "gzip";
        } elsif (req.http.Accept-Encoding ~ "deflate") {
            set req.http.Accept-Encoding = "deflate";
        } else {
            unset req.http.Accept-Encoding;
        }
    }
    
    return (hash);
}

sub vcl_backend_response {
    # 设置默认TTL
    set beresp.ttl = 2h;
    
    # 静态资源长期缓存
    if (bereq.url ~ "\.(css|js|png|gif|jp(e?)g|woff|woff2|ttf|eot|svg|ico)(\?.*)?$") {
        set beresp.ttl = 30d;
        unset beresp.http.Set-Cookie;
    }
    
    # HTML页面缓存
    if (beresp.http.Content-Type ~ "text/html") {
        set beresp.ttl = 1h;
    }
    
    # 如果后端设置Cookie,不缓存
    if (beresp.http.Set-Cookie) {
        set beresp.uncacheable = true;
        set beresp.ttl = 120s;
        return (deliver);
    }
    
    # 处理缓存控制头部
    if (beresp.http.Cache-Control ~ "(private|no-cache|no-store)") {
        set beresp.uncacheable = true;
        set beresp.ttl = 120s;
        return (deliver);
    }
    
    # 移除Set-Cookie头(如果存在)以提高缓存能力
    unset beresp.http.Set-Cookie;
}

sub vcl_deliver {
    # 添加缓存命中状态头
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT";
        set resp.http.X-Cache-Hits = obj.hits;
    } else {
        set resp.http.X-Cache = "MISS";
    }
    
    # 显示缓存状态
    set resp.http.X-Cache-Engine = "Varnish";
    set resp.http.X-Powered-By = "OpenCloudOS + Varnish";
}

WordPress配置,在WordPress的 wp-config.php 中添加:

// Varnish缓存配置
define('WP_CACHE', true);

// 自动清除缓存配置
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    $_SERVER['REMOTE_ADDR'] = $ips[0];
}

WordPress插件配置,安装并配置缓存清理插件,创建 /wp-content/plugins/varnish-http-purge/varnish-http-purge.php

<?php
/**
 * Plugin Name: Varnish HTTP Purge
 * Description: Automatically purge Varnish cache when content is published or updated.
 */

class VarnishHttpPurge {
    
    public function __construct() {
        add_action('init', array($this, 'init'));
    }
    
    public function init() {
        // 文章发布或更新时清除缓存
        add_action('publish_post', array($this, 'purgePost'));
        add_action('publish_page', array($this, 'purgePost'));
        add_action('edit_post', array($this, 'purgePost'));
        add_action('delete_post', array($this, 'purgePost'));
        add_action('trashed_post', array($this, 'purgePost'));
        
        // 评论相关
        add_action('comment_post', array($this, 'purgePostOnComment'));
        add_action('wp_set_comment_status', array($this, 'purgePostOnCommentStatus'));
    }
    
    public function purgePost($postId) {
        $post = get_post($postId);
        if (!in_array($post->post_status, array('publish', 'trash'))) {
            return;
        }
        
        $this->purgeUrl(get_permalink($postId));
        $this->purgeUrl(home_url('/'));
    }
    
    public function purgePostOnComment($commentId) {
        $comment = get_comment($commentId);
        $this->purgePost($comment->comment_post_ID);
    }
    
    public function purgePostOnCommentStatus($commentId) {
        $comment = get_comment($commentId);
        $this->purgePost($comment->comment_post_ID);
    }
    
    private function purgeUrl($url) {
        $host = parse_url($url, PHP_URL_HOST);
        $path = parse_url($url, PHP_URL_PATH);
        
        $purgeUrl = "http://" . $host . $path;
        
        wp_remote_request($purgeUrl, array(
            'method' => 'PURGE',
            'headers' => array(
                'Host' => $host,
                'X-Purge-Method' => 'default'
            )
        ));
    }
}

new VarnishHttpPurge();

Nginx/Apache后端配置,如果使用Nginx作为后端,配置示例:

server
{
    listen 80 default_server;
    listen 443 ssl default_server;
    listen 443 quic default_server;
    listen [::]:443 ssl default_server;
    listen [::]:443 quic default_server;
    http2 on;
    listen [::]:80 default_server;

启动和测试服务

# 启动Varnish
sudo systemctl enable varnish
sudo systemctl start varnish

# 查看状态
sudo systemctl status varnish

三、PHP OPcache:

opcache.enable=1
opcache.enable_cli=0
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.max_wasted_percentage=5
opcache.use_cwd=1
opcache.validate_timestamps=1
opcache.revalidate_freq=60
opcache.revalidate_path=0
opcache.save_comments=1
opcache.fast_shutdown=1
opcache.enable_file_override=1
opcache.optimization_level=0x7FFEBFFF
opcache.dups_fix=0
opcache.blacklist_filename=
opcache.max_file_size=0
opcache.consistency_checks=0
opcache.force_restart_timeout=180
opcache.error_log=
opcache.log_verbosity_level=1
opcache.record_warnings=0
opcache.preferred_memory_model=
opcache.protect_memory=0
opcache.mmap_base=
opcache.restrict_api=
opcache.file_update_protection=2
opcache.huge_code_pages=0
opcache.lockfile_path=/tmp
opcache.opt_debug_level=0
opcache.file_cache=
opcache.file_cache_only=0
opcache.file_cache_consistency_checks=1
opcache.validate_permission=0
opcache.validate_root=0
opcache.preload=
opcache.preload_user=

四、常用标头:

    add_header Strict-Transport-Security "max-age=63072000;includeSubDomains; preload" always;
    add_header Cache-Control "public,max-age=600";

五、宝塔面板中有两个提高加载速度的工具

WP Tools和“堡塔网站加速”这个免费插件

当 OpenCloudOS 9.4 的沉静内力,遇见 WordPress 的无限可能。我们在此缔结盟约,将冗余与迟缓拂去,只留下轻盈的体验,如一缕清风,瞬间拂过心弦。

原创文章,作者:高 云,如若转载,请注明出处:https://www.woxbb.com/1.html

(0)
分享本页
返回顶部
🤖
🤖

智能客服

在线
🤖

您好!我是智能客服,很高兴为您服务!有什么可以帮助您的吗?

06:10
移动端向上箭头