What is it?

Openresty = Nginx + ngxhttp_lua_module + lua_resty*

Since Openresty is mentioned, its author “Zhang Yichun” must be mentioned. About the introduction of “spring brother”, here is not much to say, god like programmer, you can learn about its portal in Zhihu.

What are the common utilization scenarios?

1. Dynamic load balancing

  • Common traffic uses consistent hashing to improve hit ratio. Hotspot traffic rotation reduces the pressure on a single server.

  • Distribute traffic to different groups and limit traffic (crawler, or IP with heavy traffic) according to request characteristics.

  • Dynamic traffic (dynamically increasing or decreasing upstream or dynamic load balancing) can be done using Balancer_by_lua or Twitter’s open source upsync.

2. The firewall

Such as: DDOS, IP/URL/UserAgent/Referer blacklist, anti-theft chain, etc.

  • Illegal request filtering, for example, should have Referer but not, or should have Cookie but not Cookie.

  • Request header filtering, such as some businesses that do not require request headers, can be filtered out when forwarding to business Nginx.

3. Prevent DDOS traffic limiting

  • You can push the request logs to the real-time computing cluster, and then push the IP addresses that need traffic limiting to the core Nginx for traffic limiting.

  • Dynamic traffic limiting is implemented according to interface characteristics and interface throughput. For example, the back-end service is about to fail, so we need to perform traffic limiting. The traffic limiting requests are treated as degraded requests. With Lua-resty-limit-traffic, you can program more flexible downgrade logic by user, by URL, etc., such as whether to let the user request wait (such as sleep 100ms, so the user request slows down but the service is still available) or return the degraded content.

4. The server requests aggregation

Nginx will take Nginx concurrent requests on the server side and aggregate the results and spit them out in one go.

5. Multi-level cache mode

For the read service, a large number of caches are used to improve performance. In the design, we mainly have the following Cache applications: First, read the Nginx local Cache Shared Dict or Nginx Proxy Cache, and directly return the content to the user if there is any; If the local cache is not hit, the distributed cache such as Redis is read, and if there is a direct return; If not, go back to the Tomcat application to read the DB or call the service to obtain data. In addition, we cache data by dimension.

6. The drop

If the volume of requests is too large to carry, then we need to actively downgrade; If the back end hangs or is curbed or the back end times out, we need to degrade passively.

7.AB test/grayscale release

For example, to create a new interface, you can write complex business rules in Business Nginx through Lua to implement different versions for different people.

8. Service quality monitoring

We can record request response time, cache response time, reverse proxy service response time to get a detailed understanding of which service is slow; Also log non-200 status code errors to see service availability.

How to install it?

Openresty download address: portal.

This example only illustrates the security of non-Windows platforms, Windows platforms directly download the corresponding installation package is OK.

Install dependencies

# ubuntu apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make build-essential curl # centos yum install readline-devel pcre-devel openssl-devel gcc curl # mac brew update brew install pcre openssl curl

Copy the code

download

Wget https://openresty.org/download/ngx_openresty-1.9.7.2.tar.gz tar ZXVF ngx_openresty - 1.9.7.2. Tar. Gz

Copy the code

Install LuaJIT

CD ngx_openResty-1.9.7.2 /bundle/ luajit-2.1-20160108 / make clean && make && make install ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit

Copy the code

Install other modules

Located in the ngx_OpenResty-1.9.7.2 /bundle directory.

Ngx_cache_purge: Purge the Nginx cache

Wget HTTP: / / http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz

Copy the code

Nginx_upstream_check_module: Ustream health check

Wget HTTP: / / http://nginx.org/download/nginx-1.0.14.tar.gz

Copy the code

Install ngx_openresty

CD/usr/local/bar/ngx_openresty - 1.9.7.2. / configure -- with openssl = / usr/local/Cellar/openssl / 1.0.2 j --prefix=/usr/local/openresty --with-http_realip_module --without-http_redis2_module --with-pcre --with-luajit -j2 make && make install

Copy the code

See the help

--help to see more options

Copy the code

Is the installation successful?

To the /usr/local/bar directory, many new directories appear.

Luajit: Luajit environment; Lua is an interpreted language that allows instant compilation of Lua code into machine code for good performance. Lualib: lua library. Nginx: installed nginx.

We can use the/usr/local/bar/nginx/sbin/nginx -h to view help information.

How to configure the development environment?

The main configuration file (/ usr/local/bar/nginx/conf/nginx. Conf).

Add lua library dependencies

lua_package_path "/usr/local/bar/lualib/? .lua;;" ; lua_package_cpath "/usr/local/bar/lualib/? .so;;" ;

Copy the code

Test whether the configuration is correct

nginx/sbin/nginx -t

Copy the code

Restart the nginx

nginx/sbin/nginx -s reload

Copy the code

Close the nginx

nginx/sbin/nginx -s quit

Copy the code

Common Lua plug-ins

  • Http client (OpenResty does not provide an Http client by default and requires a third-party client):lua-resty-http

  • Mysql client (default):lua-resty-mysql

  • Redis client (default):lua-resty-redis

  • Real-time monitoring of Nginx domain name requests: ngx_lua_reqStatus

  • Cache (shared by each Worker process):lua-resty-lrucache

  • CJSON:Lua CJSON

  • dkjson:dkjson

  • Lua utf-8 library: luautf8

  • Template rendering :lua-resty-template

  • waf:ngx_lua_waf

  • Static file merger :nginx-lua-static-merger

  • Dynamically check the status of the back-end service node: lua-resty-upgrade-healthCheck

Video data

  • OpenResty shenzhen 2016 Video: Portal password “2FEP”.

Note: the link in the article can not be clicked, please read the original view!!