[Original] Nginx + Lua server merges static files, nginxlua

Source: Internet
Author: User

[Original] Nginx + Lua server merges static files, nginxlua

Http://homeway.me





0x01. About

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

nginx-lua-static-mergerIt is an openresty-based module mainly used to merge static files, reduce http requests, and accelerate static file access.

Usenginx-lua-static-mergerYou must add the openresty module when compiling nginx, or directly install openresty as the server.

nginx-lua-static-mergerIt can make your js and css requests less than that.




0x02. About Nginxnginx works

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:



Remove Homepageindex.htmlLoading time, we can see that loading static file css and js takes a long time to wait, and image loading is only completed after js and css loading, that is, 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,nginx.confInlua_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 to correct.

2. Ensure/www/openresty/staticYou have Lua write permission.

Use

The frontend call method is as follows:

<link rel="stylesheet" href="/bootstrap/css/bootstrap.min.css;/qiniu/css/main.css;/css/navbar.css"><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 passes throughcontent_by_lua_filePut the next processing Lua into logic.

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:





This article from the summer grass, reproduced please indicate the source: http://homeway.me/2015/06/22/nginx-lua-static-merger/


-By xiaocao

20:04:10

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.