스터디/React

[Next.js] OpenSSL 적용

_leezoee_ 2023. 7. 26. 18:09

Next.js 에서 localhost  혹은 127.0.0.1:3000 로 프로그램 실행 시 openssl 을 적용하고자 한다.

 

1. 먼저 server.js 파일을 만들어 준다.

const fs = require('fs');
const https = require('https');
const next = require('next');

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

const httpsOptions = {
  key: fs.readFileSync('./private.pem'),
  cert: fs.readFileSync('./certificate.pem'),
};

app.prepare().then(() => {
  https
    .createServer(httpsOptions, (req, res) => {
      handle(req, res);
    })
    .listen(3000, (err) => {
      if (err) throw err;
      console.log('Next.js app is running over HTTPS at port 3000.');
    });
});

 

2. 저기서 사용할 private.pem 과 certificate.pem 파일을 만들어 줘야한다.

 

3. openssl 을 최신으로 설치해준다. (window 64로 다운받아 사용했음, 본인 os에 맞게 설치 진행)

https://slproweb.com/products/Win32OpenSSL.html

 

Win32/Win64 OpenSSL Installer for Windows - Shining Light Productions

Minimum system requirements: Windows XP or later 32MB RAM 200MHz CPU 30MB hard drive space Recommended system requirements: Windows XP or later 128MB RAM 500MHz CPU 300MB hard drive space March 14, 2023 - OpenSSL 3.1 is available. Also, Happy Pi Day. Users

slproweb.com

 

4. 컴퓨터 환경변수를 설정해준다.

https://aspdotnet.tistory.com/2653

 

Windows 윈도우 10 에 OpenSSL 을 설치하는 방법

OpenSSL은 TLS (Transport Layer Security) 및 SSL (Secure Sockets Layer) 프로토콜을 위한 모든 기능을 갖춘 툴킷 입니다. Apache 스타일 라이센스에 따라 라이센스가 부여됩니다. 이 튜토리얼은 Windows 운영 체제에

aspdotnet.tistory.com

 

5. cmd 창으로 openssl의 버전을 확인해본다.

openssl version

 

6. private.pem key, certificate.pem cert파일을 만든다.

*관리자 권한으로 cmd 창 열어야함

//개인키 생성
openssl genpkey -algorithm RSA -out private.pem
//인증서 요청CSR
openssl req -new -key private.pem -out csr.pem
//셀프 사인 인증서 생성
openssl req -x509 -days 365 -key private.pem -in csr.pem -out certificate.pem

 

 

8. 프로그램 폴더 경로에 넣어주고 server.js 파일로 실행한다.

 

*프로그램 경로 예시

node server.js

프로그램 실행

 

 

 

9. 구글 크롬에서 https 적용을 확인한다.