问题描述
嗯,我会尽量说得更清楚..
Umm I'll try to be more clear..
在我用 node.js 编写的应用服务器中,我有多个端口的内部代理:
In an application server I have written in node.js, I have inner-proxy for multiple ports:
- 在我的
8080
端口中,我有我的 rest api. - 在我的
3000
端口中,我有我的推送服务器和聊天.
- in my
8080
port I have my rest api. - in my
3000
port I have my push server and chat.
我使用 npm 包 subdomain-router 进行到端口的内部路由,在前端"中暴露子域,这些子域代理回这些端口.
代码演示: (显然不是应用的真名)
I use the npm package subdomain-router for inner-routing to the port, exposing sub-domains in the 'front-end' which proxy back to those ports.
code demonstration: (<some-app>
is not the real name of the app obviously)
require('subdomain-router')
({
host: '<some-app>.herokuapp.com',
subdomains:
{
'': 8080, // <some-app>.herokuapp.com <=> ::8080 --WORKS--
'api': 8080, // api.<some-app>.herokuapp.com <=> ::8080
'chat': 3000, // chat.<some-app>.herokuapp.com <=> ::3000
'push': 3000 // push.<some-app>.herokuapp.com <=> ::3000
}
}).listen(process.env.PORT || 5000);
API 运行良好,但我无法通过 <some-app>.herokuapp.com:8080
访问它,但只能通过 <some-app>.herokuapp.com
并让内部的 subdomain-router
模块发挥作用.
另外,我无法访问子域.尝试访问 api..herokuapp.com
时,我从 heroku 收到 No such app
错误页面.
The API works great, though I cannot access it through <some-app>.herokuapp.com:8080
, but only through <some-app>.herokuapp.com
and let the inner subdomain-router
module do it's magic.
Also, I can't access the subdomains. When trying to access api.<some-app>.herokuapp.com
I get No such app
error page from heroku.
TL;DR 访问 .herokuapp.com
有效(重定向到我的 API 的 /v1
路径),但无法访问 .herokuapp.com:8080
、.herokuapp.com:3000
或 chat..herokuapp.com
.
TL;DR accessing <some-app>.herokuapp.com
works (redirects to /v1
path for my API), but unable to access <some-app>.herokuapp.com:8080
, <some-app>.herokuapp.com:3000
or chat.<some-app>.herokuapp.com
.
当试图通过在 url 中指定端口来访问我的 API 时(像这样:<some-app>.herokuapp.com:8080
),我得到我的浏览器(谷歌浏览器)中出现以下错误:ERR_CONNECTION_REFUSED
.
When trying to access my API by specifying the port in the url (like this: <some-app>.herokuapp.com:8080
), I get the following error in my browser (google chrome): ERR_CONNECTION_REFUSED
.
我有根据的猜测说这可能与在heroku中打开端口有关,但我不知道如何去做(尝试使用谷歌搜索ofc).
它并没有解释为什么我无法访问子域.
My educated guess says that it might be something related to opening ports in heroku, but I have no clue on how to do it (tried googling ofc).
It doesn't explain why I cannot access the sub-domains though.
希望能对此问题有所了解.
我是 Heroku 的新手,这让我非常沮丧.
Would appreciate any light shed on this issue.
I'm new to heroku and it's getting really frustrating.
谢谢!
阿米特
推荐答案
好的,经过一些研究,我发现在 Heroku 中打开端口被禁用和不允许强>.
Okay, after doing some research I've found out that opening ports in Heroku is disabled and not allowed.
解决此问题的唯一方法是使用子域,然后在应用程序内使用代理模块(如subdomain-router
我使用的).
The only way around this is to use sub-domains and then in-app to use a proxy module (like subdomain-router
which I use).
但是 - Heroku 不允许你在他们的域上创建子域,这意味着 your-app.herokuapp.com
是固定的,不能有子域.
在 Heroku 手册中,他们要求您拥有自己的域和 dns 提供商来执行此操作,方法是在域设置的 dns 表中创建一个 A 别名(CNAME),该别名将引用您的应用程序 herokuapp 域,然后使用命令 heroku domain:add
将您的域添加到允许的源列表中.
BUT - Heroku don't let you create sub-domains on their domain, meaning that your-app.herokuapp.com
is fixed and cannot have sub-domains.
In Heroku manuals, they demand you to have your own domain and dns provider to do such thing, by creating an A-alias (CNAME) in the dns table in your domain settings, that will refer to your app herokuapp domain, and then using the command heroku domains:add
to add your domain to the allowed origin list.
您可以在此处阅读更多内容.它提供了您需要的所有信息.
You can read more here. It provides all the info you need.
希望对大家有所帮助.
这篇关于Heroku + node.js:我有一个使用多个端口的服务器.我怎样才能让 Heroku 分配它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!