Home > front end >  Expressjs app - HTTPS shows directory listing instead of site
Expressjs app - HTTPS shows directory listing instead of site

Time:09-17

I have an app currently in development. All works well on my local machine but on my live server I am getting an issue I can't solve between the http and https versions of the site. Anyone know why I am getting a difference between the display of these two domains?

http://maksemus.tech

https://maksemus.tech

I have installed the SSL certificate on the server but have been stuck for days trying to figure out what is happening here.

Here is what my www file looks like:

#!/usr/bin/env node

/**
 * Module dependencies.
 */
const app = require('../app')
const debug = require('debug')('maksemus.tech:server')
const spdy = require('spdy')
const fs = require('fs')
const http = require('http')
const https = require('https')
const privateKey = fs.readFileSync('./mkcert/maksemus_tech_key.key')
const certificate = fs.readFileSync('./mkcert/maksemus_tech_cert.crt')

const options = {
  // Private key
  key: privateKey,

  // Fullchain file or cert file (prefer the former)
  cert: certificate,
}

/**
 * Get port from environment and store in Express.
 */

const httpPort = normalizePort(process.env.HTTPPORT)
const httpsPort = normalizePort(process.env.HTTPSPORT)

/**
 * Create HTTP/HTTPS/SPDY server.
 */

httpServer = http.createServer(app)
httpServer.listen(httpPort)
httpServer.on('error', one rror)
httpServer.on('listening', onListening)

httpsServer = spdy.createServer(options, app)
httpsServer.listen(httpsPort)
httpsServer.on('error', one rror)
httpsServer.on('listening', onListening)


/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind1 = typeof httpPort === 'string' ? 'Pipe '   httpPort : 'Port '   httpPort
  var bind2 = typeof httpsPort === 'string' ? 'Pipe '   httpsPort : 'Port '   httpsPort

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind1   ' requires elevated privileges')
      console.error(bind2   ' requires elevated privileges')
      process.exit(1)
      break
    case 'EADDRINUSE':
      console.error(bind1   ' is already in use')
      console.error(bind2   ' is already in use')
      process.exit(1)
      break
    default:
      throw error
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  var addr1 = httpServer.address()
  console.log(addr1)
  var addr2 = httpsServer.address()
  console.log(addr2)
  var bind1 = typeof addr1 === 'string' ? 'pipe '   addr1 : 'port '   addr1.port
  debug('HTTP Server Listening on '   bind1)
  var bind2 = typeof addr2 === 'string' ? 'pipe '   addr2 : 'port '   addr2.port
  debug('HTTPS Server Listening on '   bind2)
}```

CodePudding user response:

After lots of debugging I found what was causing this issue. This was caused by Phusion Passengers instantiation of the application.

It simply uses two different paths when initializing the app. One for http and one for https.

The standard path it uses for http config is:

/etc/apache2/conf.d/userdata/std/2_4/YOUR_USERNAME/YOUR_DOMAIN/YOUR_APP_NAME.conf

Not sure why but Phusion Passenger does not automatically create the correct path for SSL when you register your application in cpanel, meaning it can not find a path for https application config.

I had to manually create the folders and then copy the config file from the above path to it. It looks like this:

/etc/apache2/conf.d/userdata/ssl/2_4/YOUR_USERNAME/YOUR_DOMAIN/YOUR_APP_NAME.conf

If you update one config file you need to update the other as they should be identical.

The command line from the [cpanel documentation][1] did not work for me because the folders did not exist and were not automatically created when the app was registered.

The command line I'm referring to from the above documentation is:

cp -a /etc/apache2/conf.d/userdata/std/2_4/username/example.com/application-name.conf /etc/apache2/conf.d/userdata/ssl/2_4/username/example.com/application-name.conf

Anyhow, once you have created both paths you need to rebuild the http config file for apache and restart apache.

/usr/local/cpanel/scripts/rebuildhttpdconf
/usr/local/cpanel/scripts/restartsrv_httpd

Hope this saves someone else the nightmare.

  • Related