简体   繁体   English

我目前在端口 3000 上运行我的 amazon ec2 实例。我想在端口 80 上运行它。 我该怎么做?

[英]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上运行。执行以下操作:

  1. Figure out what's already running on port 80 on your server. 找出服务器端口80上已运行的内容。 There's a good chance it's Apache. Apache很有可能。 Try using netstat to verify. 尝试使用netstat进行验证。
  2. Kill it (and prevent it from restarting). 杀掉它(并防止它重新启动)。 This will depend on whats listening. 这取决于什么是听力。
  3. Move your app to port 80 just like you've already tried. 就像你已经尝试过的那样,将你的应用移动到端口80。

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启动NodeJsExpressJs应用程序,我的意思是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.

相关问题 如何使用端口 80 而不是 3000? - How can I use port 80 instead of 3000? 我如何在80端口上使用nodemon运行我的nodejs - How can I run my nodejs with nodemon on port 80 如何使用 Amazon Elastic Beanstalk 在端口 80 上安全地运行 Node.js 服务器? - How do I safely run a Node.js server on port 80 with Amazon Elastic Beanstalk? 如何在端口 80 上运行 Node.js? - How do I run Node.js on port 80? 为什么我的应用程序在端口80而不是端口3000上侦听,因为我将其设置为在docker容器内运行? - Why does my app listen on port 80 instead of port 3000 as I set it running inside docker container? 如何在端口80上运行环回应用程序 - How can I run loopback application on port 80 为什么不能在端口3000上运行此节点应用程序? - Why can't I run this node app on port 3000? 如何设置我的应用程序/EC2 实例,以便不必将端口号放在 IP 的末尾? - How do I set up my application/EC2 instance so that I don't have to put the port number at the end of the IP? 在端口80而不是3000上运行faye(for node.js) - run faye (for node.js) on port 80 instead of 3000 当我将 SSH 插入我的 EC2 实例时,我如何确定它当前正在运行什么服务器? - When I SSH into my EC2 instance, how do I figure out what server it is currently running?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM