靜態資源web服務-CDN場景javascript
Nginx資源存儲中心會把靜態資源分發給「北京Nginx」,「湖南Nginx」,「山東Nginx」。php
而後北京User發送靜態資源請求,經過CDN,找到離本身最近的「北京Nginx」。css
文件讀取 sendfilejava
sendfile語法node
on
| off
;http
, server
, location
, if in location
語法解釋:nginx
Enables or disables the use of sendfile().web
Starting from nginx 0.8.12 and FreeBSD 5.2.1, aio can be used to pre-load data for sendfile():bash
配置語法-tcp_nopush服務器
做用:網絡
tcp_nopush語法
語法解釋
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.
Syntax: tcp_nopush on | off;
Default: tcp_nopush off;
Context: http, server, location
配置語法-tcp_nodelay
做用:
tcp_nodelay語法
在 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語法
語法解釋:
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; }
參數解讀:
再次刷新頁面
打開 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; }
參數解讀:
注意:代碼的目錄是 /opt/app/code
訪問/download/test.img 的時候會在 /opt/app/code/download/ 下面去找 test.img.zp 或test.img, 出錯能夠經過查看錯誤日誌
pwd ls
關閉 gzip_static on 後訪問出錯