简体   繁体   中英

NGINX config with Node.js Express and TCP server on same port?

I am trying to set up my node server that uses express to serve files on port 3000 and the net library to serve a TCP server on port 5052, so:

const express = require('express');
const app = express();
const httpServer = require('http').Server(app);
const io = require('socket.io').listen(httpServer);
const path = require('path');
const net = require('net');

app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, './public/index.html'))
});

let server = net.createServer(function(socket) {
 // Left out for brevity
}

server.listen(5052, 'localhost');

httpServer.listen(3000, () => {
  console.log('Ready on port 3000');
});

Locally, this all works very well. I can load up localhost:3000 and I get my HTML served and it connects to socket.io fine. I can also connect to the server on port 5052 perfectly and life is good. I just can't get nginx to serve it all correctly. Here's what I have:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name mycoolproject.com www.mycoolproject.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;
    }

  listen 443 ssl;
  ssl_certificate /etc/letsencrypt/live/mycoolproject.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/mycoolproject.com/privkey.pem;
  include /etc/letsencrypt/options-ssl-nginx.conf;

  ssl_dhparam /etc/ssl/certs/dhparam.pem;

  if ($scheme != "https") {
      return 301 https://$host$request_uri;
  }

}

server {
    listen 5053;

    server_name mycoolproject.com www.mycoolproject.com;

    location /{
        proxy_pass http://localhost:5052;
                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;
    }
}

When I navigate to mycoolproject.com I get the site loaded fine, so the express side is working fine. I just can't connect to my server on 5053. Any ideas?

You need to configure a different port for Nginx, 5052 is busy by Node.js.

server {
    listen 5053;

    server_name mycoolproject.com www.mycoolproject.com;

    location /{
        proxy_pass http://localhost:5052;
                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;
    }
}

Then you can connect to mycoolproject.com:5053

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM