nginx 作为静态资源web服务
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 后访问出错

nginx 作为静态资源web服务的更多相关文章
- Nginx之静态资源WEB服务
本篇主要记录学习Nginx的静态资源WEB服务的几种常见的功能记录学习 Nginx开发常用的命令 nginx -tc /etc/nginx/nginx.conf vim /etc/nginx/conf ...
- Nginx作为静态资源web服务之防盗链
Nginx作为静态资源web服务之防盗链 首先,为什么需要防盗链,因为有些资源存在竞争对手的关系,比如淘宝的商品图片,不会轻易的让工具来爬虫爬走收集.但是如果使用防盗链,需要知道上一个访问的资源,然后 ...
- Nginx作为静态资源web服务之跨域访问
Nginx作为静态资源web服务之跨域访问 首先了解一下什么是跨域 跨域,指的是浏览器不能执行其他网站的脚本.它是由浏览器的同源策略造成的,是浏览器施加的安全限制. 所谓同源是指,域名,协议,端口均相 ...
- Nginx作为静态资源web服务之文件读取
Nginx作为静态资源web服务之文件读取 文件读取会使用到以下几个配置 1. sendfile 使用nginx作为静态资源服务时,通过配置sendfile可以有效提高文件读取效率,设置为on表示启动 ...
- Nginx作为静态资源web服务之缓存原理
Nginx作为静态资源web服务之缓存原理 大致理一下http浏览器缓存原理: 浏览器第一次请求服务器,此时浏览器肯定没有缓存,则直接调用服务器端,服务器在返回的信息的信息头中添加 ETag和Last ...
- Nginx作为静态资源web服务
一.CDN 1.定义: 内容分发的逻辑网络. 2.作用: CDN能做到传输延时的最小化. CDN请求示意图如下: 二.静态资源需要配置的一些语法模块. 1.配置语法 - 文件读取 Syntax : s ...
- 记录Nginx作为静态资源web服务场景配置
server { listen 80; server_name localhost; sendfile on; access_log /var/log/nginx/host.ac ...
- Nginx实践篇(2)- Nginx作为静态资源web服务 - 控制浏览器缓存、防盗链
一.控制浏览器缓存 1. 浏览器缓存简介 浏览器缓存遵循HTTP协议定义的缓存机制(如:Expires;Cache-control等). 当浏览器无缓存时,请求响应流程 当浏览器有缓存时,请求响应流程 ...
- Nginx作为静态资源web服务-跨站访问
一.跨域访问 1.什么是跨域? 参看我的另一篇博客(https://www.cnblogs.com/chrdai/p/11280895.html) 2.为什么浏览器禁止跨域访问? 不安全,容易出现CS ...
随机推荐
- sql 获取某一时段中每一天中最大的时间的一条记录
SELECT *FROM ( SELECT ROW_NUMBER() OVER( PARTITION BY CONVERT(CHAR(10), DataTime, 120) ORDER BY Data ...
- elasticsearch关键词查询不分词
$query = [ 'bool' => [ 'must' => [ 'match_phrase' => ['content' => $word] //$word词不被分词 ] ...
- 常见问题:MySQL/索引
普通索引 最常用,没有任何限制. 唯一索引 必须唯一,但允许空值,如果是组合索引,列值的组合必须唯一. 组合索引 由于MySQL查询时,只能使用一个索引,因此建立组合索引在组合查询的场景下更加有效.组 ...
- laravel 提交空字符串会被转成null解决方法
在app\Http\Kernel.php文件夹中,注释全局中间件: \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull:: ...
- 网络I/O模型
事件驱动模型 与传统编程模式不同,事件驱动程序在启动之后,就在那等待,等待什么呢?等待被事件触发.传统编程下也有“等待”的时候,比如在代码块D中,你定义了一个input(),需要用户输入数据.但这与下 ...
- Linux【Ubuntu】安装docker
内核要大于3.10才能安装docker 查看内核 uname -r 安装yum命令 sudo apt install yum 由于 apt 源使用 HTTPS 以确保软件下载过程中不被篡改,故添加使用 ...
- 浅谈python反序列化漏洞
最近看到p神一篇讲python反序列化的文章,结合redis未授权访问组合漏洞,感觉在flask和redis的构架中比较常见,便记录下来. p神原文:https://www.leavesongs.co ...
- git取消追踪本地某个文件
git rm --cached readme1.txt 删除readme1.txt的跟踪,并保留在本地. git rm --f readme1.txt 删除readme1.txt的跟踪,并 ...
- SpringMVC常用注解@Controller,@Service,@repository,@Component,@Autowired,@Resource,@RequestMapping
1.controller层使用@Controller注解-用于呈现层,(spring-mvc) @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controlle ...
- Java设计模式:23种设计模式(转)
单例(Singleton)模式:某个类只能生成一个实例,该类提供了一个全局访问点供外部获取该实例,其拓展是有限多例模式. 原型(Prototype)模式:将一个对象作为原型,通过对其进行复制而克隆出多 ...