Express将端口3000设置为80时仍在监听端口3000? [英] Express is listening on port 3000 despite setting it to 80?

查看:556
本文介绍了Express将端口3000设置为80时仍在监听端口3000?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Amazon EC2上运行.该代码是由express-generator自动创建的.

Running on Amazon EC2. The code was created automatically by express-generator.

要启动该应用程序,我必须这样做:

To start the app I had to do this:

sudo PORT = 80 npm start

sudo PORT=80 npm start

然后我添加了第14行:

Then I added lines 14:

app.set('port', process.env.PORT || 80);

还有66

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));

现在,我可以说以下内容来启动应用程序:

Now I can start the app by saying:

sudo npm start

sudo npm start

但是它说-Express服务器正在侦听端口3000.我可以从浏览器访问它,但是为什么是3000

but it says - Express server listening on port 3000. I can access it from my browser, but why 3000

http://pastebin.com/bwcBHZaa

Package.json

Package.json

{
  "name": "haha",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.13.2",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "express": "~4.13.1",
    "express-generator": "^4.13.1",
    "jade": "~1.11.0",
    "kerberos": "file:kerberos",
    "mongodb": "^2.0.52",
    "morgan": "~1.6.1",
    "serve-favicon": "~2.3.0"
  }
}

推荐答案

我只是有一个简单的主意.几年前,我也发生了同样的事情.

I just had a flash idea. This same thing happened to me years ago.

问题是,绑定到端口80非常复杂.

The thing is, it's very complicated to bind to the port 80.

两种解决方案中的任何一种都可以使用,但为简单起见,我建议第一种解决方案.

Either one of the two solutions will work, but I recommend the first one for simplicity.

  1. Nginx

您可以使用非常简单的nginx配置将端口3000请求重新路由"到端口80.

You can use a very simple nginx configuration to "re-route" port 3000 request to the port 80.

#etc/nginx/sites-enabled/mysite.conf

server {
        listen 0.0.0.0:80;
        server_name www.mysite.com mysite.com;
        location /  {
                proxy_pass http://localhost:3000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }

}

  1. 或者,您可以使用IpTables NAT重路由:

https://glassonionblog.wordpress.com/2011/04/08/tomcat-redirecting-traffic-from-port-8080-to-80-using-iptables/

我发现使用nginx通常通常会更好,原因有多种(例如,如果您重新启动服务器).重新启动服务器意味着您必须在重新启动时重新加载ipTables.如果要自动执行此操作,则意味着必须安装iptables-restore.但是使用NAT重路由表进行配置很麻烦.

I found that using nginx just worked better in general, for multiple reasons (e.g. in case you restart your server). Restarting your server means you have to reload ipTables on restart. If you want to automate this, that means you have to install iptables-restore. But it's awkward to configure with NAT rerouting tables.

使用nginx既简单又简单.

Using nginx was just straight and simple.

这篇关于Express将端口3000设置为80时仍在监听端口3000?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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