网站首页 > 技术教程 正文
一、介绍
Nginx对静态资源的压缩就是在服务端进行压缩传输到浏览器端进行解压,这个压缩和解压的过程中减少中间网络传输的消耗。就是减少服务端带宽资源的消耗还有减少传输的文件大小从而实现传输的实时性。
对于压缩我们可以启用Nginx的gizp压缩设置。
二、gizp配置
#开启gzip gzip on; # gzip 压缩级别 gzip_comp_level 2; # 启用gzip压缩的最小文件 gzip_min_length 1k; # 进行压缩的文件类型。 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;
更多设置参考:Nginx文档gzip相关参数设置
三、演示
首先我在服务器准备好了演示使用的图片demo.jpg
接下来我们前往/etc/nginx/conf.d/新建test.conf进行设置
server { listen 80; server_name localhost; sendfile on; #charset koi8-r; access_log /var/log/nginx/host.access.log main; location ~ .*\.(jpg|gif|png)$ { gzip off; 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/demo/images; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 404 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
此时设置gzip off 我们访问该图片:
该图片资源大小为749KB
接下来我们开启gzip
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/demo/images; }
reload nginx:
nginx -s reload -c /etc/nginx/nginx.conf
再次访问该图片:
现在该图片传输资源大小被压缩为747KB
可以看到确实有压缩了但是似乎压缩的比并不理想
事实上gzip对文本的压缩更为显著,对图片的压缩比率并不是很理想
我们同样对文本文件压缩设置来测试一下压缩的比例:
我们在text.conf中增加这一段location ~ .*\.(txt|xml)$ { gzip off; 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/demo/doc; }
然后在/opt/app/demo/doc 中准备好文本文件
reload Nginx后在gzip off时访问该文本文件:
此时传输资源大小为1.3MB
我们将gzip打开后reload Nginx再次访问该文本:
可见此时传输资源大小为12.4KB
说明gzip对文本的压缩是非常理想的
对比两次结果从Time第一次为9.09s到第二次50ms可以看出Nginx开启gzip压缩大大提高了网页响应的速度。
作者:唐龙隆
链接:https://www.jianshu.com/p/67075626774c
猜你喜欢
- 2024-10-11 Docker四种网络模式(Bridge,Host,Container,...
- 2024-10-11 HTTP 头字段 Origin、Host 和 Referer 有什么区别?
- 2024-10-11 每日漏洞 | Host头攻击(http头hostname攻击漏洞)
- 2024-10-11 Docker容器实战之数据持久化+网络模式+资源限制
- 2024-09-19 Nginx Proxy Manager - Docker 建站最佳伴侣
- 2024-09-19 高性能负载均衡:nginx搭建tomcat集群
- 2024-09-19 Nginx路由匹配规则location的小总结
- 2024-09-19 使用 nginx 反向代理多个 docker 容器
- 2024-09-19 Nginx配置虚拟主机的三种方式(nginx虚拟主机配置文件)
- 2024-09-19 如何控制Nginx并发连接数(如何控制nginx并发连接数量)
你 发表评论:
欢迎- 最近发表
-
- Linux新手必看:几种方法帮你查看CPU核心数量
- linux基础命令之lscpu命令(linux中ls命令的用法)
- Linux lscpu 命令使用详解(linux常用ls命令)
- 如何查询 Linux 中 CPU 的数量?这几个命令要知道!
- 在linux上怎么查看cpu信息(linux如何查看cpu信息)
- 查看 CPU 的命令和磁盘 IO 的命令
- 如何在CentOS7上改变网卡名(centos怎么改网卡名字)
- 网工必备Linux网络管理命令(网工必备linux网络管理命令是什么)
- Linux 网络命令知多少(linux 网络 命令)
- Linux通过命令行连接wifi的方式(linux命令行连接无线网)
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)