[英]I currently run my amazon ec2 instance on port 3000. I want to run it on port 80 instead. How can I do it?
I have a node.js app that runs on port 3000
.我有一个在端口
3000
上运行的 node.js 应用程序。 I deployed it on amazon web services ( ec2
) and it works over there.我将它部署在亚马逊网络服务 (
ec2
) 上,它在那里工作。 My server.js
file says:我的
server.js
文件说:
var port = process.env.PORT || 3000;
(...)
app.listen(port);
console.log('App listening on port ' + port);
My security group in aws settings seems to have the port 80 also opened:我在 aws 设置中的安全组似乎也打开了端口 80:
so I thought it's enough to just change the var port
to = 80
and restart the server.所以我认为只需将
var port
更改为= 80
并重新启动服务器就足够了。 But when I did that I got an error:但是当我这样做时,我得到了一个错误:
bitnami@ip-172-31-47-102:~/apps/myproject/www/myproject$ sudo node server.js
App listening on port 80
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::80
at Object.exports._errnoException (util.js:856:11)
at exports._exceptionWithHostPort (util.js:879:20)
at Server._listen2 (net.js:1237:14)
at listen (net.js:1273:10)
at Server.listen (net.js:1369:5)
at Function.app.listen (/opt/bitnami/apps/myproject/www/myproject/node_modules/express/lib/application.js:542:24)
at Object.<anonymous> (/opt/bitnami/apps/myproject/www/myproject/server.js:43:5)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:430:10)
at startup (node.js:141:18)
at node.js:1003:3
I'm using the Bitnami MEAN 3.2.1-0
system on Amazon.我在亚马逊上使用
Bitnami MEAN 3.2.1-0
系统。
Also, the reason why I want to change this port is this:另外,我想更改此端口的原因是:
so far all my webservices operate on port 3000. However, I also have there a public_html
folder with the index.html
file.到目前为止,我所有的网络服务都在端口 3000 上运行。但是,我还有一个带有
index.html
文件的public_html
文件夹。 So when any user wants to display my webpage he has to enter not only the webpage, but also the port ( 3000
) which is not that convenient.所以当任何用户想要显示我的网页时,他不仅要输入网页,还要输入端口(
3000
),这不太方便。
So far the whole app stays under www.ec2-some-random-amazom-numbers.eu-west-1.compute.amazonaws.com:3000/index.html so I will buy a normal top level domain to point at it (eg. something.com ) but then - do I still need to change the port 3000 to 80 in that case?到目前为止,整个应用程序都在 www.ec2-some-random-amazom-numbers.eu-west-1.compute.amazonaws.com:3000/index.html 下,所以我会购买一个普通的顶级域名来指向它(例如 something.com )但是 - 在这种情况下,我还需要将端口 3000 更改为 80 吗? Or maybe it's common to leave apps on port other than 80?
或者也许将应用程序留在 80 以外的端口上很常见?
If the latter, then will it be possible for me to leave the port as it is and just point the top level domain on this whole long amazon one with a port 3000 at the end?如果是后者,那么我是否可以将端口保持原样,并将顶级域指向整个亚马逊上最后一个端口为 3000 的域名?
So for example: when user types www.something.com
it will redirect him to www.ec2-some-random-amazom-numbers.eu-west-1.compute.amazonaws.com:3000/index.html
?例如:当用户输入
www.something.com
它会将他重定向到www.ec2-some-random-amazom-numbers.eu-west-1.compute.amazonaws.com:3000/index.html
?
Based on the error you're getting, ( EADDRINUSE
), some other web server is listening on port 80 already on your server. 根据您获得的错误(
EADDRINUSE
),其他一些Web服务器正在侦听服务器上已有的端口80。 If you can prevent that server from running, then you can change your app to run on port 80. Do the following: 如果可以阻止该服务器运行,则可以将应用程序更改为在端口80上运行。执行以下操作:
netstat
to verify. netstat
进行验证。 Additional resources: 其他资源:
Something like this should work for you: 这样的事情对你有用:
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
You want to use iptables to forward request coming in on port 80 to port 3000 internally. 您希望使用iptables将端口80上的请求转发到内部端口3000。
If you are using the Bitnami Mean Stack then Apache is listening in port 80, hence the conflict. 如果您正在使用Bitnami Mean Stack,那么Apache正在侦听端口80,因此发生冲突。 You can either stop the bundled Apache:
您可以停止捆绑的Apache:
sudo /opt/bitnami/ctlscript.sh stop apache
Or you could add a ProxyPass rule in the Apache configuration: 或者您可以在Apache配置中添加ProxyPass规则:
https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypass https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypass
我使用nginx代理服务器进行端口转发
I have faced the same issue. 我遇到了同样的问题。
The solution is to start your NodeJs
or ExpressJs
app using Port 80
, I mean var port = 80
and start the app using sudo node bin/www
解决方案是使用端口
80
启动NodeJs
或ExpressJs
应用程序,我的意思是var port = 80
并使用sudo node bin/www
启动应用程序
My issue is not able to access the app from internet when the app is running on port 3000
: NodeJs App in AWS using port 3000 is not accessible from Internet 当应用程序在端口
3000
上运行时,我的问题是无法从Internet访问应用程序: 无法从Internet访问使用端口3000的AWS中的NodeJs应用程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.