http_config

设置 http {} 块内的自定义配置,作用于所有 server 块。

语法

--- http_config 段的内容会被插入到 http {} 块中,在所有 server {} 块之前。适合放置共享的 upstream 定义、Lua 共享字典、全局变量等。

perl
1=== TEST 1: upstream config
2--- http_config
3 upstream backend {
4 server 127.0.0.1:$TEST_NGINX_RAND_PORT_1;
5 }
6
7 lua_shared_dict my_cache 10m;
8--- config
9 location /t {
10 proxy_pass http://backend;
11 }
12--- request
13GET /t

init_by_lua 示例

常见用法是在 http_config 中放置初始化代码:

perl
1=== TEST 2: shared lua code
2--- http_config
3 init_by_lua_block {
4 local utils = require "my.utils"
5 package.loaded.utils = utils
6 }
7--- config
8 location /t {
9 content_by_lua_block {
10 local utils = package.loaded.utils
11 ngx.say(utils.hello())
12 }
13 }
14--- request
15GET /t
16--- response_body
17hello