[node] nodejs + https 적용 > Flutter/Dart/Node

본문 바로가기
사이트 내 전체검색

Flutter/Dart/Node

[node] nodejs + https 적용

페이지 정보

작성자 sbLAB 댓글 0건 조회 3,804회 작성일 22-03-09 10:45

본문

1) letsencryp 설치 - ex) yesman.kr 도메인

/etc/letsencrypt/live/yesman.kr/ 아래에 파일(링크)생성됨. 


2) index.js 내용


'use strict';
const fs = require("fs");
const http = require("http");
const https = require("https");
const express = require("express");

const app = express();  // http
const apps = express(); // https


//key file path
const privateKey = fs.readFileSync("/etc/letsencrypt/live/yesman.kr/privkey.pem", "utf8");
const certificate = fs.readFileSync("/etc/letsencrypt/live/yesman.kr/cert.pem", "utf8")
const ca = fs.readFileSync("/etc/letsencrypt/live/yesman.kr/chain.pem", "utf8")


const credentials = {
    key: privateKey,
    cert: certificate,
    ca: ca
};

const httpServer = http.createServer(app); //http
const httpsServer = https.createServer(credentials, apps); //https

// 8080 port -- http
app.get("/", (req, res) => {
    console.log("------ http get / -----" + (new Date()).toLocaleString());
    console.log("req.ip => " + req.ip);
    console.log("req.hostname => " + req.hostname);
    console.log(req.url);
    console.log(req.originalUrl);
    res.send("<h1>HTTP Server running on port - 8000</h1>");
})

// 9443 port -- https
apps.get("/", (req, res) => {
    console.log("------ https get / -----" + (new Date()).toLocaleString());
    console.log("req.ip => " + req.ip);
    console.log("req.hostname => " + req.hostname);
    console.log(req.url);
    console.log(req.originalUrl);
    res.send("<h1>HTTPS Server running on port 9443</h1>");
})


httpServer.listen(8000, () => {
    console.log((new Date()).toLocaleString());
    console.log('HTTP Server running on port - 8000');
})

httpsServer.listen(9443, ()=>{
    console.log((new Date()).toLocaleString());
    console.log(`HTTPS -- listening on port 9443 ...`);
})
 


3) 

sudo ufw allow 8000/tcp       <-- - 8000 허용

sudo ufw allow 9443/tcp        <-- - 9443 허용

   

4) sudo node index.js 


* 웹서버가 공유기 아래에 있다면.  공유기에서 웹서버 내부 IP 및 8000 / 9443 포트포워딩 설정 필요.


5) 외부에서 https://yesman.kr:9443     접속

HTTPS Server running on port 9443 출력됨.



[기타 아파치 웹서버 프록시 연결 예, 일반 외부80 -> 8000 포트, ssl 아닌 경우임]


<VirtualHost *:80> 

 # DocumentRoot /home/okwind/nodeweb

  ServerName nodeweb.yesman.kr

  ProxyRequests Off

  ProxyPreserveHost On

  ProxyPass / http://localhost:8000/ 

  ProxyPassReverse / http://localhost:8000/

  #CustomLog logs/ww1.access.log common 

  #ErrorLog logs/ww1.error.log 

</VirtualHost>


6) 외부에서 http://nodeweb.yesman.kr  접속
HTTP Server running on port - 8000  출력됨.



※웹서버에 https인증서 1개 있으면, apache / nodejs 등 여러 곳에서 함께 사용가능. 

댓글목록

등록된 댓글이 없습니다.

회원로그인

접속자집계

오늘
81
어제
433
최대
1,279
전체
218,749

그누보드5
Copyright © sebom.com All rights reserved.