【发布时间】:2019-03-13 20:16:40
【问题描述】:
我使用 digitalocean 来托管 2 个 node.js 应用程序,一个是暂存的,一个是生产的。两者都需要 ssl,我的服务器上有证书。但我在为两者运行 https 时遇到问题。
//socket io config
const 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: ' + port)
})
} else {
//localhost
server.listen(port, () => {
console.log('HTTP Server started on: ' + port)
})
}
我应该如何正确配置端口?
【问题讨论】:
-
如果 NODE_ENV 是生产,您希望服务器在端口 443 上运行,如果 NODE_ENV 是暂存,您希望服务器在端口 4431 上运行,对吗?不能同时进行吗?
-
@josephting 同时,它是一个 droplet(服务器)上的 2 个应用程序,但问题是如果一个正在运行,另一个会在使用默认为 443 的 SSL 端口时出错。
-
那么你只需要运行多个服务器实例。
NODE_ENV=production node index.js在一个窗口中,NODE_ENV=staging node index.js在另一个窗口中。https://api.example.co和https://api-staging.example.co:4431应该可以同时访问。 -
@josephting 我有多个服务器正在运行,api-staging.example.co:4431 无法访问我的api-staging.example.co 服务器从 3001 端口开始,而生产从 3000 端口开始。
标签: javascript node.js express nginx digital-ocean