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.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
How to Configure a Virtual Domain and Run Lua Scripts with OpenResty

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/lua

lua_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:7

Error Logs

If runtime errors occur, check the error log:

tail -f /usr/local/openresty/nginx/logs/error.log

Sample 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:7
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

NginxWeb serverLuaServer ConfigurationOpenResty
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.