Get started with OpenResty

Github OpenResty is a simple way to start openResty. Openresty has a container that is packaged. For learning, I have drawn a basic image of Ubuntu. Learn OpenResty (if you are not studying and developing in a container, remember that some commands will prompt you with permission questions, remember to use root permission and add sudo before the command).

Docker run -v /home/lmf/develop/lua_develop :/lua_develop --name lua_develop -p 81:80 --restart always -d Ubuntu :20.04 /bin/sh -c"while true; do echo hello world; sleep 1; done"
Copy the code

The contents of this article about OpenResty are all from the official documents of OpenResty, with a little change and supplement. The sources of the contents in this article are all marked with hyperlinks. You can click to view the original texthereYou can find the

directory

  • Get started with OpenResty
  • directory
    • The installation
    • Hello World
    • Openresty based dynamic routing
    • Use Luarocks in OpenResty

The installation

OpenResty ® Linux packages

Install the required tools
apt-get install -y lsb-release 
Add openResty's APT repository to the system
apt-get -y install --no-install-recommends wget gnupg ca-certificates
Import openResty's GPG key:
wget -O - https://openresty.org/package/pubkey.gpg | apt-key add -
# Add official APT repository:
echo "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/openresty.list
# installation openresty
apt update
apt-get -y install openresty
Copy the code

Hello World

译 文 : Hello World

  • Create the development directory (from now on all commands are executed in the work directory)
mkdir ~/work
cd ~/work
mkdir logs/ conf/
Copy the code
  • Add the nginx.conf file to the conf directory
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 80;
        location / {
            default_type text/html;
            content_by_lua_block {
                ngx.say("<p>hello, world</p>")
            }
        }
    }
}
Copy the code
  • Start the Nginx service
    • Configuring Environment Variables
    PATH=/usr/local/openresty/nginx/sbin:$PATH
    export PATH
    Copy the code
    • Start the service
    nginx -p `pwd`/ -c conf/nginx.conf
    Copy the code
    • access
    root@7668fd5c1cfc:/lua_develop/work# curl 127.0.0.1
    <p>hello, world</p>
    Copy the code

At this point, we have successfully started our first OpenResty service

Openresty based dynamic routing

Dynamic Routing based on OpenResty

  • Prepare redis

Create a Redis container using Docker

docker run --name myredis -p 6379:6379 --restart always -d redis redis-server
Copy the code
  • nginx conf(dynamic-routing-based-on-redis.conf)
worker_processes 1; error_log logs/error.log info; events { worker_connections 1024; } http { upstream apache.org { server apache.org; } upstream nginx.org { server nginx.org; } server { listen 80; location = /redis { internal; set_unescape_uri $key $arg_key; redis2_query get $key; Redis2_pass 192.168.179.130:6379; } location / { set $target ''; access_by_lua ' local key = ngx.var.http_user_agent local res = ngx.location.capture( "/redis", { args = { key = key } } ) print("key: ", key) if res.status ~= 200 then ngx.log(ngx.ERR, "redis server returned bad status: ", res.status) ngx.exit(res.status) end if not res.body then ngx.log(ngx.ERR, "redis returned empty body") ngx.exit(500) end local parser = require "redis.parser" local server, typ = parser.parse_reply(res.body) if typ ~= parser.BULK_REPLY or not server then ngx.log(ngx.ERR, "bad redis response: ", res.body) ngx.exit(500) end print("server: ", server) ngx.var.target = server '; proxy_pass http://$target; }}}Copy the code
  • Start the service
    • First go to add data in Redis
    192.168.179.130:6379> set foo apache.org
    OK
    192.168.179.130:6379> set bar nginx.org
    OK
    Copy the code
    • Stop the previous service (if any)
    nginx -p `pwd` -s stop
    Copy the code
    • Start the service
    root@7668fd5c1cfc:/lua_develop/work# nginx -p `pwd`/ -c conf/dynamic-routing-based-on-redis.conf 
    Copy the code
  • test
curl --user-agent foo localhost
curl --user-agent bar localhost
Copy the code

Use Luarocks in OpenResty

LuaRocks is a system for deploying and managing Lua modules.

  • The installation
    • Luajit environment variables
    PATH=/usr/local/openresty/luajit/bin:$PATH
    export PATH
    Copy the code
    • Install the required packages
    apt install -y zip mak
    Copy the code
    • Download and install Luarocks, be careful to download the latest version
    Wget https://luarocks.org/releases/luarocks-3.5.0.tar.gz tar - XZVF luarocks - 2.0.4.1. Tar. Gz luarocks 2.0.4.1 / CD ./configure make make installCopy the code
  • Example Install the LUa MD5 library using luarocks
    • The installation
    apt install -y gcc
    luarocks install md5
    Copy the code
    • Configure the OpenResty application

    Create md5.conf in the same work/conf directory

    worker_processes 1; # we could enlarge this setting on a multi-core machine error_log logs/error.log error; events { worker_connections 1024; } http { lua_package_path 'lua_src/? .lua;; '; server { listen 80; server_name localhost; location = /luarocks { content_by_lua ' local foo = require("foo") foo.say("hello, luarocks!" ) '; }}}Copy the code

    Create two lua module files: lua_src/foo.lua

    module("foo".package.seeall)
    local bar = require "bar"
    ngx.say("bar loaded")
    function say (var)
        bar.say(var)
    end
    Copy the code

    lua_src/bar.lua

    module("bar".package.seeall)
    local rocks = require "luarocks.loader"
    local md5 = require "md5"
    ngx.say("rocks and md5 loaded")
    function say (a)
        ngx.say(md5.sumhexa(a))
    end
    Copy the code
    • Example Start the Nginx service
    nginx -p `pwd`/ -c conf/md5.conf
    Copy the code
    • test

    First run

    root@7668fd5c1cfc:/# curl http://localhost/luarocks
    rocks and md5 loaded
    bar loaded
    85e73df5c41378f830c031b81e4453d2
    Copy the code

    Run it later

    root@7668fd5c1cfc:/# curl http://localhost/luarocks
    85e73df5c41378f830c031b81e4453d2
    Copy the code

    This is because the Lua Nginx Module, by default, caches the lua modules that have already been loaded, and the code for these data was run when Lua was loaded, so they will not be executed again.

    • Ab test

    Installation tools

    apt-get install -y apache2-utils
    Copy the code

    test

    Ab - c10 - n50000 http://127.0.0.1/luarocksCopy the code