OpenResty HelloWorld

1. Installation

Take CentOS as an example:

mkdir -p /opt

# download openresty
wget https://openresty.org/download/openresty-1.13.6.2.tar.gz

tar zxvf openresty-1.13.6.2.tar.gz
cd openresty-1.13.6.2

# configure
./configure --prefix=/opt/openresty -j4

make -j4 && make install

The source code package can be obtained from  https://openresty.org/cn/download.html  .
-j4Indicates that 4 cores are used. configureYou can also specify various parameters at that step, use to  ./configure --help view more options.

For installation on other system environments, please refer to  https://openresty.org/cn/installation.html  .

In fact, installing OpenResty is similar to installing Nginx, because OpenResty is developed based on Nginx.

Second, the test

Find the 3rd module supported by nginx: https://www.nginx.com/resources/wiki/modules/index.html

https://github.com/openresty/lua-nginx-module

Openresty novice: https://openresty.org/cn/getting-started.html

1. Test 1content_by_lua

Modify /opt/openresty/nginx/conf/nginx.conf

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        location / {
            default_type text/html;
            content_by_lua_block {
                ngx.say("<p>hello, world</p>")
            }
        }
    }
}

Among them content_by_luaare the instructions provided by OpenResty, which can be searched in the official documentation: https://github.com/openresty/lua-nginx-module

/opt/openresty/nginx/sbin/nginx -s realod
curl http://localhost:8080/

2. Test 2content_by_lua_file

Modify /opt/openresty/nginx/conf/nginx.conf

 
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        location / {
            default_type text/html;
            content_by_lua {
                ngx.say("<p>hello, world</p>")
            }
        }
    }
}
mkdir nginx/conf/lua
vim nginx/conf/lua/hello.lua
ngx.say("hello lua")
/opt/openresty/nginx/sbin/nginx -s reload
curl http://localhost/

3. Test 3lua_code_cache

In order to avoid the need to restart Nginx for each modification, you can serverconfigure the lua_code_cacheoptions in the Nginx options.

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        lua_code_cache off;
        location / {
            default_type text/html;
            content_by_lua {
                ngx.say("<p>hello, world</p>")
            }
        }
    }
}

The prompt says that lua_code_cacheperformance will be affected after closing. nginx/conf/lua/hello.luaThe code we modified again  will take effect after saving, without reloading the server.

Note lua_code_cache off;that the performance of the engine Nginx will be enabled in the production environment.

3. Summary

Development in OpenResty is divided into two steps. The first step is to modify the Nginx configuration, and the second step is to use Lua to develop your own script.

Published 524 original articles · praised 172 · 100,000+ views

Guess you like

Origin blog.csdn.net/INGNIGHT/article/details/104823204