12.12 靜態文件不記錄日誌和過時時間

靜態文件不記錄日誌和過時時間目錄概要

  • 配置以下
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
          expires      7d;
          access_log off;
    }
location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }

靜態文件不記錄日誌和過時時間

  • 在配置文件中添加
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$    //匹配gif|jpg|jpeg|png|bmp|swf 後綴的文件
    {
          expires      7d;        //7天后過時
          access_log off;        //匹配「.*.(gif|jpg|jpeg|png|bmp|swf) 」關閉記錄日誌
    }
location ~ .*\.(js|css)$
    {
          expires      12h;        //12個小時後過時
          access_log off;        //匹配「.*.(js|css) 」關閉記錄日誌
    }
  1. 打開虛擬主機配置文件vim /usr/local/nginx/conf/vhost/test.com.conf
[root@hanfeng vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf

server
{
    listen 80;
    server_name test.com test1.com test2.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
     if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
          expires      7d;
          access_log off;
    }     
location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }     
    access_log /tmp/test.com.log combined_realip;
}   
保存退出
  1. 檢查配置文件語法錯誤,並從新加載配置文件
[root@hanfeng vhost]# /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
[root@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng vhost]#
  1. 測試,先來模擬一個圖片
[root@hanfeng vhost]# cd /data/wwwroot/test.com/
[root@hanfeng test.com]# ls
admin  index.html
[root@hanfeng test.com]# vim 1.gif        在1.gif隨意寫入一些內容
[root@hanfeng test.com]# vim 2.js
[root@hanfeng test.com]#
  1. 接下來作一個訪問測試
[root@hanfeng test.com]# curl -x127.0.0.1:80 test.com/1.gif
sdafasf
[root@hanfeng test.com]# curl -x127.0.0.1:80 test.com/2.js
fghdfsd
[root@hanfeng test.com]# curl -x127.0.0.1:80 test.com/index.html
「test.com」
[root@hanfeng test.com]#
  1. 查看日誌,會看到只有一條日誌
[root@hanfeng test.com]# cat /tmp/test.com.log
127.0.0.1 - [05/Jan/2018:00:17:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@hanfeng test.com]#
  1. 測試過時時間,加上-I參數
[root@hanfeng test.com]# curl -x127.0.0.1:80 -I test.com/2.js
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 16:22:07 GMT
Content-Type: application/javascript
Content-Length: 8
Last-Modified: Thu, 04 Jan 2018 16:15:42 GMT
Connection: keep-alive
ETag: "5a4e532e-8"
Expires: Fri, 05 Jan 2018 04:22:07 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes

[root@hanfeng test.com]#
  • max-age=43200 過時時間
  1. 若是去掉expires,則不會顯示max-age過時時間
相關文章
相關標籤/搜索