Two upgrades to "web acceleration" LUA Redis

Source: Internet
Author: User
Tags lua

Previous Openresty related articles, but also used to accelerate the speed of the Web page, but the last time did not optimize the code, this finishing, the optimization of nginx configuration and LUA code, interested in the words can look at the article:
Https://www.cnblogs.com/w1570631036/p/8449373.html

In order to learn, constantly to their server loading things, is Logstash, but also Kafka, causing the main station network load, CPU consumption is too large, plus tomcat this itself is particularly occupied with memory things, as long as a little refresh the site, you can feel the snail-like speed, really can't stand, A period of time to add to the site N multilayer cache, still no change how much, think about, forget, have been so card, it is not as direct as the dynamic website directly into a static web page stored in Redis, and then turn off Tomcat, seemingly no change, but in Xshell inside knock command not so card, here , it also presents a kind of website acceleration method--redis store static Web pages.

First, the overall process is as follows

1. Once requested, access the Lua script via Openresty Nginx;
2. Read if there is a static web page for the URI in Redis, and if so, return directly, otherwise back to Tomcat, and then save the contents of the response to Redis.

Second, the Nginx settings

Openresty with Nginx, so only need to configure, we end up now is to intercept all the HTML end of the request, if the end of the other suffix, such as do, you can directly roll back into the tomat inside.
Due to the length of the relationship, only a partial nginx configuration, want to see the full please go to: mynginxconfig.ngx

    server {        listen       80;        # listen       443 ssl;   # ssl        server_name  www.wenzhihuai.com;        location  ~ .*\.(html)$ {       //拦截所有以html结尾的请求,调用lua脚本            ...            charset utf8;            proxy_pass_request_headers off ;            # 关闭缓存lua脚本,调试的时候专用            lua_code_cache off;            content_by_lua_file /opt/lua/hello.lua;        }        location / {        //nginx是按顺序匹配的,如果上面的不符合,那么将回滚tomcat            default_type    text/html;            root   html;            index  index.html index.htm;            ...            # websocket            proxy_http_version 1.1;            proxy_set_header Upgrade $http_upgrade;            proxy_set_header Connection "upgrade";            proxy_pass http://backend;        }
Third, Lua script

In order to facilitate the operation of key, after testing, even if the URI with a variety of characters, such as?. html = &, and so on, can be directly set to the key in Redis, so, it is not necessary to consider the Redis key violation rules, you can directly set the URI to key. The specific process is as follows:

local key = request_uri首先,key为请求访问的urilocal resp, err = red:get(key)去redis上查找有没有if resp == ngx.null then    如果没有    ngx.req.set_header("Accept", "text/html,application/xhtml+xml,application/xml;")    ngx.req.set_header("Accept-Encoding", "")    这里,特别需要注意的是,要把头部的信息去掉,这里之前说过。(如果不去掉,就是gzip加密返回,然后再经过一层gzip加密返回给用户,导致用户看到的是gzip压缩过的乱码)    local targetURL = string.gsub(uri, "html", "do")    这里讲html替换为do,即:不拦截*.do的请求,其可以直接访问tomcat    local respp = ngx.location.capture(targetURL, { method = ngx.HTTP_GET, args = uri_args })    开始回源到tomcat    red:set(key, respp.body)    将uri(key)和响应的内容设到redis里面去    red:expire(key, 600)    lua redis并没有提供在set的时候同时设置过期时间,所以,额外加一行设置过期时间    ngx.print(respp.body)    将响应的内容输出给用户    returnendngx.print(resp)
Iv. Testing

To do a test to access http://www.wenzhihuai.com/jaowejoifjefoijoifaew.html, for example, my site does not have this URI set, so, when accessed, it is uniformly tuned to the error page, You'll see this record in Redis:

This address has been successfully cached in the Redis inside, click on other pages, you can see, as long as the page is clicked, are cached into the redis inside. Overall, if you do not set the expiration time, you can cache the entire page into Redis, or even can close Tomcat, but this practice only applies to perpetual pages, as for the enterprise,,,,

Postscript:
In fact, I have a question, my code, and did not set the LUA disconnected Redis connection, do not know whether it will affect, and it refers to each request come over, need to reconnect Redis? TCP Three handshake takes a lot of time, and I don't know how to optimize this information.

The full code is as follows:

Local Redis = require "Resty.redis" local red = redis:new () local Request_uri = ngx.var.request_urilocal Ngx_log = Ngx.loglo Cal Ngx_err = Ngx. errlocal function Close_redis (red) if not red then return end local Pool_max_idle_time = 10000 local PO  Ol_size = Red:set ("Pool_size", pool_size) local OK, err = red:set_keepalive (Pool_max_idle_time, pool_size) if Not OK then Ngx_log (Ngx_err, "Set Redis keepalive Error:", ERR) endendlocal uri = Ngx.var.urired:set_timeout ( Red:connect ("119.23.46.71", 6340) Red:auth ("root") Local Uri_args = Ngx.req.get_uri_args () Local key = Request_ Urilocal resp, err = red:get (key) If resp = = Ngx.null then Ngx.req.set_header ("Accept", "Text/html,application/xhtml+xml    , Application/xml; ") Ngx.req.set_header ("accept-encoding", "") Local TargetUrl = String.gsub (URI, "html", "do") local RESPP = Ngx.locatio N.capture (TargetUrl, {method = Ngx. Http_get, args = Uri_args}) red:set (key, Respp.body) Red:expire (Key, 600) Ngx.print (respp.body) returnendngx.print (resp) Close_redis (red) 

Web acceleration Two upgrades for Lua Redis

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.