问题描述
网站使用Nginx配置路径转发,将/ai/**路由到http://abc.com/ai/**,但是前端出现以下错误:
<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>
看到http://batit.aliyun.com/alww.html应该是备案问题,检查Nginx配置发现:
location ^~ /ai {
proxy_pass http://abc.com;
proxy_set_header Host $Host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Host $host:$server_port;
}
其中http:/abc.com是调用别人的接口,且是有备案、接入商在阿里云,而自己的网站域名是在腾讯云接入的,没有在阿里云进行备案接入。
proxy_set_header Host $Host:$server_port;这一行的作用是把原HTTP请求中的Header中的Host字段也放到转发的请求里。
那么访问abc.com阿里云网就检测Header中的host,此时host是自己的域名,会发现没有自己的域名没有在阿里云备案,因此拦截。
解决方案
删除proxy_set_header Host $Host:$server_port;一行即可。
location ^~ /ai {
proxy_pass http://abc.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Host $host:$server_port;
}
总结
一定要多了解Nginx配置