安全无止界

如何让网站访问速度更快

影响访问速度因素

  • 终端
    • 电脑配置
    • 浏览器
      • 浏览器选择与配置调优
    • 网卡
  • 网络层
    • 运营商
    • 带宽
  • 服务端
    • 运营商
    • 带宽
    • 协议
    • Web Server
      • 最新版本
      • 动静分离
    • 文件大小
    • 页面级缓存
    • 语言级缓存
    • 框架级缓存
    • 应用级缓存
    • 数据级缓存

最佳实践

WordPress

WordPress中可以显著提升访问速度是FastCGI Cache > 页面资源优化。

FastCGI Cache

http {
    fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=WORDPRESS:256m inactive=30m;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
    fastcgi_cache_use_stale error timeout invalid_header http_500;
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

    add_header X-FastCGI-Cache $upstream_cache_status;

    server {
        location ~ \.php$ {
 
            try_files $uri =404; # comment out this line if php-fpm is hosted on a remote machine
            include /etc/nginx/fastcgi.conf;
 
            limit_req zone=limit_req_php burst=20 nodelay;
 
            # 管理后台等路径不做缓存
            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 $skip_cache 1;
            }

            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

            # FastCGI CACHE
            fastcgi_cache WORDPRESS;
            fastcgi_cache_valid 200 60m;
            fastcgi_cache_methods GET HEAD;
            fastcgi_cache_bypass $skip_cache;
            fastcgi_no_cache $skip_cache;

            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            if (!-f $document_root$fastcgi_script_name) {
                return 404;
            }
        }
    }
}

FastCGI Cache缓存清除

find | xargs grep -s 'fastweb' /var/cache/nginx/fastcgi
安全无止界

标签

Loading