访问Nginx指定了某些Host请求头 请求499的原因

通过Nginx跳转访问一个第三方接口(如 ip3:8080/foo)

location /foo {
    proxy_pass http://ip3:8080/foo/;
}

本地 ==> 阿里云服务器(ip2)上的Nginx ==> 第三方接口
(第三方接口有白名单限制 ip2在白名单中)

发现了这么一个奇怪的问题

当本地通过Nginx访问的时候 指定了某些特殊的Host请求头 就会访问不成功 如下所示

➜  ~ curl 'http://ip2/foo' -H 'Host: bar.com'

<html>
<head>
<meta http-equiv="Content-Type" content="textml;charset=UTF-8" />
   <style>body{background-color:#FFFFFF}</style>
<title>TestPage184</title>
  <script language="javascript" type="text/javascript">
         window.onload = function () {
           document.getElementById("mainFrame").src= "http://batit.aliyun.com/alww.html";
            }
</script>
</head>
  <body>
    <iframe style="width:860px; height:500px;position:absolute;margin-left:-430px;margin-top:-250px;top:50%;left:50%;" id="mainFrame" src="" frameborder="0" scrolling="no"></iframe>
    </body>
      </html>

对应的Nginx日志是

222.128.172.216 - - [01/Nov/2018:12:37:39 +0800] "GET /foo HTTP/1.1" 499 0 "-" "curl/7.43.0" "-"

返回状态码是499

但是在Nginx所在的服务器(ip2)上 直接访问第三方接口 同样的Host请求头却没有问题

curl 'http://ip3:8080/foo' -H 'Host: bar.com'

并且本地通过Nginx访问的时候不指定Host或者其他任意Host也都没有问题 如

curl 'http://ip2/foo'
curl 'http://ip2/foo' -H 'Host: 111.com'
curl 'http://ip2/foo' -H 'Host: 222.com'

为什么通过Nginx访问的时候 有些Host请求头(如 foo.com bar.com aaa.com之类)就会有问题呢 但是同样的Host请求头直接访问第三方接口就没问题呢?

现在的解决方法

Nginx改成监听81端口后 问题就解决了

curl 'http://ip2:81/foo' -H 'Host: bar.com'

这又该怎么解释呢?

阅读 4.6k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题