728x90
반응형
github의 cesanta/mongoose
는 C 언어로 작성된 임베디드 웹 서버 라이브러리입니다. 이 라이브러리를 이용하면 독립적인 웹 서버 기능을 제공하며, 이를 통해 HTTPS 웹 서버를 손쉽게 구현할 수 있습니다. 다음은 cesanta/mongoose
를 사용하여 HTTPS 서버를 설정하는 방법입니다.
먼저 cesanta/mongoose
라이브러리를 다운로드하고 프로젝트에 포함합니다.
git clone https://github.com/cesanta/mongoose.git
cd mongoose/examples/http-server
HTTPS 서버를 위해 자체 서명된 SSL 인증서를 생성하거나, CA에서 구매한 SSL 인증서를 설정해야 합니다. 다음은 OpenSSL을 사용하여 자체 서명된 인증서를 생성하는 방법입니다.
openssl req -new -x509 -days 365 -nodes -out server-cert.pem -keyout server-key.pem
이 명령어로 server-cert.pem
과 server-key.pem
파일이 생성됩니다.
cesanta/mongoose
라이브러리를 사용하여 HTTPS 서버를 설정하는 예시입니다. server-key.pem
과 server-cert.pem
파일을 HTTPS 설정에 추가합니다.
#include "mongoose.h"
// HTTPS 서버 핸들러 함수
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
mg_http_reply(c, 200, "Content-Type: text/plain\r\n", "Hello, HTTPS World!\n");
}
}
int main(void) {
struct mg_mgr mgr;
struct mg_tls_opts tls_opts = {
.cert = "server-cert.pem",
.key = "server-key.pem",
};
mg_mgr_init(&mgr); // Mongoose 매니저 초기화
// HTTPS 리스너 생성
mg_http_listen(&mgr, "https://0.0.0.0:443", cb, &tls_opts);
printf("Starting HTTPS server on port 443...\n");
for (;;) mg_mgr_poll(&mgr, 1000); // 이벤트 루프
mg_mgr_free(&mgr); // Mongoose 매니저 해제
return 0;
}
- mg_tls_opts: SSL 인증서와 키 파일을 설정합니다.
- mg_http_listen: HTTPS 서버를 특정 주소와 포트에 바인딩합니다.
- mg_mgr_poll: 이벤트 루프를 통해 클라이언트의 요청을 처리합니다.
이 코드를 컴파일하여 HTTPS 서버를 구동할 수 있습니다. mongoose
는 경량 웹 서버이기 때문에 이 코드로 독립적인 HTTPS 서버를 구축할 수 있습니다.
728x90
반응형
'Web Server' 카테고리의 다른 글
레이트 리미팅(Rate Limiting)과 캐싱(Caching) 전략 (0) | 2024.11.06 |
---|