config
设置 server {} 块内的自定义 Nginx 配置指令。这是最常用的配置段。
语法
--- config 段的内容会被插入到自动生成的 server {} 块中。通常用于定义 location 块和相关指令。
perl
1=== TEST 1: basic config2--- config3 location /t {4 echo "hello";5 }6--- request7GET /t8--- response_body9hello工作原理
框架会自动生成一个包含如下结构的 nginx.conf:
nginx
1http {2 server {3 listen $TEST_NGINX_PORT;4 server_name localhost;56 # 你的 config 内容会被插入这里78 # 框架自动添加的默认 location /9 location / {10 ...11 }12 }13}因此你在 --- config 中只需要写 server 块内部的指令。
如果你需要控制 http 块级别的配置,请使用 http_config 段。
配合 Lua 使用
在 OpenResty 项目中,config 段最常见的用法是配合 Lua 指令:
perl
1=== TEST 2: lua content handler2--- config3 location /t {4 content_by_lua_block {5 local args = ngx.req.get_uri_args()6 ngx.say("name: ", args.name or "unknown")7 }8 }9--- request10GET /t?name=test11--- response_body12name: test