使用proxy_pass时浏览器返回本地后端

我试图配置安装在本地网络中且通过另一个本地服务器的proxy_pass指令访问的Wordpress,但响应作为带有本地后端的地址在浏览器中返回,请参见以下配置:

+--------+
| router |
+--------+
  端口转发:80 -> 1888

+-------+
| nginx |
+-------+
  在lua块中请求被检查

  local site = string.lower(ngx.var.http_host)
  local backend = "http://192.168.0.20:1888"  # 这里运行WP

  # 只有当请求到达'some_site.com'后端时,地址才会更改
  if site == "some_site.com"
  then
    backend = "http://another_local_address"
  end

  return backend
  ...

  proxy_pass $backend;

当我在地址栏中访问域时,我看到:

response

请帮助我理解正确的配置,使请求正确地传递到我的本地服务器。

点赞
用户3342050
用户3342050

你的帖子里有一个打字错误。使用一个=符号来分配一个变量。使用两个==符号来测试相等性。if site == 'some_site.com'

为测试目的,选择一个:

local ngx  = { var = { http_host = nil } }

ngx.var.http_host = 'http://some_site.com'
ngx.var.http_host = 'https://some_site.com'
ngx.var.http_host = 'http://some_site.com/this'
ngx.var.http_host = 'https://some_site.com/that'
ngx.var.http_host = 'http://192.168.0.20:1888'
ngx.var.http_host = 'https://192.168.0.20:1888'

在生产环境中,不要覆盖ngx.var.http_host。另外,应该返回值而不是打印输出。


local site_name = 'some_site.com'
local site = string.lower(ngx.var.http_host)

-- 左去(lstrip)掉(http:// 或者 https://)和右去掉(rstrip)除了名称之外的任何内容
local stripped = site:gsub('http[s]-://', ''):sub(1, #site_name)
local backend = 'http://192.168.0.20:1888'

if stripped == site_name then
    backend = 'http://another_local_address'
end

print(backend) -- 这里使用print()进行测试
-- return backend
2020-12-01 23:40:20