nginx proxy_pass 设置主机名

nginx proxy_pass 设置主机名

这是来自 nginx.conf 的服务器块,我正在尝试重写我的请求,这很正常,但是我的上游主机名设置不正确,它总是解析为实际的 IP 地址,但我的 API 在 apache 中作为虚拟主机托管,因此端点总是返回 500,有什么想法可以解决这个问题吗?

listen 8090;
server_name example.dev.xyz.com;

set $api_path http://example-dev/api;

location ~ ^/api/ {
    rewrite  ^/api/(.*)  /$1  break;
        proxy_pass $api_path;
        proxy_redirect off;
        proxy_set_header Host $server_name ;
        proxy_set_header X-Real-IP $remote_addr ;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }

以下是 nginx 日志的详细信息

    ...top/api-aggregator-master/sandbox/lua/system/sandbox.lua: in function <...top/api-aggregator-master/sandbox/lua/system/sandbox.lua:1> while sending to client, client: 127.0.0.1, server: example.dev.xyz.com, request: "GET /aggr/conversations HTTP/1.1", host: "localhost:8090"
2013/10/02 09:21:57 [warn] 27711#0: *8 an upstream response is buffered to a temporary file /Users/santthosh.selvadurai/Desktop/api-aggregator-master/sandbox/proxy_temp/4/00/0000000004 while reading upstream, client: 127.0.0.1, server: example.dev.xyz.com, request: "GET /aggr/conversations HTTP/1.1", subrequest: "/v1/GetTopicPage", upstream: "http://XX.XX.110.48:80/api", host: "localhost:8090"
2013/10/02 09:21:57 [error] 27711#0: *8 lua entry thread aborted: runtime error: ...top/api-aggregator-master/sandbox/lua/system/sandbox.lua:86: attempt to concatenate field 'function_to_call_file' (a nil value)
stack traceback:
coroutine 0:

答案1

因为您通过 设置“Host”标头proxy_set_header,所以您可以将proxy_pass指令更改为后端服务器的实际 IP 地址,而不是依赖于 DNS。例如;

location / {
  proxy_pass http://10.0.0.2;
  proxy_set_header Host www.example.com;

  # various other required directives omitted
}

10.0.0.2这将在端口 80 上发起到 的连接,并发送Host的标头www.example.com。如果www.example.com解析为10.0.0.2,则将与以下内容相同:

location / {
  proxy_pass http://www.example.com;
}

希望这可以帮助。

相关内容