推薦使用vscode. 相關的插件:
debug配置文件配置:
{
"version": "0.2.0",
"configurations": [
{
"type": "lua-local",
"request": "launch",
"name": "OpenResty Debug",
"program": {
"command": "/usr/local/openresty/bin/resty", // 替換為你的 resty 路徑
},
"args": [
"${file}"
],
}
]
}
啓動/停止方式:
# sudo /usr/local/openresty/bin/openresty -c "`pwd`/hello.conf"
# sudo /usr/local/openresty/bin/openresty -c "`pwd`/hello.conf" -s stop
hello.conf 配置內容:
# sudo /usr/local/openresty/bin/openresty -c "`pwd`/hello.conf"
# sudo /usr/local/openresty/bin/openresty -c "`pwd`/hello.conf" -s stop
worker_processes 1;
# events {}
events {
worker_connections 512;
}
http {
server {
listen 80;
server_name *.*;
location / {
content_by_lua_block {
ngx.print("hello, world")
}
}
location /echo {
content_by_lua_block {
-- 獲取 URL 查詢參數(?a=1&b=2)
local args = ngx.req.get_uri_args()
-- 獲取請求頭
local headers = ngx.req.get_headers()
-- 返回 JSON 格式的響應
ngx.header['Content-Type'] = 'application/json'
ngx.say(
require('cjson').encode(
{
args=args,
-- headers=headers,
method=ngx.var.request_method,
path = ngx.var.request_uri
}
)
)
}
}
}
}