[Original] merge static files on the Nginx + Lua Server

Source: Internet
Author: User

[Original] merge static files on the Nginx + Lua Server

 

 

 


 

 

0x01. About

Source code has been uploaded to github: https://github.com/grasses/nginx-lua-static-merger

Nginx-lua-static-merger is an openresty-based module used to merge static files, reduce http requests, and accelerate the access speed of static files.

To use nginx-lua-static-merger, you must add the openresty module during nginx compilation, or directly install openresty as the server.

Nginx-lua-static-merger can make your js and css requests less than that. One is enough.

 


  <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPiZuYnNwOzwvcD4NCjxociAvPg0KPGJyIC8 + DQo8cD4mbmJzcDs8L3A + DQo8aDEgaWQ9 "0x02about-nginx"> 0x02. About Nginxnginx

First, let's look at nginx's process of processing http requests based on the nginx principle.

The Nginx module has the following functions:

Handlers (processor module) directly processes requests and outputs content and modifies headers information. Generally, Handlers can have only one processor module.

Filters (filter module) mainly modifies the content output by other processor modules, and is finally output by Nginx.

Proxies (proxy module) and Nginx HTTP Upstream modules. These modules interact with backend services such as FastCGI to implement Service proxy and load balancing functions.

 


 

That is to say, when an http request comes over, nginx first processes the request information, then filters the information, and finally throws it to the proxy for processing. It does not process many things, it only maps the request to a location block by searching the configuration file, and the commands configured in the location will start different modules to complete the work.

From the normal perspective, loading 10 JavaScript files and css files on a page is normal, and there are 10 images, even if nothing is done, A page also needs to process more than 20 http requests. Each http request must run the TCP request, nginx response, filtering, and allocation.

Speed test:

Take a look at the figure. A foreign speed testing tool:

 


 

The loading time of index.html is dropped. It can be seen that loading static file css takes a long time for js, and image loading takes a long time after loading JavaScript and css. That is to say, if loading js or css is slow, the page will be slow.

This page is github.com/grasses.

 


 

0x02. UsageOpenresty

Http://openresty.org/cn/

Openresty is an open-source project written by Chinese people. It packages the standard Nginx core, many common third-party modules, and most of their dependencies.

tar xzvf ngx_openresty-VERSION.tar.gzcd ngx_openresty-VERSION/./configuremakemake install

For detailed installation tutorials, visit the official Internet cafe.

file path
|--/usr/local/openresty/nginx|                       `--lua |                           `--nginx-lua-static-merger.lua|                       `--conf |                           `--nginx.lua|--/www/openresty/static|               `--js|               `--css|               `--cache

Note:

1. In nginx. conf, lua_package_path "/usr/local/openresty/lualib /?. Lua; "; and
Lua_package_cpath "/usr/local/openresty/lualib /?. So; "; if you compile nginx instead of directly installing openresty, remember to set the directory correctly.

2. Ensure that/www/openresty/static has Lua write permission.

use

The frontend call method is as follows:

  
<script src="/js/jquery.min.js;/js/main.js;/qiniu/bootstrap/js/bootstrap.min.js;/qiniu/js/plupload/plupload.full.min.js;/qiniu/js/plupload/i18n/zh_CN.js"></script> 

 


 

0x03.How it work

Nginx uses content_by_lua_file in location to perform logic for the next Lua process.

Lua uses uri for md5 encoding to determine whether the cache exists. If the cache does not exist, it cyclically splits and traverses the uri, accesses the response path, searches for static files, and records the existence, write the cache file to facilitate next access.

 


 

 


 

0x04.Effect

The following is a request for multiple js results without processing:

 


 

In the first request, lua not only needs to obtain data but also merge and generate cache, which is cold data:


 

 


 

The second access is hot data, and the access speed is increased:

 


 

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.