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 ...
随机推荐
- windows下新增项目本地通过git bash推送至远程github
本地E盘workspace目录下新增了spring-cloud-alibaba-demo项目,还没有编译过,没有target等不需要推送至git的文件,所以就直接用git bash丢到github了. ...
- linux添加动态库路劲
修改这个文件/etc/ld.so.conf.d,最后加上so的绝对路径即可
- DNS寻址以及IP解析
域名解析从右向左 DNS寻址: 1 客户端发送查询请求,在本地计算机缓存查询,若没有找到,就会将请求发送给dns服务器 2 先发送给本地的dns服务器,现在自己的区域内查找,若找到,根据此记录进行查询 ...
- AI - TensorFlow - 示例05:保存和恢复模型
保存和恢复模型(Save and restore models) 官网示例:https://www.tensorflow.org/tutorials/keras/save_and_restore_mo ...
- 个人博客搭建全记录(Hexo,Github)
搭建过程主要借鉴小歪的博客 博客主题airclod Hexo,Github建站记录 1. 准备 Github账号 注册登陆Github 创建Repository,Repository Name就是Yo ...
- Intellij IDEA 打包jar的多种方式
IDEA打包jar包的多种方式 用IDEA自带的打包形式 用Maven插件maven-shade-plugin打包 用Maven插件maven-assembly-plugin打包 1.view-> ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- Spring Boot 入门(八):集成RabbitMQ消息队列
本片文章续<Spring Boot 入门(七):集成 swagger2>,关于RabbitMQ的介绍请参考<java基础(六):RabbitMQ 入门> 1.增加依赖 < ...
- Gulp-构建工具 相关内容整理
Gulp- 简介 Automate and enhance your workflow | 用自动化构建工具增强你的工作流程 Gulp 是什么? gulp是前端开发过程中一种基于流的代码构建工具,是自 ...
- Word 自动图文集使用方法
1. 自动图文集简介 使用自动图文集当你在文档中输入你所需的模板名称后,就能立刻变出该内容出来. 1.1 效果演示 1:个人简历 如下图所示,在Word文档中输入了"个人简历"后, ...