Nginx之Concat

先确定当前Nginx版本:

[root@localhost ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.8.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx 
--with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module 
--with-http_gzip_static_module --with-ipv6 --with-http_sub_module 
--with-google_perftools_module

再根据版本号到http://nginx.org/download/下载对应版本的Nginx,下载contcat

[root@localhost ~]# wget http://nginx.org/download/nginx-1.8.0.tar.gz
[root@localhost ~]# wget https://github.com/alibaba/nginx-http-concat/archive/master.zip -O nginx-http-concat-master.zip
[root@localhost ~]# tar -xzvf nginx-1.8.0.tar.gz
[root@localhost ~]# unzip nginx-http-concat-master.zip

由于nginx的mime.types 的js类型是 application/javascript,所以需要加一下此类型:

[root@localhost ~]# vi nginx-http-concat-master/ngx_http_concat_module.c

找到下面的地方,加上一行ngx_string(“application/javascript”),

static ngx_str_t ngx_http_concat_default_types[] = {
    ngx_string("application/javascript"),
    ngx_string("application/x-javascript"),
    ngx_string("text/css"),
    ngx_null_string
};

将concat模块移到usr目录下,copy也可以

[root@localhost ~]# mv nginx-http-concat-master /usr/local/nginx-http-concat

准备完成,接下来开始重新编译Nginx:最前面确定Nginx版本时已给出了原先的编译语句,现在在此基础上加:
–add-module=/usr/local/nginx-http-concat,也就是刚才移到的路径

[root@localhost ~]# cd nginx-1.8.0
[root@localhost  nginx-1.8.0]# ./configure --prefix=/usr/local/nginx 
--with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module 
--with-http_gzip_static_module --with-ipv6 --with-http_sub_module 
--with-google_perftools_module --add-module=/usr/local/nginx-http-concat
[root@localhost nginx-1.8.0]# make && make install

最后编辑站点配置(在需要的地方)

[root@pay nginx-1.8.0]# cd /usr/local/nginx/conf/vhost/
[root@pay vhost]# vi res.kaibuy.cn.conf 

前三项必须定义,其他任意。

location / {                        这里可以是根目录,也可以是指定的子目录。
        concat on;                  打开concat。
        concat_max_files 20;                每个请求最大文件数,默认20
        concat_unique off;              是否只接受在[MIME types]中的相同类型的文件,关闭时,可同时合并js和css
        concat_delimiter "\n";              两个合并的文件中间加的分隔符,默认为空,也就是不加任何内容。
        concat_ignore_file_error on;            是否忽略不存在的文件
        concat_types: text/css application/x-javascript;    定义哪些MIME types是可以被接受,默认ngx_http_concat_module.c中的全部接受
        add_header Access-Control-Allow-Origin *;   是否允许其他域名调用,这不是concat的定义部分,在这顺便一起定义
}
[root@localhost vhost]# lnmp restart

http://res.kq126.pw/1.1/css/??bootstrap.css,common.css,index.css
http://res.kq126.pw/1.1/??css/bootstrap.css,css/common.css,css/index.css
以上两种方法效果一样。也就是只要保证去掉问号后,各自可以组成一个完整可访问地址均可。

mkdir /home/wwwroot/test/concat
cd /home/wwwroot/test/concat
vi a.css
body{background:#abc;}

vi b.css
html{color:#def}

你可能感兴趣的:(nginx)