Nginx:何时使用 proxy_set_header Host $host vs $proxy_host [英] Nginx: when to use proxy_set_header Host $host vs $proxy_host

查看:29
本文介绍了Nginx:何时使用 proxy_set_header Host $host vs $proxy_host的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读反向代理,想知道 proxy_set_header Host $host 什么时候适合 proxy_set_header Host $proxy_host.我做了一些研究,在 this文章 它说在大多数情况下我们将 Host 设置为 $host.那为什么nginx默认为$proxy_host呢?为了帮助我更具体的理解,将反向代理配置这里(文章底部)如果我们使用 $proxy_host 仍然有效吗?

I've been reading up on reverse proxying and am wondering when proxy_set_header Host $host is appropriate over proxy_set_header Host $proxy_host. I did some research and in this article it says that in most cases we set Host to $host. Then why does nginx default to $proxy_host? To help me understand more concretely, will the reverse proxy configuration here (bottom of article) still work if we use $proxy_host instead?

谢谢

推荐答案

一般来说不需要显式做proxy_set_header Host proxy_host 因为它是默认的.如果您需要通过其他指令调用服务器而不是proxy_pass指令,那么您需要通过proxy_set_header something覆盖.

In general there is no need to explicitly do proxy_set_header Host proxy_host because it's the default. If you need to call a server by something other than what is in the proxy_pass directive, then you will need to override via proxy_set_header something.

如果您想代理与server_name 指令中的相同 主机,那么您将有机会使用proxy_set_header $host.如果实际应用程序托管在另一个端口或某个内部服务器上,则通常会出现这种情况.

If you want to proxy the same host as was in your server_name directive, then you would have occasion to use proxy_set_header $host. This would commonly be the case if perhaps the actual application is hosted on another port or on some internal server.

server {
    listen 80;
    server_name site.example.com;

    location / {
       proxy_set_header Host $host;
       proxy_pass http://localhost:8080;
    }
}

如果您在上游调用的名称不是其实际的 DNS 名称,那么您可能有以下内容:

If the name you are calling the upstream is not its actual DNS name, then you might have something like:

# 192.168.2.1 responds to site.example.com, but
# site.example.com doesn't actually resolve to 192.168.2.1
proxy_pass http://192.168.2.1;
proxy_set_header Host site.example.com;

另一种情况可能是基于名称的"虚拟主机,其中上游有一个有用的 DNS 名称,但您想用其他名称来称呼它.

Another case might be for "name-based" virtual hosting where there is a useful DNS name for the upstream, but you would like to call it by another name.

proxy_pass http://origin.example.com;
proxy_set_header Host site.example.com

这篇关于Nginx:何时使用 proxy_set_header Host $host vs $proxy_host的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆