http_config
设置 http {} 块内的自定义配置,作用于所有 server 块。
语法
--- http_config 段的内容会被插入到 http {} 块中,在所有 server {} 块之前。适合放置共享的 upstream 定义、Lua 共享字典、全局变量等。
perl
1=== TEST 1: upstream config2--- http_config3 upstream backend {4 server 127.0.0.1:$TEST_NGINX_RAND_PORT_1;5 }67 lua_shared_dict my_cache 10m;8--- config9 location /t {10 proxy_pass http://backend;11 }12--- request13GET /tinit_by_lua 示例
常见用法是在 http_config 中放置初始化代码:
perl
1=== TEST 2: shared lua code2--- http_config3 init_by_lua_block {4 local utils = require "my.utils"5 package.loaded.utils = utils6 }7--- config8 location /t {9 content_by_lua_block {10 local utils = package.loaded.utils11 ngx.say(utils.hello())12 }13 }14--- request15GET /t16--- response_body17hello