作为一个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;
}
}
任何提示我做错了什么?
答案 0 :(得分:1)
在谈到http时,网络服务器通过检查Host
标头来检测请求了哪些已配置的服务器。在这种情况下,您告诉nginx将请求代理到另一台服务器,但指示它将原始Host
标头传递给该服务器。显然,远程服务器上没有配置,可以接受该域的请求。所以它用404
回复你。
要使其正常工作,只需将标题更改为proxy_set_header Host www.example.com;
,而不是www.example.com
,而应使用与proxy_pass
指令中相同的主机。 (或相同的变量)