nginx中使用nginx-http-concat模块合并静态资源文件
2023-06-13 09:15:32 时间
首先了解一下nginx-http-concat,他是一个淘宝的开源Nginx模块,是一个能把多个CSS和JS合并成一个请求的Nginx模块,对于Web性能优化非常有意义。
Github地址:https://github.com/alibaba/nginx-http-concat,
先看看淘宝用起来是什么样的,访问淘宝网主页,查看源代码可以看到类似的这样的style/script链接
<linkrel="stylesheet"href="//g.tbcdn.cn/??tb/global/2.1.6/global-min.css,tb/tb-fp/1.2.3/style-min.css?t=20130912">
<scriptsrc="//g.tbcdn.cn/??kissy/k/1.3.1/seed-min.js,tb/global/2.1.6/global-min.js,tb/tb-fp/1.2.3/core-min.js?t=20130912"></script>
是不是很神奇,只需要一个请求,就可以把需要的CSS/JS文件通过合并的方式把他输出成一个文件(注意,虽然淘宝有min格式的文件,但是这里它仅仅是合并多个文件,而不会自动的对其压缩打包文件)
首先我们先从Git上下载安装它
#下载
$gitclonegit://github.com/alibaba/nginx-http-concat.git
#移动目录
$mvnginx-http-concat/usr/local/src/nginx-http-concat
查看原始Nginx版本,这很重要,因为我们需要安装同一个版本来升级数据
#查看版本号以及配置信息(目录/模块)
$/usr/local/nginx/sbin/nginx-V
nginxversion:nginx/1.3.1
TLSSNIsupportdisabled
configurearguments:--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_module--with-http_gzip_static_module
根据查询的版本号下载对应版本的nginx,可以到官方下载指定版本:http://nginx.org/download/
我这里使用的是1.3.1
$wgetnginx-1.3.1.tar.gz
$tarzxvfnginx-1.3.1.tar.gz
$cdnginx-1.3.1
#根据上面-V的信息加入concat模块所在路径(--add-module=/usr/local/src/nginx-http-concat)进行编译
$./configure--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_module--with-http_gzip_static_module--add-module=/usr/local/src/nginx-http-concat
make之前备份配置文件,防止意外
$cp-r/usr/local/nginx/conf/root/nginxconf
#编译安装
$make&&makeinstall
接下来就是配置你的静态服务器conf文件
server{
listen 80;
server_namestatic.dexphp.loc;
indexindex.htmlindex.htm;
root /mnt/siteroot/static.dexphp.com;
location/static/css/{
concaton;
concat_max_files20;//最大合并文件数量是20个
}
location/status{
stub_statuson;
access_log off;
}
location~.*\.(gif|jpg|jpeg|png|bmp|swf|js)${
expires off;
}
error_log /mnt/siteroot/wwwlogs/static.dexphp.loc.error.log;
access_log /mnt/siteroot/wwwlogs/static.dexphp.loc.access.log;
}
相关文章
- Golang框架Gin入门实战--(4)HTML模板渲染以及模板语法 自定义模板函数 静态文件服务(下)
- web服务 访问Linux中的静态文件
- PHP执行Shell脚本或Bash脚本文件并返回命令输出详情
- 递归删除指定文件-目录
- C语言小项目 -- 通讯录(静态版+动态版+文件版)
- Typecho生成静态首页index.html文件
- Linux文件权限设置教程详解程序员
- Linux恢复已删除文件的技巧(linux还原删除文件)
- Java数据持久层框架 MyBatis之API学习六(Mapper XML 文件详解)编程语言
- 使用Linux vi修改文件的快捷方式(linuxvi修改文件)
- 如何静态编译Linux系统并实现文件独立,省去运行时动态链接的麻烦?(静态编译linux)
- 精通Linux文件处理命令,掌握数据管理能力(linux 文件处理命令)
- MySQL数据库中的log文件解析(mysql中log文件)
- JavaBean实现多文件上传的两种方法
- php下载远程文件类(支持断点续传)
- C#对文件与文件夹的操作包括删除、移动与复制
- PHP中使用smarty生成静态文件的例子
- 采用thinkphp自带方法生成静态html文件详解
- PHP合并静态文件详解
- 使用nodejs、Python写的一个简易HTTP静态文件服务器
- nginx支持.htaccess文件实现伪静态的方法分享