如何将nginx设置为反向代理(堡垒);我有“400Bad 请求:主机头太多”

use*_*443 3 reverse-proxy nginx

我尝试设置 nginx 来验证传入的 https 请求并将它们传递到同一 Intranet (LAN) 中不同主机上的服务器。我从不同的来源得到了以下 conf 文件:

    proxy_redirect              off;
    proxy_set_header            Host            $http_host;
    proxy_set_header            X-Real-IP       $remote_addr;
    proxy_set_header            X-Forwared-For  proxy_add_x_forwarded_for;

upstream syncthing_gui 
  {
      server 10.0.0.129:8329; 
  }

server {
  listen       443 ssl;
  server_name  geras.duckdns.org;

    ssl    on;
    ssl_certificate    /etc/letsencrypt/live/geras.duckdns.org/fullchain.pem;
    ssl_certificate_key    /etc/letsencrypt/live/geras.duckdns.org/privkey.pem;

    auth_basic "Username and Password required (syncthing)";
    auth_basic_user_file /etc/nginx/.htpasswd;

  location /sync {

    error_log /var/log/nginx/error.log info; 
    access_log /var/log/access.log;
    proxy_pass http://syncthing_gui;
}
Run Code Online (Sandbox Code Playgroud)

}

但我得到了错误400 Bad Request: too many Host headers。我需要改变什么?

小智 6

这个消息不是来自 nginx,而是来自Golang(Syncthing 是用 Go 编写的)。乍一看,我将此错误解释为发送到 nginx 的标头过多或过大。但它真正的意思是Host: abc.example.com标题太多了。

在您的配置中,您有

proxy_set_header Host $http_host;

我敢打赌,在其他一些配置文件中,您有重复或类似的行,这会导致 nginx 向服务器发送两个 HTTPHost:标头。大多数软件并不关心这一点,但 Go 的标准 HTTP 服务器会对此进行检查并拒绝400请求。