如何阻止nginx解析upstream到ip?

2024-02-27

我想将 nginx 配置为反向代理,以将 HTTP 请求转发到外部 Cloud-API。这个nginx 但我收到连接拒绝错误。

 29 09:19:02 [error] 7#7: *2 connect() failed (111: Connection refused) while connecting to upstream, client: x.x.x.x, server: 10.0.2.2, request: "GET /apiv1/endpoint HTTP/1.1", upstream: "https://0.0.0.0:443/apiv1/endpoint", host: "localhost:8080"

当然,我将上面(外部云的)ip替换为0.0.0.0

但我认为这就是问题所在。 nginx解析云主机的ip,并将upstream url替换为ip地址。但如果没有主机名,云主机就不知道将其站点上的请求重定向到哪里。

只是猜测......因为我也无法使用curl或postman向端点(以ip作为主机)发出请求。但有了 url 就可以了。

我的 nginx.conf

upstream cloudapi {
   here-comes-the-cloud-url.com:443;
}

server {
  listen 8080 default_server;
  server_name localhost; # 

  location ^~ /apiv1/ {
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection 'upgrade';
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_cache_bypass $http_upgrade;

     proxy_pass https://cloudapi$uri;
  }
 }

您应该为后端启用 SNI。

从 nginx 1.7.0 开始,这是可能的:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ssl_server_name http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ssl_server_name

proxy_ssl_server_name on;

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何阻止nginx解析upstream到ip? 的相关文章

随机推荐