-
Here is my server // server.ts
import { Ignitor } from '@adonisjs/core/build/standalone';
import { existsSync, readFileSync } from 'fs';
import { createServer as createHttpServer } from 'http';
import { createServer as createHttpsServer } from 'https';
import 'reflect-metadata';
import sourceMapSupport from 'source-map-support';
sourceMapSupport.install({ handleUncaughtExceptions: false });
new Ignitor(__dirname).httpServer().start((handle) => {
const sslKey = '/etc/letsencrypt/live/rgi-api.guoyunhe.me/privkey.pem';
const sslCert = '/etc/letsencrypt/live/rgi-api.guoyunhe.me/fullchain.pem';
if (existsSync(sslKey) && existsSync(sslCert)) {
return createHttpsServer(
{
key: readFileSync(sslKey),
cert: readFileSync(sslCert),
},
handle
);
} else {
return createHttpServer(handle);
}
}); if you need the full repo, it is here https://github.com/guoyunhe/rgi-api When I curl the site url, it refused connection.
However, a simple server like this: const https = require(`https`);
const fs = require(`fs`);
const certDir = `/etc/letsencrypt/live`;
const domain = `rgi-api.guoyunhe.me`;
const options = {
key: fs.readFileSync(`${certDir}/${domain}/privkey.pem`),
cert: fs.readFileSync(`${certDir}/${domain}/fullchain.pem`)
};
https.createServer(options, (req, res) => {
res.writeHead(200);
res.end(`hello world\n`);
}).listen(443); Works perfectly:
So I think it is not a network or firewall issue. Can you help? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Finally, I figured it out. The ReasonLet's say my website domain is
Solution 1Set system environment variables. If you only have one app on the server, this way is better. Create a file
Solution 2Run node with inline environment variables. If you have multiple apps on the server and they rely on different
Solution 3 from @RomainLanzDon't let your node app deal with SSL. Let your reverse proxy (Nginx, Traefik, Caddy, etc.) deal with it. |
Beta Was this translation helpful? Give feedback.
Finally, I figured it out.
The Reason
Let's say my website domain is
api.example.com
.HOST=localhost
..env
files. So yourHOST=api.example.com
in your.env
file is always ignored.HOST
is stilllocalhost
..listen({ host: this.application.env('HOST'), port: this.application.env('PORT') })
. For HTTPS server, it accepts only requests whose domain matchHOST
, which is stilllocalhost
.https://api.example.com/
, server refuses your connection.Solution 1
Set system environment variables. If you only have one app on…