简体   繁体   English

相对 URL 不适用于 express-http-proxy

[英]Relative URL's not working with express-http-proxy

I have a server hosting my app plus a few microservices (all of them are very simple web apps).我有一个服务器托管我的应用程序和一些微服务(它们都是非常简单的 web 应用程序)。 I would like to access these services through the internet but without opening a port for each one.我想通过互联网访问这些服务,但不为每个服务打开一个端口。 so I decided that I would set up a reverse proxy ( using express-http-proxy) on the server so that I can access all the services through a single port.所以我决定在服务器上设置一个反向代理(使用 express-http-proxy),这样我就可以通过一个端口访问所有服务。 the problem is that while I can access the main page of each service, all the relative links point to incorrect locations.问题是虽然我可以访问每个服务的主页,但所有相关链接都指向不正确的位置。

here is an example:这是一个例子:

service.js服务.js

var proxy = require('express-http-proxy');
var express = require("express")
var app = express()

app.use(express.static('public'))
app.listen(8000);

proxy.js代理.js

let express = require("express");
let proxy = require('express-http-proxy');
let app = express();
app.listen(80);

app.get("/", (req, res) => {
    res.send("hello world")
});

app.use('/service/8000/', proxy("http://localhost:8000"));

say index.html contain the following lineindex.html包含以下行

<a href="image.jpg">test image</a>

if I access the microservice through the proxy none of the relative links work.如果我通过代理访问微服务,则所有相关链接都不起作用。 and if I hover over the above link I can see where is the problem.如果我通过上面的链接 hover 我可以看到问题出在哪里。 the link looks like this链接看起来像这样

http://myserver.com/serivce/image.jpg

where it should like this它应该像这样

http://myserver.com/service/8000/image.jpg

notice the missing /8000 before image.jpg注意image.jpg之前缺少的/8000

ok, I solved it.好的,我解决了。 apparently, when you try to access a service that is behind a proxy you need to include / at the end of the URL otherwise, node-http-proxy will ignore the last part.显然,当您尝试访问代理后面的服务时,您需要在 URL 的末尾包含/否则, node-http-proxy将忽略最后一部分。 example for more clarification:更多说明的示例:

if you access this URL all the relative URLs inside of it will be missing the 8000 part http://myserver.com/service/8000如果您访问此 URL,则其中的所有相对 URL 都将丢失8000部分http://myserver.com/service/8000

while this http://myserver.com/service/8000/ does work correctly.虽然这个http://myserver.com/service/8000/工作正常。 Notice the / at the end.注意末尾的/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM