configurationCentOS 6 iptables使用端口80运行在端口3000上的NodeJS应用程序

我正在使用AWS运行一个CentOS 6的新实例。 iptables中有一些东西阻塞了我的nodejs应用程序。

这是我的nodejs应用程序:

var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!'); }); var server = app.listen(3000, function () { var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); }); 

按照这个指南 ,我尝试用下面的命令为我的应用添加一个条目:

 sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000 

当我运行node.js应用程序并尝试通过访问ec2-XXX-XXX-XXX-XXX.YYY.compute.amazonaws.com在浏览器中访问它时,我的浏览器就挂起了。

我已经确认,iptables没有做我想做的事情,因为每当我运行命令service iptables stop并转到地址ec2-XXX-XXX-XXX-XXX.YYY.compute.amazonaws.com:3000应用程序得到服务。 但是,我猜这不是一个好主意,不运行iptables。

Node.js正在运行和监听,这里是netstat -tulpn的输出:

 Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN 1364/node tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN - tcp 0 0 :::22 :::* LISTEN - tcp 0 0 ::1:25 :::* LISTEN - udp 0 0 0.0.0.0:68 0.0.0.0:* - 

以下是我运行命令iptables -L

 Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT icmp -- anywhere anywhere ACCEPT all -- anywhere anywhere ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) target prot opt source destination REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) target prot opt source destination 

记得实际上也开放80端口到世界。

完整的命令列表需要:

 sudo iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000 

还要记得保存iptablesconfiguration。 这应该通过以下命令完成:

 sudo iptables-save > /etc/sysconfig/iptables 

你也可以尝试看看它是否确实是iptables,通过在防火墙中打开端口3000,但仍然有效:

 sudo iptables -I INPUT 1 -p tcp --dport 3000 -j ACCEPT