Express is listening on port 3000 despite setting it to 80?

﹥>﹥吖頭↗ 提交于 2020-01-06 20:21:39

问题


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

Then I added lines 14:

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

And 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

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

{
  "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"
  }
}

回答1:


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

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

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. Or, you can use IpTables NAT rerouting :

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


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.

Using nginx was just straight and simple.



来源:https://stackoverflow.com/questions/34404348/express-is-listening-on-port-3000-despite-setting-it-to-80

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!