生产中的 Nginx(仅限 HTTP)反向代理设置

Nginx (HTTP Only) Reverse Proxy Settings in Production

我正在尝试 Nginx 并成功设置了一个简单的(目前仅 HTTP)反向代理。作为一个新手,我想知道我需要修改什么才能使这个产品准备就绪。这让我想到了以下问题:

nginx.conf:

worker_processes 1;

events {
    worker_connections 1024;
}

http {
  sendfile on;
  gzip on;
  # skip log_format/access_log

  server {
    listen 80;
    server_name server1.company.com;

    location / {
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Url-Scheme $scheme;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_pass http://server1;  # IP or FQDN would be better here
    }
  }

  server {
    listen 80;
    server_name server2.company.com;

    location / {
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Url-Scheme $scheme;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_pass http://server2;  # IP or FQDN would be better here
    }
  }
}

任何 feedback/point 方向将不胜感激。

如果您将所有 proxy_set_header 语句放在 http 块中,它们将被继承到 server 块中,然后再进入 location 块中。继承只发生在没有另一个 proxy_set_header 语句的块中。有关详细信息,请参阅 this document

或者,将通用语句放入单独的文件中,然后使用 include 指令将它们拉入配置的任何部分。有关详细信息,请参阅 this document

您应该设置哪个 headers 取决于您的应用程序。但是 this article 讨论了防止某些 headers 被传递到代理服务器,例如

proxy_set_header Accept-Encoding "";

并且 this article 通过以下方式缓解 HTTPoxy 漏洞:

proxy_set_header Proxy "";