OpenResty(ngx_openresty)將 Nginx 核心、常用第三方模塊、LuaJIT、lua-nginx-module 打包整合,開箱即用構建高性能 Web 應用。本文演示最小化 Hello World 配置與驗證。
yum/apt install openresty 或官網下載編譯安裝。docker run -p 80:80 openresty/openresty。
# nginx.conf 片段
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /lua {
default_type text/plain;
# 直接內聯 Lua 代碼
content_by_lua_block {
ngx.say("Hello World from inline Lua!")
}
# 或引用外部文件 (推薦生產)
# content_by_lua_file /opt/lua/bin/test.lua;
}
}
-- /opt/lua/bin/test.lua
local welcome = "Hello World from file!"
ngx.say(welcome)
nginx -s reload
# 或 openresty -s reload
瀏覽器訪問 https://localhost/lua,顯示:

| 指令 | 用途 |
|---|---|
content_by_lua_block | location 內直接寫 Lua 代碼 |
content_by_lua_file | 引用外部 .lua 文件 |
ngx.say(...) | 輸出響應體 (自動添加換行) |
ngx.print(...) | 輸出響應體 (不換行) |
default_type | 設置默認 Content-Type |
lua-resty-router 實現 RESTful 路由。lua-resty-mysql、lua-resty-redis 非阻塞驅動。lua_shared_dict 共享內存字典,lua-resty-lrucache LRU 緩存。ngx.sleep、ngx.timer.at、cosocket 非阻塞網絡。test-nginx (Perl) 集成測試框架。OpenResty 以 **Nginx 事件循環 + LuaJIT 協程** 實現極高並發,**配置即代碼,代碼即配置**。Hello World 只是起點,**「萬事開頭難,Hello World 成功一半」**,繼續探索 lua-nginx-module 豐富 API,構建下一代高性能 Web 服務。