42121 发表于 2015-11-2 09:33:21

LNMP - nginx配置静态文件过期时间

                      网页中的的    也就缓存,过期时间。
1、编辑虚拟主机配置文件
# vim /usr/local/nginx/conf/vhosts/test.confserver
{
    listen 80;
    server_name www.test.com www.aaa.com;
    if ($host != 'www.test.com')
    {
rewrite ^/(.*)$ http://www.test.com/$1 permanent;
    }
    index index.html index.htm index.php;
    root /data/www;
    access_log /tmp/access.log combined_realip;
    location ~ .*admin\.php$ {
auth_basic "caimz auth";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
      include fastcgi_params;
      fastcgi_pass unix:/tmp/php-fcgi1.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
access_log off;
expires 15d;
    }
    location ~ \.(js|css)
    {
access_log off;
expires 2h;
    }
    location ~ (static|cache)
    {
access_log off;
    }
    location ~ \.php$ {
      include fastcgi_params;
      fastcgi_pass unix:/tmp/php-fcgi1.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
    }
}

2、检测配置文件的正确性
# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
3、重新加载配置文件
# /usr/local/nginx/sbin/nginx -s reload

4、测试
使用chrome浏览器 按下F12,进入调试模式。

刷新知道之前设置的图片。cache-control 大小是1296000 也就是15天。15*60*60=1296000秒
cache-control也就是缓存时间


后者是命令行测试:
# curl -x127.0.0.1:80 'http://www.test.com/static/js/forum.js?mWW' -I
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Sun, 25 Oct 2015 09:18:18 GMT
Content-Type: application/javascript
Content-Length: 22720
Last-Modified: Tue, 09 Jun 2015 17:21:10 GMT
Connection: keep-alive
ETag: "55772086-58c0"
Expires: Sun, 25 Oct 2015 11:18:18 GMT
Cache-Control: max-age=7200               #两个小时
Accept-Ranges: bytes

                   

页: [1]
查看完整版本: LNMP - nginx配置静态文件过期时间