multiple ssl domain in single server multiple ssl domain in single server nginx nginx

multiple ssl domain in single server


I suggest not serving HTTPS use NodeJS.Use nginx as a reverse proxy and provide https support is a better idea.

Just create two vhost in nginx with ssl certificate and proxy_pass to your nodejs app.


Multiple Instances (Different Port)

In NodeJS, you cannot run multiple servers with a single node instance.

You will have to run multiple node instances with different NODE_ENV to have both servers run simultaneously.

Port 3000 and 3001 seems to be default port that was used by NodeJS. Make sure you have NODE_ENV specified properly.

//socket io configconst server = require('http').createServer(app)let io = require('socket.io')(server)if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'staging') {  let api_domain, https_port  if (process.env.NODE_ENV === 'production') {    api_domain = "api.example.co"    https_port = 443  } else if (process.env.NODE_ENV === 'staging') {    api_domain = "api-staging.example.co"    https_port = 4431 //this is not working  }  const credentials = {    key: fs.readFileSync(      '/etc/letsencrypt/live/' + api_domain + '/privkey.pem',      'utf8'    ),    cert: fs.readFileSync(      '/etc/letsencrypt/live/' + api_domain + '/cert.pem',      'utf8'    ),    ca: fs.readFileSync(      '/etc/letsencrypt/live/' + api_domain + '/chain.pem',      'utf8'    )  }  const httpsServer = https.createServer(credentials, app)  //socket io config  io = require('socket.io')(httpsServer)  httpsServer.listen(https_port, () => {    console.log('HTTPS Server started on: ' + https_port)  })} else {  //localhost  server.listen(port, () => {    console.log('HTTP Server started on: ' + port)  })}

Then, you want to launch 2 separate instance like so. Assuming the file above is index.js

$ NODE_ENV=production node index.jsHTTPS Server started on: 443$ NODE_ENV=staging node index.jsHTTPS Server started on: 4431

You will then be able to access to the servers with the URLs below.

Do remember to have these domains resolve to the proper IPs.

Production - https://api.example.co

Staging - https://api-staging.example.co:4431


Single Instance (Same Port)

If you want to differentiate the environment based on the hostname, you can use request.headers.host to determine where did the request came from and go from there.

However, you can only specify 1 certificate to be used per instance.

Below is one of the way you can achieve this.

const path = require('path')const fs = require('fs')const https = require('https')let app = function(req, res) {  res.writeHead(200, { 'Content-Type': 'text/plain' })  if (req.headers.host === 'prod.dev.localhost') {    res.write('Welcome to Production server.')  } else if (req.headers.host === 'stg.dev.localhost') {    res.write('Welcome to Staging server.')  } else {    res.write('Welcome!')  }  res.end()}const credentials = {  key: fs.readFileSync(    path.join(path.dirname(__filename), 'cert', 'wildcard.dev.localhost.pem'),    'utf8'  ),  cert: fs.readFileSync(    path.join(path.dirname(__filename), 'cert', 'wildcard.dev.localhost.crt'),    'utf8'  )}const httpsServer = https.createServer(credentials, app)let io = require('socket.io')(httpsServer)httpsServer.listen(443, () => {  console.log('HTTPS Server started on: ' + 443)})