西崑雲這篇文章主要介紹了nginx中使用nginx-http-concat模塊合併靜態資源文件,用以加速網站的CSS、JS等靜態資源載入速度,需要的朋友可以參考下
首先了解一下 nginx-http-concat,他是一個淘寶的開源Nginx模塊,是一個能把多個CSS和JS合併成一個請求的Nginx模塊,對於Web性能優化非常有意義。
是不是很神奇,只需要一個請求,就可以把需要的CSS/JS文件通過合併的方式把它輸出成一個文件(注意,雖然淘寶有min格式的文件,但是這裡它僅僅是合併多個文件,而不會自動的對其壓縮打包文件)
首先我們先從Git上下載安裝它
複製代碼代碼如下:
#下載
$ git clone git://github.com/alibaba/nginx-http-concat.git
#移動目錄
$ mv nginx-http-concat /usr/local/src/nginx-http-concat
查看原始Nginx版本,這很重要,因為我們需要安裝同一個版本來升級數據
#查看版本號以及配置信息(目錄/模塊)
$/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.3.1
TLS SNI support disabled
configure arguments: --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
複製代碼代碼如下:
$ wget nginx-1.3.1.tar.gz
$ tar zxvf nginx-1.3.1.tar.gz
$ cd nginx-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 && make install
接下來就是配置你的靜態伺服器conf文件
複製代碼如下:
server {
listen 80;
server_name static.dexphp.loc;
index index.html index.htm;
root /mnt/siteroot/static.dexphp.com;
location /static/css/ {
concat on;
concat_max_files 20;//最大合併文件數量是20個
}
location /status {
stub_status on;
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;
}
特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺「網易號」用戶上傳並發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.