http-concat module to aggregate java-script/css files into a bundle on-the-flyDear friends,
I tried to submit this request to nginx development, but obviously I do not have permission to do this.
There's a wonderful little module written by Joshua Zhu as part of his tengine port of nginx. The module is called &quot;http-concat&quot;. It allows aggregating multiple files of the same mime type into a single bundle, which dramatically reduces request time needed to fetch all files separately. We use it for aggregating Javascript and CSS files. This module is now distributed via the git repository: https://github.com/perusio/nginx-http-concat and it's possible to use it with nginx 1.1.14, which I just verified.
In short, this module can process URLs like: &quot;/jquery/??jquery.asmselect.css,ui.imageuploader.css,jquery.linkselect.css&quot; where all of the files followed after the &quot;??&quot; character sequence in the URL are concatenated and output to the client. It is currently very efficient and small module and does it's little job just fine.
However, there are several issues:
Issue #1: at this moment http-concat cannot take files from locations, where &quot;alias&quot; or &quot;root&quot; directives are used. Example:
location /jquery/ {
try_files $uri $uri/ @common;
}
location @common {
# handle common requests for all sites: /jquery/...., etc. if local version of the requested file is not available
root /opt/www/_common;
}
...
If we attempt to aggregate files from location /jquery, the request results in 404.
I would very much appreciate it if Maxim Dounin or Igor Sysoev or someone knowledgable in nginx internal structures could have a look at the source of http-concat module (URL: https://github.com/perusio/nginx-http-concat/blob/master/ngx_http_concat_module.c) and let me know what is needed to handle locations defined by &quot;root&quot; or &quot;alias&quot;, so files could be concatenated in these locations. This is currently a major issue and a show-stopper for us. I contacted http-concat author, and he told me he currently does not know how to figure out path to files within locations that have &quot;root&quot; or &quot;alias&quot; definitions.
Second issue is that certain CSS files might use the standard &quot;url&quot; tag to point to external images. Unfortunately, when these links are relative, they become relative to the current page where the aggregation happens, but not relative to the CSS file where &quot;url&quot; tags are present. For example: &quot;/jquery/jquery-ui-1.8.17.custom.css&quot; of the standard jquery library contains 8 links to images/.... resolving to /jquery/images/...., and of course these links don't work when we attempt to aggregate jquery-ui-1.8.17.custom.css with other CSS files using http-concat in another folder. Before we used PHP to concatenate CSS/Javascript files, and we wrote the following code to automatically substitute relative urls to images with absolute urls, because absolute urls are known by the script for each CSS file in question:
$uri_path = dirname($css_file['uri']);
// example: url(relative/path/to/file) becomes: url(/absolute/and/not/relative/path/to/file) within a CSS file
$buf = preg_replace('/(:?\s*url\s*\()[\'&quot;\s]*([^\/\'&quot;].*)[\'&quot;\s]*\)/isU', '$1' . ($uri_path == '/' ? '/' : $uri_path . '/') . '$2)', $buf);
It works like a charm, but it's in PHP. Could this be possibly ported to C, provided that nginx already uses PCRE library and so I assume it is somehow possible to implement within http-concat module.
Any patches or suggestions from knowledgable nginx/c experts are highly appreciated. I am sure this module would be beneficial for many. Questions like &quot;Why do you need this module?&quot; or statements like &quot;It is better to concatenate all files in advance and serve them off as a static file&quot; are not welcome.
Anxiously waiting for any hints and best wishes to everybody!
Andrejshttps://forum.nginx.org/read.php?10,222074,222074#msg-222074
Sun, 15 Sep 2019 06:54:36 -0400Phorum 5.2.16https://forum.nginx.org/read.php?10,222074,222074#msg-222074http-concat module to aggregate java-script/css files into a bundle on-the-flyhttps://forum.nginx.org/read.php?10,222074,222074#msg-222074
I tried to submit this request to nginx development, but obviously I do not have permission to do this.

There's a wonderful little module written by Joshua Zhu as part of his tengine port of nginx. The module is called "http-concat". It allows aggregating multiple files of the same mime type into a single bundle, which dramatically reduces request time needed to fetch all files separately. We use it for aggregating Javascript and CSS files. This module is now distributed via the git repository: https://github.com/perusio/nginx-http-concat and it's possible to use it with nginx 1.1.14, which I just verified.

In short, this module can process URLs like: "/jquery/??jquery.asmselect.css,ui.imageuploader.css,jquery.linkselect.css" where all of the files followed after the "??" character sequence in the URL are concatenated and output to the client. It is currently very efficient and small module and does it's little job just fine.

However, there are several issues:

Issue #1: at this moment http-concat cannot take files from locations, where "alias" or "root" directives are used. Example:

location /jquery/ {
try_files $uri $uri/ @common;
}

location @common {
# handle common requests for all sites: /jquery/...., etc. if local version of the requested file is not available
root /opt/www/_common;
}

...

If we attempt to aggregate files from location /jquery, the request results in 404.

I would very much appreciate it if Maxim Dounin or Igor Sysoev or someone knowledgable in nginx internal structures could have a look at the source of http-concat module (URL: https://github.com/perusio/nginx-http-concat/blob/master/ngx_http_concat_module.c) and let me know what is needed to handle locations defined by "root" or "alias", so files could be concatenated in these locations. This is currently a major issue and a show-stopper for us. I contacted http-concat author, and he told me he currently does not know how to figure out path to files within locations that have "root" or "alias" definitions.

Second issue is that certain CSS files might use the standard "url" tag to point to external images. Unfortunately, when these links are relative, they become relative to the current page where the aggregation happens, but not relative to the CSS file where "url" tags are present. For example: "/jquery/jquery-ui-1.8.17.custom.css" of the standard jquery library contains 8 links to images/.... resolving to /jquery/images/...., and of course these links don't work when we attempt to aggregate jquery-ui-1.8.17.custom.css with other CSS files using http-concat in another folder. Before we used PHP to concatenate CSS/Javascript files, and we wrote the following code to automatically substitute relative urls to images with absolute urls, because absolute urls are known by the script for each CSS file in question:

It works like a charm, but it's in PHP. Could this be possibly ported to C, provided that nginx already uses PCRE library and so I assume it is somehow possible to implement within http-concat module.

Any patches or suggestions from knowledgable nginx/c experts are highly appreciated. I am sure this module would be beneficial for many. Questions like "Why do you need this module?" or statements like "It is better to concatenate all files in advance and serve them off as a static file" are not welcome.