Saturday 29 April 2017

Nginx - my 2nd nodes js app cannot listen to the new configured port

I've got 2 node apps in my Nginx with pm2. I had one in cluster mode, and now i started one in fork mode and 1 instance. I want 1 to listen to :3000 and the other to :4000. I've got the following configuration in my nginx.conf:

server {
listen 443 ssl;
listen [::]:443 ssl ipv6only=on;

ssl_certificate /path_to.key;
ssl_certificate_key /path_to.key;

server_name sub.domain.com;

location / {
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_pass http://localhost:3000;
}

location /blog {
    rewrite /blog(.*)$ /$1 break;
    proxy_http_version 1.1;
    proxy_pass http://localhost:4000;
    proxy_set_header Connection "";
}

}

Then my simple node.js app looks like:

var express = require('express');
var http = require('http');
var bodyParser = require('body-parser');

var app = express();

var port = 4000;

app.use(function(req, res, next) { //allow cross origin requests
    res.set('Access-Control-Allow-Origin', "*");
    res.set('Access-Control-Allow-Methods', 'GET,OPTIONS,POST,PUT,DELETE');
    res.set('Access-Control-Allow-Headers', 'X-Requested-With,content-type,accept, content-type, x-parse-application-id, x-parse-rest-api-key, x-parse-session-token');
    res.set('Access-Control-Allow-Credentials', true);
    next();
});

app.use(bodyParser.json({limit: '50mb', type:'application/json'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true, parameterLimit:100000,type:'application/x-www-form-urlencoding'}));

app.get('/',function(req, res) {
    res.status(200).send('Good. Serving.');
});

var theServer = http.createServer(app);

theServer.listen(port, function() {
    console.log('My 2nd, in fork mode, app is running on port ' + port + '.');
    process.send('ready');
});

So i was expecting that hitting sub.domain.com/blog or maybe directly to the 4000 port to get the response res.status(200).send('Good. Serving.');

I always use service nginx restart to restart the service with the new configurations.

Also in PM2 apps list i see my app online in fork mode etc. Am i missing something here?

Note: i normally see anything regarding my 1st app that listens to 3000.



via FotisK

No comments:

Post a Comment