node.js - 快速重定向错误的网址

标签 node.js express

当使用express res函数redirect(url)时,我被重定向到错误的url!

我的网站 URL 是 mywebsite.com :

function myroute(app) {
    app.get(/route, function(req, res) {
        var url = getMyUrl();
        console.log(url); // https://otherwebsite.com/foo?bar=baz [OK]
        res.redirect(url); // redirect to https://mywebsite.com/foo?bar=baz [WRONG!]
    });
}

我不知道为什么使用良好的参数快速重定向到 mywebsite.com 而不是 otherwebsite.com

不知道为什么...

此错误发生在生产环境中。在我的开发环境中,重定向 URL 是很好的 URL。

提前致谢

编辑

我也尝试过:

res.location(url);

res.setHeader(302, {Location: url});

但它总是重定向到错误的 URL... 我的 url 变量很好,但我收到此 header 响应:

HTTP/1.1 302 Found
X-Powered-By: Express
Location: https://mywebsite.com/foo?bar=baz
Vary: Accept\r\nContent-Type: text/html; charset=UTF-8
Content-Length: 170
Date: Fri, 13 Jul 2018 21:52:18 GMT
Connection: keep-alive

编辑2

这里还可以进行更多测试:

res.redirect("https://www.google.fr/toto?key=value"); // https://mywebsite.com/toto?key=value **[fail]**

res.redirect("https://www.google.fr/toto"); // https://mywebsite.com/toto **[fail]**

res.redirect("https://www.google.fr"); // https://www.google.fr **[success]**

如果我添加路径,它似乎不会重定向到我想要的域。有什么想法吗?

编辑3

也许是我的虚拟主机造成的?

<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName mywebsite.com

ProxyRequests     Off
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:5001/
<Location />
    ProxyPassReverse /
    Order deny,allow
    Allow from all
</Location>

SSLCertificateFile ...
SSLCertificateKeyFile ...
</VirtualHost>
</IfModule>

最佳答案

这次提供了一个实际的答案,因为我的上一个答案已(理所当然地)被删除了。

不正确的 URL 重定向肯定是由以下原因引起的:

ProxyPassReverse/

this该指令的作用是:

“让 Apache httpd 调整 HTTP 重定向响应上的 Location、Content-Location 和 URI header 中的 URL。”

就我而言,删除此指令会导致我的应用程序中出现其他内部重定向问题,因此我试图找到一种向其添加异常的方法。

然而,事实证明,如果我调整 Express Node js 配置的“mountpath”以匹配代理服务器的路径,我就永远不需要 ProxyPassReverse(仅 ProxyPass)。不确定这个答案是否能帮助您作为实际的解决方案,但它设法在我的 Node 应用程序上使用 Apache 作为反向代理解决了这个问题。

关于node.js - 快速重定向错误的网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51216770/

相关文章:

javascript - Mongoose 'or' 和 'like' 运营商 node.js

angularjs - 每次新建项目都要安装express、node等吗?

node.js - 如何根据环境配置express js应用程序?比如开发、登台和制作?

javascript - Array.prototype.slice.call 返回空数组

node.js - 在 Node 应用程序中使用 typescript 创建日志文件

javascript - 在 Node.js 网络中使用 pipe()

javascript - NodeJS为应用程序创建DNS名称

node.js - 为什么 npm 不能在 lubuntu 上全新安装的 Node 上工作?

javascript - Node.js 表示 post 参数始终未定义

node.js - 路由器内的快速错误处理程序不起作用