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 ...
随机推荐
- 【DataBase】H2 DateBase的拓展使用
连接模式 支持以下连接模式: 嵌入模式(使用JDBC的本地连接) 服务器模式(使用JDBC或ODBC over TCP / IP进行远程连接) 混合模式(同时本地和远程连接) 嵌入模式 在嵌入模式下, ...
- Linux的桌面虚拟化技术KVM(一)——新建KVM虚拟机
(1).虚拟化产品对比介绍 虚拟化技术有以下三种:仿真虚拟化,这是一种对系统硬件没有要求,但性能最低的虚拟化技术:半虚拟化,这是一种直接使用物理硬件,性能高,但需要修改内核的虚拟化技术:全虚拟化,这是 ...
- 关于Image模块的调色板
参考:https://blog.csdn.net/zhangziju/article/details/79123275 https://blog.csdn.net/qq_21239003/articl ...
- Java以UTF-8格式读写及追加写文件示例
package test; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; im ...
- Ansible安装配置及命令使用详解
Ansible和saltstack目前市面上一些其它的项目管理工具有很大的不同,它的设计初衷就是为了更方便.快捷的进行配置管理.它易于安装和使用.语法也非常简单易学.你可以用Ansible将平常复杂的 ...
- 热修复干货| AndFix热补丁动态修复框架使用教程
本篇文章会与大家一起学习使用阿里的AndFix热修复框架,可以说AndFix是国内热修复技术的开山始祖,尽管现在阿里已经放弃了对这个项目的维护,但是后来很多的热修复技术都借鉴了这一框架的实现思路. 1 ...
- Jenkins控制台乱码修改
原文地址:https://www.jianshu.com/p/8b9df45df401 方案一. 设置jenkins所在服务器环境变量,右键我的电脑→属性→高级系统设置→环境变量,添加JAVA_TOO ...
- zero copy图解
原文链接:https://www.jianshu.com/p/8c6b056f73ce 1 传统的IO读写 传统的IO读写有两种方式:IO终端和DMA.他们各自的原理如下. 1.1 IO中断原理 ...
- 伸缩布局 Flex
其中在webkit内核的浏览器中使用时,必须加上-webkit-前缀,采用Flex布局的元素,称为Flex容器(flex container),简称”容器”.它的所有子元素自动成为容器成员,称为Fle ...
- 用pytorch1.0搭建简单的神经网络:进行多分类分析
用pytorch1.0搭建简单的神经网络:进行多分类分析 import torch import torch.nn.functional as F # 包含激励函数 import matplotlib ...