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 ...
随机推荐
- vs webapi 取消controller
1.添加引用 using Panda.DynamicWebApi; 2.starup.cs public void ConfigureServices(IServiceCollection servi ...
- k8s记录-flanneld+docker网络部署(四)
1)程序准备tar xvf flannel-v0.10.0-linux-amd64.tar.gz mkdir -p /data/projects/common/kubernetes/{bin,cfg, ...
- Python 初级 5 判断再判断
复习: 1 三种数据类型: 整数:int, (1, 2, 200) 浮点数: float(2.0, 9.5, 100.38) 字符串: str("小明", "abc&qu ...
- LeetCode_219. Contains Duplicate II
219. Contains Duplicate II Easy Given an array of integers and an integer k, find out whether there ...
- docker里安装kali linux
docker里安装kali linux 官网镜像 docker search kali docker pull kalilinux/kali-linux-docker vi /etc/apt/sour ...
- 热力图heatmap使用
参考:https://www.cnblogs.com/julinhuitianxia/p/7755246.html 1.首先到echarts官网下载应用实例:http://echarts.baidu. ...
- Can't find bundle for base name javax.servlet.LocalStrings, locale zh_CN
启动junit4测试报错,原因是没有servlet-api.jar eclipse右键项目>>>>>>Build Path>>>>>C ...
- Appium移动自动化测试-----(九) appium API 之应用操作
1.安装应用 方法: installApp() 安装应用到设备中去.需要apk包的路径. driver.installApp("path/to/my.apk"); driver.i ...
- LeetCode 14. 最长公共前缀(Longest Common Prefix)
14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Lee ...
- springboot2.x日志配置记录
springboot日志管理: springboot2.x默认使用commons-logging作为内部日志的输出,日志的实现可以选择Java Util Logging,Log4J2和logback如 ...