无法命中托管在 EC2 实例上的简单节点 Web 服务器
Cannot hit simple node web server hosted on EC2 instance
无法访问我在 AWS 的 ubuntu EC2 上托管的简单节点 Web 服务器。但我看不出我错过了什么!我在 AWS 中提供了以下屏幕截图 - 我错过了什么?请帮忙!。
非常感谢,
节点代码
const http = require('http');
const hostname = '127.0.0.1';
const port = 8080;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
命令提示符
$ node index.js
命令提示响应
Server running at http://127.0.0.1:8080/
EC2 实例
安全设置
弹性 IP 设置
浏览器
http://"Public DNS (IPv4) value":8080/
更新
当你select类型时,select "Custom TCP Rule":
并在端口范围字段中输入 8080。
编辑
但是,这只会让您完成部分工作。如果您注意到,您的服务器正在侦听 IP 地址 127.0.0.1
。这意味着它不听外界,只听localhost
。要在服务器机器之外访问它,您需要将代码更改为:
const http = require('http');
const hostname = '0.0.0.0';
const port = 8080;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
变化在于,与仅本地主机相比,您现在正在 "all interfaces" 上收听。
无法访问我在 AWS 的 ubuntu EC2 上托管的简单节点 Web 服务器。但我看不出我错过了什么!我在 AWS 中提供了以下屏幕截图 - 我错过了什么?请帮忙!。 非常感谢,
节点代码
const http = require('http');
const hostname = '127.0.0.1';
const port = 8080;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
命令提示符
$ node index.js
命令提示响应
Server running at http://127.0.0.1:8080/
EC2 实例
安全设置
弹性 IP 设置
浏览器
http://"Public DNS (IPv4) value":8080/
更新
当你select类型时,select "Custom TCP Rule":
并在端口范围字段中输入 8080。
编辑
但是,这只会让您完成部分工作。如果您注意到,您的服务器正在侦听 IP 地址 127.0.0.1
。这意味着它不听外界,只听localhost
。要在服务器机器之外访问它,您需要将代码更改为:
const http = require('http');
const hostname = '0.0.0.0';
const port = 8080;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
变化在于,与仅本地主机相比,您现在正在 "all interfaces" 上收听。