Nginx作為靜態資源web服務
靜態資源web服務-CDN場景
Nginx資源存儲中心會把靜態資源分發給“北京Nginx”,“湖南Nginx”,“山東Nginx”。
然后北京User發送靜態資源請求,通過CDN,找到離自己最近的“北京Nginx”。
靜態資源核心配置
文件讀取 sendfile
sendfile語法
- Syntax:sendfile
on
|off
; - Default:sendfile off;
- Context:
http
,server
,location
,if in location
語法解釋:
Enables or disables the use of sendfile().
Starting from nginx 0.8.12 and FreeBSD 5.2.1, aio can be used to pre-load data for sendfile():
配置語法-tcp_nopush
作用:
- sendfile開啟的情況下,提高網絡包的傳輸效率。
tcp_nopush語法
- Syntax: tcp_nopush on | off;
- Default:tcp_nopush off;
- Context:http, server, location
語法解釋
Enables or disables the use of the TCP_NOPUSH socket option on FreeBSD or the TCP_CORK socket option on Linux. The options are enabled only when sendfile is used. Enabling the option allows
sending the response header and the beginning of a file in one packet, on Linux and FreeBSD 4.*;
sending a file in full packets.
tcp_nopush指令,在連接套接字時啟用Linux系統下的TCP_CORK。該選項告訴TCP堆棧附加數據包,並在它們已滿或當應用程序通過顯式刪除TCP_CORK指示發送數據包時發送它們。 這使得發送的數據分組是最優量,並且因此提高了網絡數據包的傳輸效率。
也就是說 tcp_nopush=on 時,結果就是數據包不會馬上傳送出去,等到數據包最大時,一次性的傳輸出去,這樣有助於解決網絡堵塞,雖然有一點點延遲。
Syntax: tcp_nopush on | off;
Default: tcp_nopush off;
Context: http, server, location
配置語法-tcp_nodelay
作用:
- keepalive連接下,提高網絡包的傳輸實時性。
tcp_nodelay語法
- Syntax: tcp_nodelay on | off;
- Default: tcp_nodelay on;
- Context: http, server, location
在 keepalive 連接下,提高網絡數據包的傳輸實時性。
tcp_nodelay選項和tcp_nopush正好相反,數據包不等待,實時發送給用戶。
配置語法-gzip壓縮
作用:
- 壓縮傳輸,提高傳輸效率。
- 開啟壓縮,可以加快資源響應速度,同時節省網絡帶寬資源。
gzip語法
Syntax: gzip on | off;
Default: gzip off; Context:
http, server, location, if in location
配置壓縮比,壓縮等級配置(壓縮等級越高,越消耗服務器資源)
Syntax: gzip_comp_level level;
Default: gzip_comp_level 1;
Context: http, server, location
gzip協議版本配置
Syntax: gzip_http_version 1.0 | 1.1;
Default: gzip_http_version 1.1;
Context: http, server, location
壓縮擴展模塊,預讀gzip功能 ngx_http_gzip_static_module
Syntax: gzip_static on | off | always;
Default: gzip_static off;
Context: http, server, location
應用支持gunzip的壓縮方式 ngx_http_gunzip_module
Syntax: gunzip on | off;
Default: gunzip off;
Context: http, server, location
Syntax: gunzip_buffers number size;
Default: gunzip_buffers 32 4k|16 8k;
Context: http, server, location
配置語法-gzip_static
作用:
傳輸預壓縮靜態文件給客戶端(.gz文件為預壓縮)
gzip_static語法
- Syntax: gzip_static on | off | always;
- Default: gzip_static off;
- Context: http, server, location
語法解釋:
Enables (“on”) or disables (“off”) checking the existence of precompressed files. The following directives are also taken into account: gzip_http_version, gzip_proxied, gzip_disable, and gzip_vary.
With the “always” value (1.3.6), gzipped file is used in all cases, without checking if the client supports it. It is useful if there are no uncompressed files on the disk anyway or the ngx_http_gunzip_module is used.
The files can be compressed using the gzip command, or any other compatible one. It is recommended that the modification date and time of original and compressed files be the same.
gzip 壓縮圖片 小案例
sendfile on; location ~ .*\.(jpg|gif|png)$ { #gzip on; #gzip_http_version 1.1; #gzip_comp_level 2; #gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; root /opt/app/code/images; }
訪問
http://192.168.1.112/wei.png
可以看到圖片的大小是 239KB
sendfile on; location ~ .*\.(jpg|gif|png)$ { gzip on; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; root /opt/app/code/images; }
參數解讀:
- gzip on – nginx是否開啟gzip壓縮功能;
- gzip_min_length 1000 – nginx限制最小gzip壓縮資源大小;
- gzip_proxied – nginx作為反向代理時,允許gzip壓縮請求或響應頭里字段;
- gzip_types – nginx允許gzip壓縮的靜態資源類型;
- gzip_http_version 1.1 – nginx允許gzip壓縮的HTTP請求版本;
- gzip_comp_level – nginx允許壓縮的級別,共9個等級,級別越高壓縮率越大;
再次刷新頁面
打開 giz 的功能大小是 182 B
gzip 壓縮文本 小案例
sendfile on; location ~ .*\.(txt|xml)$ { #gzip on; #gzip_http_version 1.1; #gzip_comp_level 1; #gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; root /opt/app/code/doc; }
訪問
http://192.168.1.112/access.txt
可以看到沒開啟 gzip 的大小是 175 kb
開啟 gizp 功能
sendfile on; location ~ .*\.(txt|xml)$ { gzip on; gzip_http_version 1.1; gzip_comp_level 1; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; root /opt/app/code/doc; }
再次刷新頁面
可以看到大小為 182 B
gzip 文件預讀案例演示
開啟gzip 預讀功能
sendfile on; location ~ ^/download { gzip_static on; tcp_nopush on; root /opt/app/code; }
參數解讀:
- gzip_static on – nginx是否開啟預讀gzip文件功能;
- tcp_nopush on – nginx是否一次性發送整個文件,提高傳輸效率;
- root /opt/app/code – 指定根目錄;
注意:代碼的目錄是 /opt/app/code
訪問/download/test.img 的時候會在 /opt/app/code/download/ 下面去找 test.img.zp 或test.img, 出錯可以通過查看錯誤日志
pwd ls
關閉 gzip_static on 后訪問出錯