阿里巴巴开源项目nginx_concat_module部署实例

雷晋
2023-12-01

tengine自带nginx_concat_module插件


ginx_concat_module 是淘宝研发的针对 nginx 的文件合并模块,主要用于合并前端代码减少 http 请求数

如果你的应用环境中部署了 nginx,那么可以考虑尝试此模块减少请求数

编译安装

cd tengine-2.1.1

./configure --with-rtsig_module --with-select_module \
--with-poll_module --with-file-aio --with-ipv6 --with-http_spdy_module \
--with-http_realip_module --with-http_addition_module --with-http_xslt_module \
--with-http_image_filter_module --with-http_geoip_module --with-http_sub_module \
--with-http_dav_module --with-http_flv_module \
--with-http_slice_module --with-http_mp4_module \
--with-http_gunzip_module --with-http_gzip_static_module \
--with-http_auth_request_module --with-http_concat_module \
--with-http_random_index_module --with-http_secure_link_module \
--with-http_degradation_module --with-http_sysguard_module --with-http_dyups_module \
--with-http_perl_module --with-http_lua_module --with-mail --with-mail_ssl_module \
--with-cpp_test_module --with-backtrace_module --with-pcre --with-pcre-jit \
--with-md5-asm --with-sha1-asm --with-libatomic --with-jemalloc --with-debug \
--with-http_concat_module=shared  \    加上即可
&& make && make install

[root@wolf conf]# vi nginx.conf

#basic
user nginx;
worker_processes 4;
pid /var/run/nginx.pid;
worker_rlimit_nofile 102400;
events
{
use epoll;
worker_connections 102300;
}
dso {
   #可以指定模块路径
   #path /usr/local/nginx/modules/
   load ngx_http_concat_module.so;
}
http
{
  include mime.types;
#  default_type application/octet-stream;
  fastcgi_intercept_errors on;
  charset  utf-8;
  server_names_hash_bucket_size 128;


#  tcp_nopush  on;
#  tcp_nodelay on;
#  sendfile on;
 
 
  keepalive_timeout 60;
  keepalive_requests 8192;
  types_hash_max_size 2048;
   
  client_header_buffer_size 16k;
  large_client_header_buffers 4 64k;
  client_body_buffer_size  2m;
  client_max_body_size 1000m;
 
  proxy_connect_timeout 60;
  proxy_read_timeout 1800;
  proxy_send_timeout 1800;
  proxy_buffer_size 256k;
  proxy_buffers 4 256k;
  proxy_busy_buffers_size 256k;
  proxy_temp_file_write_size 256k;
  proxy_http_version 1.1;
  proxy_buffering off;


  gzip on;
  #gzip_disable "msie6";
  gzip_disable "MSIE [1-6]\.";
  gzip_min_length  2k;
  gzip_buffers   4 16k;
  gzip_http_version 1.1;
  gzip_comp_level 2;
gzip_types  text/plain application/javascript text/css;
  #text/plain application/x-javascript text/css application/xml text/html;
  gzip_vary on;


server_tag off;
server_info off;
server_tokens off;


###2012-12-19 change nginx logs
log_format  main  '$http_x_forwarded_for - $remote_user [$time_local] "$http_host" "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent"  $request_time $remote_addr';

##################################################################################################################

#加载vhost
include /etc/nginx/conf.d/*.conf;
include /web/servers/tengine/conf/domains/*.conf;
}


1.nginx需要安装combo插件(nginx_concat_module),配置上开启文件合并功能,文件合并上线配置为100
2.static.wolf.com域下 js, css, tpl资源设置缓存时间缓存时间设置为1天(max-age=86400)
3.配置mime.types文件,将tpl后缀文件配置type为text/html
4.对tpl文件请求的respone设置access-origin打开CORS跨域处理

mkdir -p /web/servers/tengine/conf/domains/
mkdir -p /data/static.wolf.com/


server {
    listen       80;
    server_name  static.wolf.com;
access_log               /data/logs/static.wolf.com_access.log main;
    error_log                /data/logs/static.wolf.com_error.log warn;
add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Headers X-Requested-With;
    add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
    location / {
       root /data/static.wolf.com/;
index  index.html index.htm;        
            add_header  Access-Control-Allow-Origin *;
            add_header  Cache-Control  max-age=86400;
            concat on;
            concat_max_files 100;
            concat_types text/html text/css application/javascript;
        }
    #不允许访问隐藏文件例如.htaccess, .htpasswd, .DS_Store
    location ~ /\. {
    deny all;
    access_log off;
    log_not_found off;
    }
}


参数解释
# /content/ 改成你自己的文件目录
location /content/ {
concat on;                        #启用concat函数
concat_unique off;                #允许返回不同类型的文件内容(js,css同时合并)
concat_delimiter "\n";            #自动在返回每个文件内容的末尾添加换行符
concat_ignore_file_error off;     #不要忽略所合并的文件不存在等错误
}


[root@wolf static.wolf.com]# find / -name mime.types
/usr/local/nginx/conf/mime.types
/root/nginx/tengine-2.1.1/conf/mime.types
[root@wolf static.wolf.com]# vi /usr/local/nginx/conf/mime.types


types {
    text/html                             html htm shtml tpl;


[root@wolf nginx]# /usr/local/nginx/sbin/nginx -t
[root@wolf nginx]# /usr/local/nginx/sbin/nginx -s stop

[root@wolf nginx]# /usr/local/nginx/sbin/nginx

可以进行测试,echo “wolf” >index.html   

http://url/index.html


 类似资料: