我的 Web 应用程序在尝试通过域名访问时不起作用,但如果我使用服务器的 IP 后跟端口,它会起作用

My web application does not work when trying to access it by the domain name, but it works if I use the server's IP followed by a port

免责声明:这是我第一次同时使用 node.js、Web 服务器和 Web 应用程序。

大家好,

我已经将我的第一个 node.js 应用程序上传到我的 DigitalOcean Droplet (vps)。我使用 Putty 设法访问了我的服务器,并使用终端下载了 NPMNodeMongoDBPython,最后是 Nodemon

我使用 npm 安装了所有依赖项,并且能够使用 "nodemon app.js" 启动我的服务器。然后,我使用 DigitalOcean 为我的服务器提供的 IP 地址成功连接到我的 Web 应用程序。

但是,url 自动将 IP 填充为:3000 作为端口,例如:http://123.45.678.999:3000。然后,我从GoDaddy购买了一个域名,并将域名服务器设置为ns1.digitalocean.comns2.digitalocean.comns3.digitalocean.com。在 Digital ocean 上,我将网络/DNS 设置配置为 link 我的新域名到我服务器的 IP。

但是,当我导航到我的域名时,我的网页只显示:

This page isn't working, domain didn't send any data, ERR_EMPTY_RESPONSE.

我仍然可以使用 ip:port 连接到我的网络应用程序。

我是不是漏掉了什么?我看到的每个指南/教程,人们都可以仅使用 IP 进行连接,为什么我的需要端口?任何想法为什么域不工作?任何帮助将不胜感激...

提前致谢!

当您在没有任何端口的浏览器中输入IP时,如果您使用http,它会假定您输入的是80端口。所以在浏览器中没有明确写端口的时候,会使用80端口。我认为您应该尝试 运行 端口 80 上的节点应用程序(可能来自 root 用户)。

推荐在你的nodejs应用中使用像nginx这样的反向代理:

https://medium.com/intrinsic/why-should-i-use-a-reverse-proxy-if-node-js-is-production-ready-5a079408b2ca

这也将使之后使用 letsencrypt 设置 https 变得更容易: https://certbot.eff.org/lets-encrypt/ubuntuxenial-nginx