NodeJs on nginx not working without a port number

2019-06-06 14:03发布

问题:

I have tried quite a few things already, I'm running a NodeJS app using ExpressJS on a nginx server, everything works as long as I add :3000 (or any other port) to my url

http://162.209.01.01:3000 displays the correct node app http://162.209.01.01 displays the nginx welcome page (Welcome to nginx, if you see this page...)

I want to map port 3000 to port 80 just so that I can load the node app without typing the port in the url.

This is my sites-enabled/default file:

upstream testApp {
server 127.0.0.1:3000;
    keepalive 64;
}

server {
    listen 80;
    server_name .testApp;
    access_log /var/log/nginx/simplyAsk.log;

    location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For     
            $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_pass http://162.209.01.01;
            proxy_redirect off;
            proxy_http_version 1.1;
    }
}

This is my bin/www file:

#!/usr/bin/nodejs
var debug = require('debug')('my-application');
var app = require('../app');

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

var server = app.listen(app.get('port'), function() {
    debug('Express server listening on port ' + server.address().port);
});

回答1:

Your proxy_pass statement is a little off.

Try the following.

proxy_pass http://testApp/;