【问题标题】:What's the difference of $http_host:$proxy_port and $host:$server_port in NginxNginx 中 $http_host:$proxy_port 和 $host:$server_port 有什么区别
【发布时间】:2020-11-25 15:16:57
【问题描述】:

我工作的公司使用 Nginx 作为反向代理和负载均衡器,所以我只是在 nginx.conf 文件中添加另一个块,如下所示用于新服务,当使用 curl http://10.11.12.15:8080/api 请求 Nginx 服务器时,Nginx 返回 400错误请求,但是在将此配置 proxy_set_header Host $http_host:$proxy_port; 更改为 proxy_set_header Host $host:$server_port; 后解决了错误请求问题。但我不明白这一点。

我已经阅读了What's the difference of $host and $http_host in NginxPractical difference of $http_host and $host 的答案。

但是对于这种情况没有明确的答案。这个标头有什么区别?

proxy_set_header Host $http_host:$proxy_port;

proxy_set_header Host $host:$server_port;

使用过这个方块;

 upstream web-service {

    server 10.11.12.13:8080;
    server 10.11.12.14:8080 backup;
  }
  server {

    listen 8080;
    server_name myHost 10.11.12.15;
    location / {

      proxy_pass http://web-service;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header Host $http_host:$proxy_port;  # Throw HTTP 400 Bad Request
  #   proxy_set_header Host $host:$server_port;      # Working solution
    }
  }

【问题讨论】:

    标签: nginx nginx-reverse-proxy nginx-config


    【解决方案1】:

    $http_host 已包含非默认端口。因此,如果您再次尝试添加端口,则会出错。 $host 不包含端口。

    【讨论】:

      猜你喜欢
      • 2013-03-03
      • 1970-01-01
      • 2011-08-30
      • 1970-01-01
      • 2018-12-21
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      相关资源
      最近更新 更多