2012-03-07 56 views

回答

4

返回一个301相应的位置,如果头请求主机名== www.xyz.com

浏览器将做休息。

您可以在代理的任一侧执行此操作。但我认为这样做是有道理的这样做:

http.createServer(function (req, res) { 
    // 
    // Put your custom server logic here, then proxy 
    // 
    if (req.headers.host == 'www.xyz.com') { 
    res.writeHead(301, {'Location': 'xyz.com/' + req.url}); 
    res.end(); 
    } 
    else { 
    proxy.proxyRequest(req, res, { 
     host: 'localhost', 
     port: 9000 
    }); 
    } 

}).listen(80); 
+0

该路径也应该重定向。主机头只能包含主机名。我编辑我的帖子澄清。 – Alexander 2012-03-07 08:27:59

+0

301仍然可以工作,只需传递位置标题中的路径信息,所有这些数据都应该在请求对象中可用。 – 2012-03-07 08:29:10

+1

谢谢。我不得不写“res.writeHead(301,{'Location':'http://xyz.com/'+ req.url}); res.end();”而不是“res.send(...)”,因为send()不存在。 – Alexander 2012-03-09 01:09:58