在以下假设下,您建议使用什么样的缓存插件配置?为什么
完全控制服务器配置,在多站点/多域模式下运行WordPress,大多数域未使用www.
前缀(cookie)
此外,请不要使用一般指导原则,如较小的图像。
公平地说,使用多个缓存插件会给您带来比解决问题更多的问题,所以请尝试给出一个简单的方法。
在以下假设下,您建议使用什么样的缓存插件配置?为什么
完全控制服务器配置,在多站点/多域模式下运行WordPress,大多数域未使用www.
前缀(cookie)
此外,请不要使用一般指导原则,如较小的图像。
公平地说,使用多个缓存插件会给您带来比解决问题更多的问题,所以请尝试给出一个简单的方法。
“什么插件”的基本答案可能是W3 Total Cache. 它是目前功能最强大、开发最活跃的插件之一。然而,完整的性能链比WordPress插件单独处理要长得多。
Web服务器(Apache或其他)配置(响应时间、到第一个字节的时间、标头)好的开始应该是静态缓存插件(如W3)和基于操作码内存的缓存,如APC.
但从那里可以做更多(更复杂)的事情,比如内容分发网络、备用web服务器等。
默认情况下,Apache在端口80(默认web端口)上侦听请求。首先,我们将对Apache conf和虚拟主机文件进行更改,以侦听端口8080。
httpd.conf
将KeepAlive设置为offports.conf
NameVirtualHost *:8080
Listen 8080
Per Site Virtual Host
<VirtualHost 127.0.0.1:8080>
ServerAdmin info@yoursite.com
ServerName yoursite.com
ServerAlias www.yoursite.com
DocumentRoot /srv/www/yoursite.com/public_html/
ErrorLog /srv/www/yoursite.com/logs/error.log
CustomLog /srv/www/yoursite.com/logs/access.log combined
</VirtualHost>
您还应该安装mod_rpaf 因此,您的日志将包含访问者的真实ip地址。如果没有,您的日志将以127.0.0.1作为原始ip地址。$ nano /etc/apt/sources.list
deb http://www.backports.org/debian lenny-backports main
$ nano /etc/apt/preferences
Package: nginx
Pin: release a=lenny-backports
Pin-Priority: 999
发出以下命令以从Backport导入密钥。org验证包并更新系统的包数据库:$ wget -O - http://backports.org/debian/archive.key | apt-key add -
$ apt-get update
现在使用apt get安装apt-get install nginx
nginx.conf
user www-data;
worker_processes 4;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
client_body_temp_path /var/lib/nginx/body 1 2;
gzip_buffers 32 8k;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_comp_level 6;
gzip_http_version 1.0;
gzip_min_length 0;
gzip_types text/html text/css image/x-icon
application/x-javascript application/javascript text/javascript application/atom+xml application/xml ;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
现在您需要设置Nginx虚拟主机。我喜欢使用sites enabled方法,将每个v主机sym链接到sites available目录中的一个文件。$ mkdir /etc/nginx/sites-available
$ mkdir /etc/nginx/sites-enabled
$ touch /etc/nginx/sites-available/yourservername.conf
$ touch /etc/nginx/sites-available/default.conf
$ ln -s /etc/nginx/sites-available /etc/nginx/sites-enabled
$ nano /etc/nginx/sites-enabled/default.conf
default.conf
注:只有启用了Nginx代理缓存集成器插件,以下文件中的静态缓存设置才会起作用。
proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=staticfilecache:180m max_size=500m;
proxy_temp_path /var/lib/nginx/proxy;
proxy_connect_timeout 30;
proxy_read_timeout 120;
proxy_send_timeout 120;
#IMPORTANT - this sets the basic cache key that\'s used in the static file cache.
proxy_cache_key "$scheme://$host$request_uri";
upstream wordpressapache {
#The upstream apache server. You can have many of these and weight them accordingly,
#allowing nginx to function as a caching load balancer
server 127.0.0.1:8080 weight=1 fail_timeout=120s;
}
Per WordPress site conf (对于多站点,您只需要一个vhost)server {
#Only cache 200 responses, and for a default of 20 minutes.
proxy_cache_valid 200 20m;
#Listen to your public IP
listen 80;
#Probably not needed, as the proxy will pass back the host in "proxy_set_header"
server_name www.yoursite.com yoursite.com;
access_log /var/log/nginx/yoursite.proxied.log;
# "combined" matches apache\'s concept of "combined". Neat.
access_log /var/log/apache2/nginx-access.log combined;
# Set the real IP.
proxy_set_header X-Real-IP $remote_addr;
# Set the hostname
proxy_set_header Host $host;
#Set the forwarded-for header.
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
# If logged in, don\'t cache.
if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
set $do_not_cache 1;
}
proxy_cache_key "$scheme://$host$request_uri $do_not_cache";
proxy_cache staticfilecache;
proxy_pass http://wordpressapache;
}
location ~* wp\\-.*\\.php|wp\\-admin {
# Don\'t static file cache admin-looking things.
proxy_pass http://wordpressapache;
}
location ~* \\.(jpg|png|gif|jpeg|css|js|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx)$ {
# Cache static-looking files for 120 minutes, setting a 10 day expiry time in the HTTP header,
# whether logged in or not (may be too heavy-handed).
proxy_cache_valid 200 120m;
expires 864000;
proxy_pass http://wordpressapache;
proxy_cache staticfilecache;
}
location ~* \\/[^\\/]+\\/(feed|\\.xml)\\/? {
# Cache RSS looking feeds for 45 minutes unless logged in.
if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
set $do_not_cache 1;
}
proxy_cache_key "$scheme://$host$request_uri $do_not_cache";
proxy_cache_valid 200 45m;
proxy_cache staticfilecache;
proxy_pass http://wordpressapache;
}
location = /50x.html {
root /var/www/nginx-default;
}
# No access to .htaccess files.
location ~ /\\.ht {
deny all;
}
}
Self Hosted CDN conf
对于自行托管的CDN配置,只需将其设置为提供静态文件,而无需代理传递server {
proxy_cache_valid 200 20m;
listen 80;
server_name yourcdndomain.com;
access_log /srv/www/yourcdndomain.com/logs/access.log;
root /srv/www/yourcdndomain.com/public_html/;
proxy_set_header X-Real-IP $remote_addr;
location ~* \\.(jpg|png|gif|jpeg|css|js|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx)$ {
# Cache static-looking files for 120 minutes, setting a 10 day expiry time in the HTTP header,
# whether logged in or not (may be too heavy-handed).
proxy_cache_valid 200 120m;
expires 7776000;
proxy_cache staticfilecache;
}
location = /50x.html {
root /var/www/nginx-default;
}
# No access to .htaccess files.
location ~ /\\.ht {
deny all;
}
}
现在启动服务器$ /etc/init.d/apache2 restart
$/etc/init.d/nginx start
基准测试结果在Apache Bench上此设置理论上每秒可处理1833.56个请求$ ab -n 1000 -c 20 http://yoursite.com/
对于多站点,请使用最小64MB Ram的Web空间,并在Apache上使用APC和Memcached,缓存不是静态的,您可以毫无问题地使用所有WP函数。您可以通过PageSpeed读取其他选项进行扫描,主题中有编码。缓存可以做得很好,但不能修复坏的主题或插件。另一种解决方案是在WordPress中使用没有Cookie的子域作为CDN。将其添加到wp配置中。php仅用于域上的Cookie,而不用于子域。
define( \'COOKIE_DOMAIN\', \'example.com\' );
现在在函数中设置新函数。php的主题或写一个插件来替换路径形式的静态内容到您的子域,您的自定义CDN。// replace for CDN on bloginfo
if ( !function_exists(\'fb_add_static_wpurl\') ) {
function fb_add_static_wpurl($info, $show) {
if ( is_admin() )
return $info;
$keys = array(
\'url\',
\'wpurl\',
\'stylesheet_url\',
\'stylesheet_directory\',
\'template_url\',
\'template_directory\',
);
if ( in_array( $show, $keys ) ) {
$wpurl = get_bloginfo(\'wpurl\');
$search = array(
$wpurl . \'/wp-content/images/\',
$wpurl . \'/wp-content/download/\',
$wpurl . \'/wp-content/themes/\',
$wpurl . \'/wp-content/plugins/\',
);
$replace = array(
\'http://cdn1.example.com/\',
\'http://cdn2.example.com/\',
\'http://cdn3.example.com/\',
\'http://cdn4.example.com/\',
);
return str_replace( $search, $replace, $info );
} else {
return $info;
}
}
add_filter( \'bloginfo_url\', \'fb_add_static_wpurl\', 9999, 2 );
}
现在,模板和样式表路径的函数function fb_add_static_stylesheet_uri($uri) {
if ( is_admin() )
return $uri;
$wpurl = get_bloginfo(\'wpurl\');
$search = array(
$wpurl . \'/wp-content/images/\',
$wpurl . \'/wp-content/download/\',
$wpurl . \'/wp-content/themes/\',
$wpurl . \'/wp-content/plugins/\',
);
$replace = array(
\'http://cdn1.example.com/\',
\'http://cdn2.example.com/\',
\'http://cdn3.example.com/\',
\'http://cdn4.example.com/\',
);
return str_replace( $search, $replace, $uri );
}
add_filter ( \'template_directory_uri\', \'fb_add_static_stylesheet_uri\' );
add_filter ( \'stylesheet_uri\', \'fb_add_static_stylesheet_uri\' );
add_filter ( \'stylesheet_directory_uri\', \'fb_add_static_stylesheet_uri\' );
现在读取前端静态CDN URL上的页面速度,无需cookie。还将以下源添加到。块副本内容的htaccess:
##
# Explicitly send a 404 header if a file on cdn[0-9].example.org is not
# found. This will prevent the start page (empty URL) from being loaded.
##
RewriteCond %{HTTP_HOST} ^cdn[0-9]\\.example\\.org [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* - [R=404,L]
##
# Do not dispatch dynamic resources via cdn[0-9].example.org.
##
RewriteCond %{HTTP_HOST} ^cdn[0-9]\\.example\\.org [NC]
RewriteCond %{REQUEST_FILENAME} \\.(php|html)$
RewriteRule .* - [R=404,L]
请使用函数,也是示例,您可以用我的想法编写您的解决方案。现代CPU,高内存带宽,因为WordPress主要是内存复制,亚毫秒磁盘访问时间,试试UpCloudhttps://github.com/szepeviktor/debian-server-tools
你怎么set up routing rules 正确使用Nginx 支持WP Super Cache 对于WordPress(3.x)网站?