博文大綱:
1)靜態資源類型
2)靜態資源場景
3)靜態資源配置語法
4)靜態資源文件壓縮
5)靜態資源瀏覽器緩存
6)靜態資源防盜鏈
1)靜態資源類型
Nginx作爲靜態資源web服務器部署配置,傳輸非常的高效,常常用於靜態資源處理、請求、動靜分離!
非服務器動態運行生成的文件屬於靜態資源!
類型 | 種類 |
---|---|
瀏覽器端渲染 | HTML、CSS、JS |
圖片 | JPEG、GIF、PNG |
視頻 | FLV、MP4 |
文件 | TXT、任意下載文件 |
2)靜態資源場景
靜態資源傳輸延遲最小化!
如圖:
3)靜態資源配置語法
1)文件讀取高效——>sendfile
Syntax:sendfile on | off ;
Default:sendfile off ;
Context:http、server、location、if in location ;
2)提高網絡傳輸效率——>nopush
Syntax:tcp_nopush on | off ;
Default:tcp_nopush off ;
Context:http、server、location ;
作用: sendfile開啓情況下, 提⾼⽹絡包的'傳輸效率';
3)與 tcp_nopush 之對應的配置 tcp_nodelay
Syntax: tcp_nodelay on | off ;
Default: tcp_nodelay on ;
Context:http, server, location ;
作⽤: 在keepalive連接下,提⾼⽹絡的傳輸‘實時性’;
4)靜態資源文件壓縮
Nginx 將響應報⽂發送⾄客戶端之前可以啓⽤壓縮功能,這能夠有效地節約帶寬,並提⾼響應⾄客戶端的速度。
1)gzip壓縮配置語法
Syntax: gzip on | off ;
Default: gzip off ;
Context: http, server, location, if in location ;
作⽤:傳輸壓縮;
2)gzip壓縮比率配置語法
Syntax: gzip_comp_level level ;
Default: gzip_comp_level 1 ;
Context: http, server, location ;
作⽤:壓縮本身⽐較耗費服務端性能 ;
3)gzip壓縮協議版本
Syntax: gzip_http_version 1.0 | 1.1 ;
Default: gzip_http_version 1.1 ;
Context: http, server, location ;
作⽤: 壓縮使⽤在http哪個協議, 主流版本1.1 ;
4)擴展壓縮模塊
Syntax: gzip_static on | off | always ;
Default: gzip_static off ;
Context: http, server, location ;
作⽤: 預讀gzip功能 ;
5)圖片壓縮案例
[root@nginx ~]# mkdir -p /soft/code/images
[root@nginx ~]# vim /etc/nginx/conf.d/static_server.conf
server {
listen 80;
server_name 192.168.1.10;
sendfile on;
access_log /var/log/nginx/static_access.log main;
location ~ .*\.(jpg|gif|png)$ {
gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/json application/x-javascript app lication/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png;
root /soft/code/images;
}
}
[root@nginx ~]# nginx -t
[root@nginx ~]# nginx -s reload
由於圖片的壓縮效果不是太好,所以這裏只附上配置!
6)文件壓縮案例
[root@nginx ~]# mkdir -p /soft/code/doc
[root@nginx ~]# vim /etc/nginx/conf.d/static_server.conf
server {
listen 80;
server_name 192.168.1.10;
sendfile on;
access_log /var/log/nginx/static_access.log main;
location ~ .*\.(txt|xml)$ {
gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/json application/x-javascript app lication/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png;
root /soft/code/doc;
}
}
[root@nginx ~]# nginx -t
[root@nginx ~]# nginx -s reload
1)沒有啓用gzip文件壓縮
2)啓用gzip文件壓縮
5)靜態資源瀏覽器緩存
HTTP協議定義的緩存機制(如: Expires; Cache-control 等)
1)瀏覽器無緩存
瀏覽器請求——>無緩存——>請求web服務器——>請求響應——>呈現
2)瀏覽器有緩存
瀏覽器請求->有緩存->校驗過期->是否有更新->呈現
校驗是否過期 Expires HTTP1.0, Cache-Control(max-age) HTTP1.1
協議中Etag頭信息校驗 Etag ()
Last-Modified頭信息校驗 Last-Modified (具體時間)
1)緩存配置語法expires
Syntax: expires [modified] time ;
Default: expires off ;
Context: http, server, location, if in location ;
作⽤: 添加Cache-Control Expires頭 ;
2)配置靜態資源緩存
[root@nginx conf.d]# vim static_server.conf
server {
listen 80;
server_name 192.168.1.10;
sendfile on;
access_log /var/log/nginx/static_access.log main;
location ~ .*\.(txt|js|css|html)$ {
root /soft/code/doc;
expires 1h;
}
location ~ .*\.(jpg|gif|png)$ {
root /soft/code/images;
expires 7d;
}
}
[root@nginx conf.d]# nginx -t
[root@nginx conf.d]# nginx -s reload
瀏覽器測試效果:
3)配置靜態文件不緩存
[root@nginx conf.d]# vim static_server.conf
server {
listen 80;
server_name 192.168.1.10;
sendfile on;
access_log /var/log/nginx/static_access.log main;
location ~ .*\.(txt|js|css|html)$ {
root /soft/code/doc;
add_header Cache-Control no-store;
add_header Pragma no-cache;
}
location ~ .*\.(jpg|gif|png)$ {
root /soft/code/images;
expires 7d;
}
}
[root@nginx conf.d]# nginx -t
[root@nginx conf.d]# nginx -s reload
瀏覽器測試效果:
6)靜態資源防盜鏈
盜鏈指的是在⾃⼰的界⾯展示不在⾃⼰服務器上的內容,通過技術⼿段獲得他⼈服務器的資源地址,繞過別⼈資源展示⻚⾯,在⾃⼰⻚⾯向⽤戶提供此內容,從⽽減輕⾃⼰服務器的負擔,因爲真實的空間和流量來⾃別⼈服務器;
防盜鏈設置思路: 區別哪些請求是⾮正常⽤戶請求;
基於http_refer 防盜鏈配置模塊:
Syntax: valid_referers none | blocked | server_names | string ...;
Default: —
Context: server, location
另一臺nginx服務器(初始的情況),編寫html文件:
[root@nginx02 conf.d]# vim /usr/share/nginx/html/dl.html
<html>
<head>
<meta charset="utf-8">
<title>pachong</title>
</head>
<body style="background-color:red;">
<img src="http://192.168.1.10/test.jpg">
</body>
</html>
[root@nginx02 conf.d]# nginx -t
[root@nginx02 conf.d]# nginx -s reload
第一臺服務正常訪問:
第二胎服務器盜用第一臺服務器的圖片:
2)啓用防盜鏈
[root@nginx conf.d]# vim static.conf
server {
listen 80;
server_name 192.168.1.10;
valid_referers none blocked 192.168.1.10;
location ~ .*\.(jpg|gif|png)$ {
if ($invalid_referer) {
return 403;
break;
}
root /soft/code/images;
}
}
[root@nginx conf.d]# nginx -t
[root@nginx conf.d]# nginx -s reload
再次訪問第二臺服務器:
——————————本文到此結束,感謝閱讀————————