【问题标题】:Expressjs app - HTTPS shows directory listing instead of siteExpressjs 应用程序 - HTTPS 显示目录列表而不是站点
【发布时间】:2021-09-10 14:57:35
【问题描述】:

我目前正在开发一个应用程序。在我的本地机器上一切正常,但在我的实时服务器上,我遇到了一个我无法在网站的 http 和 https 版本之间解决的问题。有谁知道为什么我在这两个域的显示上有所不同?

http://maksemus.tech

https://maksemus.tech

我已经在服务器上安装了 SSL 证书,但几天来一直试图弄清楚这里发生了什么。

这是我的 www 文件的样子:

#!/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', onError)
httpServer.on('listening', onListening)

httpsServer = spdy.createServer(options, app)
httpsServer.listen(httpsPort)
httpsServer.on('error', onError)
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)
}```

【问题讨论】:

    标签: node.js apache express http https


    【解决方案1】:

    经过大量调试,我找到了导致此问题的原因。这是由应用程序的 Phusion 乘客实例化引起的。

    它在初始化应用程序时只是使用了两个不同的路径。一个用于 http,一个用于 https。

    它用于 http 配置的标准路径是:

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

    不知道为什么,但是当您在 cpanel 中注册应用程序时,Phusion Passenger 不会自动为 SSL 创建正确的路径,这意味着它找不到 https 应用程序配置的路径。

    我必须手动创建文件夹,然后将配置文件从上述路径复制到它。它看起来像这样:

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

    如果您更新一个配置文件,您需要更新另一个,因为它们应该是相同的。

    [cpanel 文档][1] 中的命令行对我不起作用,因为这些文件夹不存在并且在注册应用程序时没有自动创建。

    我在上述文档中所指的命令行是:

    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
    

    无论如何,一旦你创建了两个路径,你需要为 apache 重建 http 配置文件并重启 apache。

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

    希望这可以避免其他人的噩梦。

    【讨论】:

      猜你喜欢
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 2014-06-28
      相关资源
      最近更新 更多