343
技術社區[雲棲]
Nginx/Apache 對圖片,css,js等優化,靜態頁麵設置過期時間
- 圖片,CSS,JS,html設置過期時間
- 不是本域名的重定向到本域名
Nginx 圖片,css,js等優化,靜態頁麵設置過期時間
server{ ... location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d;#圖片緩存30天 } location ~ .*\.(js|css)?$ { expires 12h;#js css緩存12小時 } ... }
以上是在NGINX.CONF裏複製的
我的站靜態頁麵是如下設置的,偽靜態也適用,這種方式是所有.html後綴都試用,如果不需要可以改改
靜態頁麵設置過期時間3600秒,按後退按鈕不會重新訪問
sever{ location / { ....... if ($request_uri ~ ^.+\.html) { add_header Cache-Control max-age=3600,must-revalidate; } if ($host != 'www.lanmps.com') { rewrite ^/(.*)$ https://www.lanmps.com/$1 permanent; } } }
上麵這種情況,如果開發語言重新給HTTP頭部設置過期時間,那麼就會覆蓋此處設置
nginx 設置GZIP壓縮
vim nginx/conf/nginx.conf
gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_vary on; gzip_disable "MSIE [1-6]\.";
Apache 圖片,css,js等優化,靜態頁麵設置過期時間
<VirtualHost *:80> ServerAdmin admin@lanmps.com DocumentRoot "/var/www/html/" ServerName www.lanmps.com:80 #ServerAlias *.lanmps.com ErrorDocument 404 /404.html #ErrorLog /var/www/log/lanmps.com.error.log #CustomLog /var/www/log/lanmps.com.CustomLog common <IfModule mod_expires.c> ExpiresActive On #開啟過期時間功能 ExpiresByType image/gif A2592000 #設置 gif 圖片過期時間 2592000秒 ExpiresByType image/jpeg A2592000 #設置 jpeg 圖片過期時間 2592000秒 ExpiresByType image/png A2592000 #設置 png 圖片過期時間 2592000秒 ExpiresByType application/x-shockwave-flash A2592000 #設置 flash過期時間 2592000秒 ExpiresByType text/css A2592000 #設置 css 過期時間 2592000秒 ExpiresByType application/x-javascript A2592000 ExpiresByType application/javascript A2592000 #設置 js 過期時間 2592000秒 ExpiresByType text/html A6000 #設置 html 過期時間 6000秒 </IfModule> <IfModule mod_rewrite.c> RewriteEngine on #打開rewirte功能 RewriteCond %{HTTP_HOST} !^www.lanmps.com [NC] #聲明Client請求的URL地址中前綴不是www.lanmps.com,其中 [NC] 的意思是忽略大小寫 RewriteRule ^(.*) https://www.lanmps.com/$1 [R=301,L] #含義是如果Client請求的主機中的前綴符合上述條件,則直接進行跳轉到https://www.lanmps.com/, #[R=301] 表示永久重定向 #[L]意味著立即停止重寫操作,並 不再應用其他重寫規則 #.*是指匹配所有URL中不包含換行字符,()括號的功能是把所有的字符做一個標記,以便於後麵的應用.就是引用前麵裏的 (.*)字符 </IfModule> <IfModule mod_deflate.c> SetOutputFilter DEFLATE #必須的,就像一個開關一樣,告訴apache對傳輸到瀏覽器的內容進行壓縮 SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary #設置對後綴gif,jpg,jpeg,png的圖片文件進行壓縮 SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary #同上,就是設置不對exe,tgz,gz。。。的文件進行壓縮 SetEnvIfNoCase Request_URI .(?:pdf|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary AddOutputFilterByType DEFLATE text/* #設置對文件是文本的內容進行壓縮,例如text/html text/css text/plain等 AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript application/javascript application/x-javascript #這段代碼你隻需要了解application/javascript application/x-javascript這段就可以了,這段的意思是對javascript文件進行壓縮 AddOutputFilterByType DEFLATE application/x-httpd-php application/x-httpd-fastphp #這段是告訴apache對php類型的文件進行壓縮 BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.x 有一些問題,所以隻壓縮文件類型是text/html的 BrowserMatch ^Mozilla/4.0[678] no-gzip # Netscape 4.06-4.08 有更多的問題,所以不開啟壓縮 BrowserMatch \bMSIE !no-gzip !gzip-only-text/html # IE瀏覽器會偽裝成 Netscape ,但是事實上它沒有問題 </IfModule> </VirtualHost>
最後更新:2017-04-03 14:54:06