静态资源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 后访问出错