How to Configure a Virtual Domain and Run Lua Scripts with OpenResty
This guide walks you through setting up a local virtual domain for OpenResty, creating and editing configuration files, adding Lua location blocks, using external Lua script files, managing the lua_code_cache setting, and troubleshooting errors with practical command examples.
Overview
To simplify development, a virtual domain http://openresty.tinywan.com is configured locally. Create a configuration file openresty.tinywan.com.conf under /usr/local/openresty/nginx/conf with the following minimal server block:
server {
listen 80;
server_name openresty.tinywan.com;
}Write location Block
Add a location /lua block to the server configuration to return a simple "Hello World" message using Lua:
location /lua {
default_type 'text/html';
content_by_lua 'ngx.say("Hello World")';
}Validate the Nginx configuration syntax: /usr/local/openresty/nginx/sbin/nginx -t If the syntax is OK, reload the service: sudo systemctl restart openresty.service or /usr/local/openresty/nginx/sbin/nginx -s reload Visiting http://openresty.tinywan.com/lua should display Hello World.
Use External Lua Script Files
Embedding Lua code directly in the configuration can become unwieldy. Create a lua directory under /usr/local/openresty/nginx/conf and add hello.lua with the content:
ngx.say("Hello 开源技术小栈")Update the server block to load the script file:
server {
listen 80;
server_name openresty.tinywan.com;
location /lua {
default_type 'text/html';
content_by_lua_file conf/lua/hello.lua; # relative to the nginx installation directory
}
}The script path can also be absolute, e.g., /usr/local/openresty/nginx/conf/lua/hello.lua. After restarting OpenResty, test with:
curl -i http://openresty.tinywan.com/lualua_code_cache Setting
By default, lua_code_cache is enabled, caching compiled Lua code. Changes to Lua scripts require a full Nginx reload to take effect. During development you can disable the cache with lua_code_cache off; so that modifications are reflected immediately without reloading, but remember to enable it in production for performance.
server {
listen 80;
server_name openresty.tinywan.com;
location /lua {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file conf/lua/hello.lua;
}
}When the cache is turned off, reloading Nginx produces a warning:
nginx: [alert] lua_code_cache is off; this will hurt performance in /etc/nginx/conf.d/openresty.tinywan.com.conf:7Error Logs
If runtime errors occur, check the error log:
tail -f /usr/local/openresty/nginx/logs/error.logSample error output showing an invalid return code in a configuration file:
2024/07/13 13:13:48 [emerg] 1#1: invalid return code "Hello World!" in /etc/nginx/conf.d/tinywan_lua.conf:7
2024/07/13 13:14:01 [emerg] 1#1: invalid return code "Hello World!" in /etc/nginx/conf.d/tinywan_lua.conf:7
2024/07/13 13:14:28 [emerg] 1#1: invalid return code "Hello World!" in /etc/nginx/conf.d/tinywan_lua.conf:7Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
