openssl genrsa -out key.pem 2048
openssl req -new -key key.pem -out csr.pem
openssl x509 -req -days 365 -in csr.pem -signkey key.pem -out cert.pem
프로젝트 루트 터미널에서 저 명령어 실행하면 됩니당
여러가지 입력 옵션들 나올텐데 다 건너뛰셔도 되고
Common Name (e.g. server FQDN or YOUR name) []:
이거 나오면 localhost 입력해주세요!!! (안그럼 경고뜸)
그러면 프로젝트 root에 cert.pem, key.pem, csr.pem 생성됩니다.
프로젝트 루트에 /certs 라는 디렉토리를 만들어주세요
아까 만들어진 cert.pem, key.pem, csr.pem 옮겨줍니다.
프로젝트 루트에 server.mjs 라는 파일 생성해주시고 아래 코드입력해주세요
import fs from "fs";
import { createServer } from "https";
import next from "next";
import path from "path";
import { fileURLToPath, parse } from "url";
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
const __dirname = fileURLToPath(new URL(".", import.meta.url));
// 인증서 경로
const httpsOptions = {
key: fs.readFileSync(path.join(__dirname, "certs", "key.pem")),
cert: fs.readFileSync(path.join(__dirname, "certs", "cert.pem")),
};
const port = process.env.PORT || 3000;
app.prepare().then(() => {
createServer(httpsOptions, (req, res) => {
const parsedUrl = parse(req.url, true);
handle(req, res, parsedUrl);
}).listen(port, () => {
console.log(`> 🚀 HTTPS server running at <https://localhost>:${port}`);
});
});
그리고 디코에 있는 환경변수 파일 .env 파일에 넣어주세요