├── .github
└── FUNDING.yml
├── README.md
└── lua
└── minify
├── minify.lua
└── minify_header.lua
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: [C0nw0nk]# Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4 | patreon: # Replace with a single Patreon username
5 | open_collective: # Replace with a single Open Collective username
6 | ko_fi: # Replace with a single Ko-fi username
7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9 | liberapay: # Replace with a single Liberapay username
10 | issuehunt: # Replace with a single IssueHunt username
11 | otechie: # Replace with a single Otechie username
12 | custom: ['https://paypal.me/wimbledonfc','https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=ZH9PFY62YSD7U&source=url']
13 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Nginx-Lua-minification-library
2 | A compression and minification library to minify static or dynamic assets like HTML PHP outputs CSS style sheets JS Javascript all text/html or plain text mime types that nginx can output that the browser will read so they are small compressed and have white space removed as well as helping reduce bandwidth consumption since the files served from nginx webserver are now smaller.
3 |
4 | > Please note, this script will only remove comments, white spaces and breaklines from your code. So, this mean it will not "rebuild" your files in order to minify the code. If you want something similar, I recommend using [Pagespeed](https://developers.google.com/speed/pagespeed/module/ "Pagespeed") from Google
5 |
6 | # Information :
7 |
8 | I built this script to compress and keep the specified mime types outputs small and minify the bandwidth that my servers have to use when serving these files to users.
9 |
10 | If you have any bugs issues or problems just post a Issue request.
11 |
12 | https://github.com/C0nw0nk/Nginx-Lua-minification-library/issues
13 |
14 | If you fork or make any changes to improve this or fix problems please do make a pull request for the community who also use this.
15 |
16 | https://github.com/C0nw0nk/Nginx-Lua-minification-library/pulls
17 |
18 | # Usage :
19 |
20 | Edit settings inside `minify.lua` to add your own mime types or improve my regex. (Please share your soloutions and additions)
21 |
22 | https://github.com/C0nw0nk/Nginx-Lua-minification-library/blob/master/lua/minify/minify.lua#L69
23 |
24 | Add this to your Nginx configuration folder.
25 |
26 | `nginx/conf/lua/minify`
27 |
28 | Once installed into your `nginx/conf/` folder.
29 |
30 | Add this to your HTTP block or it can be in a server or location block depending where you want this script to run for individual locations the entire server or every single website on the server.
31 |
32 | ```
33 | header_filter_by_lua_file conf/lua/minify/minify_header.lua
34 | body_filter_by_lua_file conf/lua/minify/minify.lua;
35 | ```
36 |
37 | ### Example nginx.conf :
38 |
39 | This will run for all websites on the nginx server
40 |
41 | ```
42 | http {
43 | #nginx config settings etc
44 | header_filter_by_lua_file conf/lua/minify/minify_header.lua
45 | body_filter_by_lua_file conf/lua/minify/minify.lua;
46 | #more config settings and some server stuff
47 | }
48 | ```
49 |
50 | This will make it run for this website only
51 |
52 | ```
53 | server {
54 | #nginx config settings etc
55 | header_filter_by_lua_file conf/lua/minify/minify_header.lua
56 | body_filter_by_lua_file conf/lua/minify/minify.lua;
57 | #more config settings and some server stuff
58 | }
59 | ```
60 |
61 | This will run in this location block only
62 |
63 | ```
64 | location / {
65 | #nginx config settings etc
66 | header_filter_by_lua_file conf/lua/minify/minify_header.lua
67 | body_filter_by_lua_file conf/lua/minify/minify.lua;
68 | #more config settings and some server stuff
69 | }
70 | ```
71 |
72 |
73 | # Building your own regex rules to apply inside this script :
74 |
75 | Using my code examples below copy and paste them to test and edit and build your own regex on the Lua demo site here :
76 |
77 | https://www.lua.org/cgi-bin/demo
78 |
79 | #### Examples :
80 |
81 | ##### Basic :
82 | ```
83 | local string = ""
84 | local regex = ""
85 | local replace_with = ""
86 | local output = string.gsub(string, regex, replace_with)
87 | print(output)
88 | ```
89 |
90 | ##### Advanced :
91 | ```
92 | local add_to_string = [[added me!]]
93 | local string = [[]] .. add_to_string .. [[
94 | hello world!
95 | ]]
96 | local regex = ""
97 | local replace_with = ""
98 | local output = string.gsub(string, regex, replace_with)
99 | print(output)
100 | ```
101 |
102 | # Requirements :
103 | NONE! :D You only need Nginx + Lua to use my scripts.
104 |
105 | ###### Where can you download Nginx + Lua ?
106 |
107 | Openresty provide Nginx + Lua builds for Windows Linux etc here.
108 |
109 | https://openresty.org/en/download.html
110 |
111 | Nginx4windows has Windows specific builds with Lua here.
112 |
113 | http://nginx-win.ecsds.eu/
114 |
115 | Or you can download the source code for Nginx here and compile Nginx yourself with Lua.
116 |
117 | https://nginx.org/en/download.html
118 |
119 | # About :
120 |
121 | I was inspired to create this because of Cloudflare feature "Auto Minify" https://www.cloudflare.com/
122 | ```
123 | Auto Minify
124 |
125 | Reduce the file size of source code on your website.
126 |
127 | What does Auto Minify do?
128 |
129 | Auto Minify removes unnecessary characters from your source code (like whitespace, comments, etc.) without changing its functionality.
130 |
131 | Minification can compress source file size which reduces the amount of data that needs to be transferred to visitors and thus improves page load times.
132 | ```
133 |
134 | I love that feature so much ontop of having it enabled on all my Cloudflare proxied sites I decided to make it into a feature on my own servers so the traffic I send to cloudflare is also reduced in bandwidth too! (Every little helps right!)
135 |
136 | Thank you to @Cloudflare for the inspiration and your community for all the love, A big thanks to the @openresty community you guys rock Lua rocks you are all so awesome!
137 |
138 | Lets build a better internet together! Where Speed, Privacy, Security and Compression matter!
139 |
140 | Here are links to my favorite communities :)
141 |
142 | http://openresty.org/en/
143 |
144 | https://community.cloudflare.com/
145 |
--------------------------------------------------------------------------------
/lua/minify/minify.lua:
--------------------------------------------------------------------------------
1 | --[[
2 | Introduction and details :
3 |
4 | Copyright Conor Mcknight
5 |
6 | https://github.com/C0nw0nk/Nginx-Lua-minification-library
7 |
8 | Disclaimer :
9 | I am not responsible for what you do with this script nor liable,
10 | This script was released under default Copyright Law as a proof of concept.
11 | For those who want to know what that means for your use of this script read the following : http://choosealicense.com/no-license/
12 |
13 | Information :
14 | I built this script to compress and keep the specified mime types outputs small and minify the bandwidth that my servers have to use when serving these files to users.
15 |
16 | If you have any bugs issues or problems just post a Issue request. https://github.com/C0nw0nk/Nginx-Lua-minification-library/issues
17 |
18 | If you fork or make any changes to improve this or fix problems please do make a pull request for the community who also use this. https://github.com/C0nw0nk/Nginx-Lua-minification-library/pulls
19 |
20 | Usage :
21 |
22 | Add this to your Nginx configuration folder.
23 |
24 | nginx/conf/lua/minify
25 |
26 | Once installed into your nginx/conf/ folder.
27 |
28 | Add this to your HTTP block or it can be in a server or location block depending where you want this script to run for individual locations the entire server or every single website on the server.
29 |
30 |
31 | header_filter_by_lua_file conf/lua/minify/minify_header.lua
32 | body_filter_by_lua_file conf/lua/minify/minify.lua;
33 |
34 | Example nginx.conf :
35 |
36 | This will run for all websites on the nginx server
37 | http {
38 | #nginx config settings etc
39 | header_filter_by_lua_file conf/lua/minify/minify_header.lua
40 | body_filter_by_lua_file conf/lua/minify/minify.lua;
41 | #more config settings and some server stuff
42 | }
43 |
44 | This will make it run for this website only
45 | server {
46 | #nginx config settings etc
47 | header_filter_by_lua_file conf/lua/minify/minify_header.lua
48 | body_filter_by_lua_file conf/lua/minify/minify.lua;
49 | #more config settings and some server stuff
50 | }
51 |
52 | This will run in this location block only
53 | location / {
54 | #nginx config settings etc
55 | header_filter_by_lua_file conf/lua/minify/minify_header.lua
56 | body_filter_by_lua_file conf/lua/minify/minify.lua;
57 | #more config settings and some server stuff
58 | }
59 |
60 | ]]
61 |
62 | --[[
63 | Settings used to modify and compress each invidual mime type output you specify on the fly.
64 |
65 | I decided to make it as easy to use and customisable as possible to help the community that will use this.
66 | ]]
67 | local content_type_list = {
68 | {
69 | "text/html",
70 | {
71 | --[[
72 | Usage :
73 | Regex, Replacement
74 | Text, Replacement
75 | ]]
76 | --Example :
77 | --{"replace me", " with me! ",},
78 | {"", "",}, --remove nulled out html example !! I DO NOT RECOMMEND REMOVING COMMENTS, THIS COULD BREAK YOUR ENTIRE WEBSITE FOR OLD BROWSERS, BE AWARE
79 | {"(//[^.*]*.\n)", "",}, -- Example: this //will remove //comments (result: this remove)
80 | {"(/%*[^*]*%*/)", "",}, -- Example: this /*will*/ remove /*comments*/ (result: this remove)
81 | {"", "",},
82 | {"", "",},
83 | {"%s%s+", "",}, --remove blank characters from html
84 | {"[ \t]+$", "",}, --remove break lines (execution order of regex matters keep this last)
85 | }
86 | },
87 | {
88 | "text/css",
89 | {
90 | --[[
91 | Usage :
92 | Regex, Replacement
93 | ]]
94 | {"(//[^.*]*.\n)", "",},
95 | {"(/%*[^*]*%*/)", "",},
96 | {"%s%s+", "",},
97 | {"[ \t]+$", "",},
98 | }
99 | },
100 | --javascript has allot of different mime types i don't know why!?
101 | {
102 | "application/javascript",
103 | {
104 | --[[
105 | Usage :
106 | Regex, Replacement
107 | ]]
108 | {"(//[^.*]*.\n)", "",},
109 | {"(/%*.*[^*].*%*/)", "",},
110 | {"%s%s+", "",},
111 | {"[ \t]+$", "",},
112 | }
113 | },
114 | {
115 | "application/ecmascript",
116 | {
117 | --[[
118 | Usage :
119 | Regex, Replacement
120 | ]]
121 | {"(//[^.*]*.\n)", "",},
122 | {"(/%*[^*]*%*/)", "",},
123 | {"%s%s+", "",},
124 | {"[ \t]+$", "",},
125 | }
126 | },
127 | {
128 | "application/x-ecmascript",
129 | {
130 | --[[
131 | Usage :
132 | Regex, Replacement
133 | ]]
134 | {"(//[^.*]*.\n)", "",},
135 | {"(/%*[^*]*%*/)", "",},
136 | {"%s%s+", "",},
137 | {"[ \t]+$", "",},
138 | }
139 | },
140 | {
141 | "application/x-javascript",
142 | {
143 | --[[
144 | Usage :
145 | Regex, Replacement
146 | ]]
147 | {"(//[^.*]*.\n)", "",},
148 | {"(/%*[^*]*%*/)", "",},
149 | {"%s%s+", "",},
150 | {"[ \t]+$", "",},
151 | }
152 | },
153 | {
154 | "text/ecmascript",
155 | {
156 | --[[
157 | Usage :
158 | Regex, Replacement
159 | ]]
160 | {"(//[^.*]*.\n)", "",},
161 | {"(/%*[^*]*%*/)", "",},
162 | {"%s%s+", "",},
163 | {"[ \t]+$", "",},
164 | }
165 | },
166 | {
167 | "text/javascript",
168 | {
169 | --[[
170 | Usage :
171 | Regex, Replacement
172 | ]]
173 | {"(//[^.*]*.\n)", "",},
174 | {"(/%*[^*]*%*/)", "",},
175 | {"%s%s+", "",},
176 | {"[ \t]+$", "",},
177 | }
178 | },
179 | {
180 | "text/javascript1.0",
181 | {
182 | --[[
183 | Usage :
184 | Regex, Replacement
185 | ]]
186 | {"(//[^.*]*.\n)", "",},
187 | {"(/%*[^*]*%*/)", "",},
188 | {"%s%s+", "",},
189 | {"[ \t]+$", "",},
190 | }
191 | },
192 | {
193 | "text/javascript1.1",
194 | {
195 | --[[
196 | Usage :
197 | Regex, Replacement
198 | ]]
199 | {"(//[^.*]*.\n)", "",},
200 | {"(/%*[^*]*%*/)", "",},
201 | {"%s%s+", "",},
202 | {"[ \t]+$", "",},
203 | }
204 | },
205 | {
206 | "text/javascript1.2",
207 | {
208 | --[[
209 | Usage :
210 | Regex, Replacement
211 | ]]
212 | {"(//[^.*]*.\n)", "",},
213 | {"(/%*[^*]*%*/)", "",},
214 | {"%s%s+", "",},
215 | {"[ \t]+$", "",},
216 | }
217 | },
218 | {
219 | "text/javascript1.3",
220 | {
221 | --[[
222 | Usage :
223 | Regex, Replacement
224 | ]]
225 | {"(//[^.*]*.\n)", "",},
226 | {"(/%*[^*]*%*/)", "",},
227 | {"%s%s+", "",},
228 | {"[ \t]+$", "",},
229 | }
230 | },
231 | {
232 | "text/javascript1.4",
233 | {
234 | --[[
235 | Usage :
236 | Regex, Replacement
237 | ]]
238 | {"(//[^.*]*.\n)", "",},
239 | {"(/%*[^*]*%*/)", "",},
240 | {"%s%s+", "",},
241 | {"[ \t]+$", "",},
242 | }
243 | },
244 | {
245 | "text/javascript1.5",
246 | {
247 | --[[
248 | Usage :
249 | Regex, Replacement
250 | ]]
251 | {"(//[^.*]*.\n)", "",},
252 | {"(/%*[^*]*%*/)", "",},
253 | {"%s%s+", "",},
254 | {"[ \t]+$", "",},
255 | }
256 | },
257 | {
258 | "text/jscript",
259 | {
260 | --[[
261 | Usage :
262 | Regex, Replacement
263 | ]]
264 | {"(//[^.*]*.\n)", "",},
265 | {"(/%*[^*]*%*/)", "",},
266 | {"%s%s+", "",},
267 | {"[ \t]+$", "",},
268 | }
269 | },
270 | {
271 | "text/livescript",
272 | {
273 | --[[
274 | Usage :
275 | Regex, Replacement
276 | ]]
277 | {"(//[^.*]*.\n)", "",},
278 | {"(/%*[^*]*%*/)", "",},
279 | {"%s%s+", "",},
280 | {"[ \t]+$", "",},
281 | }
282 | },
283 | {
284 | "text/x-ecmascript",
285 | {
286 | --[[
287 | Usage :
288 | Regex, Replacement
289 | ]]
290 | {"(//[^.*]*.\n)", "",},
291 | {"(/%*[^*]*%*/)", "",},
292 | {"%s%s+", "",},
293 | {"[ \t]+$", "",},
294 | }
295 | },
296 | {
297 | "text/x-javascript",
298 | {
299 | --[[
300 | Usage :
301 | Regex, Replacement
302 | ]]
303 | {"(//[^.*]*.\n)", "",},
304 | {"(/%*[^*]*%*/)", "",},
305 | {"%s%s+", "",},
306 | {"[ \t]+$", "",},
307 | }
308 | },
309 | }
310 |
311 | --[[
312 |
313 | DO NOT TOUCH ANYTHING BELOW THIS POINT UNLESS YOU KNOW WHAT YOU ARE DOING.
314 |
315 | ^^^^^ YOU WILL MOST LIKELY BREAK THE SCRIPT SO TO CONFIGURE THE FEATURES YOU WANT JUST USE WHAT I GAVE YOU ABOVE. ^^^^^
316 |
317 | THIS BLOCK IS ENTIRELY WRITTEN IN CAPS LOCK TO SHOW YOU HOW SERIOUS I AM.
318 |
319 | ]]
320 |
321 | local content_type = ngx.header["content-type"]
322 | for key, value in ipairs(content_type_list) do
323 | if string.find(content_type, ";") ~= nil then -- Check if content-type has charset config
324 | content_type = string.match(content_type, "(.*)%;(.*)") --Split ;charset from header
325 | end
326 | if string.match(value[1], content_type) then
327 | for k, v in ipairs(value[2]) do
328 | local output = ngx.arg[1]
329 | local output_minified = output
330 |
331 | output_minified, occurrences, errors = string.gsub(output_minified, v[1], v[2])
332 |
333 | if output_minified then
334 | --Modify the output to replace it with our minified output
335 | ngx.arg[1] = output_minified
336 | end
337 |
338 | end --end foreach regex check
339 | end --end content_type if check
340 | end --end content_type foreach mime type table check
341 |
--------------------------------------------------------------------------------
/lua/minify/minify_header.lua:
--------------------------------------------------------------------------------
1 | --[[
2 | Introduction and details :
3 |
4 | Copyright Conor Mcknight
5 |
6 | https://github.com/C0nw0nk/Nginx-Lua-minification-library
7 |
8 | Disclaimer :
9 | I am not responsible for what you do with this script nor liable,
10 | This script was released under default Copyright Law as a proof of concept.
11 | For those who want to know what that means for your use of this script read the following : http://choosealicense.com/no-license/
12 |
13 | Information :
14 | I built this script to compress and keep the specified mime types outputs small and minify the bandwidth that my servers have to use when serving these files to users.
15 |
16 | If you have any bugs issues or problems just post a Issue request. https://github.com/C0nw0nk/Nginx-Lua-minification-library/issues
17 |
18 | If you fork or make any changes to improve this or fix problems please do make a pull request for the community who also use this. https://github.com/C0nw0nk/Nginx-Lua-minification-library/pulls
19 |
20 | Usage :
21 |
22 | Add this to your Nginx configuration folder.
23 |
24 | nginx/conf/lua/minify
25 |
26 | Once installed into your nginx/conf/ folder.
27 |
28 | Add this to your HTTP block or it can be in a server or location block depending where you want this script to run for individual locations the entire server or every single website on the server.
29 |
30 |
31 | header_filter_by_lua_file conf/lua/minify/minify_header.lua
32 | body_filter_by_lua_file conf/lua/minify/minify.lua;
33 |
34 | Example nginx.conf :
35 |
36 | This will run for all websites on the nginx server
37 | http {
38 | #nginx config settings etc
39 | header_filter_by_lua_file conf/lua/minify/minify_header.lua
40 | body_filter_by_lua_file conf/lua/minify/minify.lua;
41 | #more config settings and some server stuff
42 | }
43 |
44 | This will make it run for this website only
45 | server {
46 | #nginx config settings etc
47 | header_filter_by_lua_file conf/lua/minify/minify_header.lua
48 | body_filter_by_lua_file conf/lua/minify/minify.lua;
49 | #more config settings and some server stuff
50 | }
51 |
52 | This will run in this location block only
53 | location / {
54 | #nginx config settings etc
55 | header_filter_by_lua_file conf/lua/minify/minify_header.lua
56 | body_filter_by_lua_file conf/lua/minify/minify.lua;
57 | #more config settings and some server stuff
58 | }
59 |
60 | ]]
61 |
62 | ngx.header.content_length = nil
63 |
--------------------------------------------------------------------------------