快速演示节点服务器未在端口80上收到任何请求 [英] express demo node server not receiving any request on port 80

查看:39
本文介绍了快速演示节点服务器未在端口80上收到任何请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用express创建一个Node服务器.我做了以下事情:

I'm trying to create a Node server with express. I did the following:

npm init
npm i express

并从express复制以下示例代码:

and copied this sample code from express:

const express = require('express')
const app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.listen(80, function () {
  console.log('Example app listening on port 80!')
})

在localhost上有效.在OVH的VPS上,我出现了此问题我解决的问题:

On localhost, that works. On my VPS from OVH, I got this issue that I solved with:

setcap 'cap_net_bind_service=+ep' $(which node)

我还具有以下防火墙配置:

I also have the following Firewall configuration:

# Vider les tables actuelles
iptables -t filter -F

# Vider les règles personnelles
iptables -t filter -X

# Interdire toute connexion entrante et sortante
iptables -t filter -P INPUT DROP
iptables -t filter -P FORWARD DROP
iptables -t filter -P OUTPUT DROP

# ---

# Ne pas casser les connexions etablies
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

# Autoriser loopback
iptables -t filter -A INPUT -i lo -j ACCEPT
iptables -t filter -A OUTPUT -o lo -j ACCEPT

# ICMP (Ping)
iptables -t filter -A INPUT -p icmp -j ACCEPT
iptables -t filter -A OUTPUT -p icmp -j ACCEPT

# ---

# SSH In
iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT

# SSH Out
iptables -t filter -A OUTPUT -p tcp --dport 22 -j ACCEPT

# DNS In/Out
iptables -t filter -A OUTPUT -p tcp --dport 53 -j ACCEPT
iptables -t filter -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -t filter -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -t filter -A INPUT -p udp --dport 53 -j ACCEPT

# NTP Out
iptables -t filter -A OUTPUT -p udp --dport 123 -j ACCEPT

# HTTP + HTTPS Out
iptables -t filter -A OUTPUT -p tcp --dport 80 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 443 -j ACCEPT

# HTTP + HTTPS In
iptables -t filter -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -t filter -A INPUT -p tcp --dport 443 -j ACCEPT

# FTP Out
iptables -t filter -A OUTPUT -p tcp --dport 20:21 -j ACCEPT

# FTP In
modprobe ip_conntrack_ftp # ligne facultative avec les serveurs OVH
iptables -t filter -A INPUT -p tcp --dport 20:21 -j ACCEPT
iptables -t filter -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

我想我的快递代码还可以.但是,当我尝试向我的网站提出请求时,即使在使用服务器的直接IP时,也无法在服务器上找到答案和踪迹.

I guess that my express code is ok. But when I try to make a request to my website, I get no answer and no trace of it on the server, even when using the direct IP of the server.

但是 netstat -tulpn |grep LISTEN 告诉我Node正在侦听端口80 ...

But netstat -tulpn | grep LISTEN tells me that Node is listening on port 80...

如果我在端口3000上执行所有操作,则一切正常...

If I do everything on port 3000, everything works fine...

我的节点版本为12.11.1.

I have Node version 12.11.1.

我绝对不知道下一步该怎么做才能理解这个问题...

I have absolutely no clue what to do next to understand the problem...

推荐答案

端口80是HTTP请求的默认端口,因此,当尝试访问端口80上的网站时,由于该端口可能已经存在,您可能会遇到冲突的问题正在使用.

Port 80 is the default port for HTTP requests, therefore when trying to access a website on port 80 you may be having a conflicting issue given that port may already be in use.

有很多原因不能在端口80上运行Web服务器.其中之一是,如果您的节点进程受到威胁,则可以在服务器上运行sudo命令.通常,您不应该以root用户身份在端口80上运行任何内容,但是使用反向代理和nginx可以利用端口80,因为它们具有使用降级的权限绑定到端口的正确启动代码.

There are numerous reason not to run a web server on port 80. One of these being that if your node process is compromised it would have access to run sudo commands on your server. Generally, you shouldn't be running anything as root on port 80, however using a reverse proxy and nginx can utilise port 80 given they have the correct start up code to bind to the port using downgraded permissions.

如果要创建演示应用程序,请不要在端口80或端口443上运行Express服务器,以保持最佳实践.端口3000可以很好地使用,并且应该可以正常使用.

If you're creating a demo application, stay away from running the express server on either port 80 or port 443 in order to maintain best practices. Port 3000 would be fine to use and should be ok for everything.

不确定在哪里找到示例代码,但快速文档建议在hello world示例中使用端口3000.

Not sure where you found the sample code, but express documentation recommends using port 3000 in the hello world example.

https://expressjs.com/en/starter/hello-world.html

这篇关于快速演示节点服务器未在端口80上收到任何请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆