Nginx反向文件夹位置到外部网站

时间:2016-05-12 18:05:25

标签: nginx reverse-proxy

作为一个Nginx新手我试图让反向代理工作到外部域。稍后我将需要移植到内部域。当试图将代理服务器反向代理到外部域时,我似乎碰到了一堵墙而且无法找到404的响应。

目标是当我尝试访问http://localhost/example时,反向代理服务于www.example.com。

这是我的配置:

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /example/ {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://www.example.com/;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

任何提示我做错了什么?

1 个答案:

答案 0 :(得分:1)

在谈到http时,网络服务器通过检查Host标头来检测请求了哪些已配置的服务器。在这种情况下,您告诉nginx将请求代理到另一台服务器,但指示它将原始Host标头传递给该服务器。显然,远程服务器上没有配置,可以接受该域的请求。所以它用404回复你。

要使其正常工作,只需将标题更改为proxy_set_header Host www.example.com;,而不是www.example.com,而应使用与proxy_pass指令中相同的主机。 (或相同的变量)