说明:
图片、css、js等文件往往会占用掉一个网站大量的服务器带宽和页面载入时间,如果使用nginx做前端服务器可以设置类似的静态文件在客户端的缓存时间。
实现:
# cat /usr/local/nginx/conf/vhost/love.conf //在虚拟主机love.conf最后一个}前添加以下内容
server
{
listen 80;
server_name love.me;
index index.html index.htm index.php default.html default.htm default.php;
root /home/www/data/love;
location ~ .*.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location /{
if (!-e $request_filename) {
rewrite ^([_0-9a-zA-Z-]+)?(/wp-.*) $2 last;
rewrite ^([_0-9a-zA-Z-]+)?(/.*.php)$ $2 last;
rewrite ^ /index.php last;
}
}
location ~ .*.(js|css)?$
{
expires 12h;
}
#添加以下内容
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
access_log off;
}
}
将类似静态文件的客户端缓存时间设置为15天,这样客户在30天内重新访问这些文件时只需要在本地缓存中读取,而不用重新从服务器获取,大大提高了网站访问速度。当然,对于这些静态文件的访问记录计入日志,在一般情况下也是没有意义的,将accss_log设为off,也能在一定程度上降低服务器压力。