geqintan commited on
Commit
fe4c471
·
1 Parent(s): 07fbb37
Files changed (2) hide show
  1. app.lua +2 -2
  2. nginx.conf +8 -12
app.lua CHANGED
@@ -19,8 +19,8 @@ local api_version, api_pxoxy_pass_url_indicator = uri:match("/?([^/]+)/?(.*)")
19
  ngx.say("api_version: ", api_version)
20
  ngx.say("api_pxoxy_pass_url_indicator: ", api_pxoxy_pass_url_indicator)
21
  pass_url = api_pxoxy_pass_url_indicator
22
- local upstream_dict = ngx.shared.upstream_dict
23
- upstream_dict:set("upstream", pass_url)
24
  pass_url = pass_url:gsub("http/", "http://")
25
  pass_url = pass_url:gsub("https/", "https://")
26
  ngx.say("pass_url: ", pass_url)
 
19
  ngx.say("api_version: ", api_version)
20
  ngx.say("api_pxoxy_pass_url_indicator: ", api_pxoxy_pass_url_indicator)
21
  pass_url = api_pxoxy_pass_url_indicator
22
+ -- local upstream_dict = ngx.shared.upstream_dict
23
+ -- upstream_dict:set("upstream", pass_url)
24
  pass_url = pass_url:gsub("http/", "http://")
25
  pass_url = pass_url:gsub("https/", "https://")
26
  ngx.say("pass_url: ", pass_url)
nginx.conf CHANGED
@@ -18,18 +18,14 @@ http {
18
 
19
  location / {
20
  default_type text/html;
21
- # content_by_lua_file /data/myapp/app.lua;
22
- rewrite_by_lua_block {
23
- ngx.say("Nginx Version: ", ngx.var.nginx_version)
24
- -- ngx.say("Hello World!",ngx.var.uri)
25
- -- ngx.var.proxy_pass = ngx.var.uri
26
- ngx.var.upstream = 'https://baidu.com';
27
- }
28
- content_by_lua_block {
29
- local upstream_dict = ngx.shared.upstream_dict
30
- ngx.var.upstream = upstream_dict:get("upstream")
31
- }
32
- proxy_pass $upstream;
33
  }
34
  }
35
  }
 
18
 
19
  location / {
20
  default_type text/html;
21
+
22
+ # 使用Lua从共享字典获取目标地址
23
+ set_by_lua $proxy_target '
24
+ return ngx.shared.upstream_dict:get("target")
25
+ ';
26
+
27
+ # 动态设置proxy_pass
28
+ proxy_pass $proxy_target;
 
 
 
 
29
  }
30
  }
31
  }