본문 바로가기
Server

window 에서 Nginx+Nodejs+SSL 설정하기

by 욧닭 2024. 12. 26.
반응형

Nginx 가 설치 되어 있다는 가정하에 작성된 포스팅 입니다 

window 서버 환경에 nginx 설치 법이 궁금하시다면 아래 포스팅을 참고 해주세요

https://yotdark.tistory.com/76

 

window 에서 nginx 설정 하기

Nginx 설치Nginx 다운로드: Nginx 공식 웹사이트에서 Windows용 최신 안정 버전(stable version)을 다운로드합니다.http://nginx.org/en/download.html nginx: download nginx.org  2. 압축 해제: 다운로드한 ZIP 파일의 압축

yotdark.tistory.com

 

nginx.conf 파일 설정

nginx 를 처음 설치했을땐 nginx.conf 파일을 먼저 설정해 준다

path 느 nginx 의 root path에 conf 폴더 내에 있다.

 

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
	 server_names_hash_bucket_size 256;
    include C:/nginx-1.26.2/conf/conf.d/*.conf;
}

 

1. worker_processes 1;

해석: Nginx가 사용할 워커 프로세스(worker process)의 수를 지정합니다.

의미: 현재 설정은 워커 프로세스를 1개로 제한합니다. 일반적으로, 서버의 CPU 코어 수에 맞추어 설정하는 것이 성능에 유리합니다.

 

2. events { worker_connections 1024; }

events 블록: Nginx에서 이벤트 처리 방식을 설정하는 섹션입니다.

worker_connections 1024;: 각 워커 프로세스가 동시에 처리할 수 있는 연결(connection) 수를 제한합니다.

의미: 각 워커 프로세스는 최대 1024개의 클라이언트 요청을 처리할 수 있습니다. 연결 수는 서버의 네트워크 처리 성능과 클라이언트 요청 수에 따라 조정합니다.

 

3. http { ... }

http 블록: HTTP 요청 및 응답에 관련된 설정을 포함합니다. 웹 서버 동작을 정의하는 핵심 섹션입니다.

 

3.1. include mime.types;

해석: 외부 파일(mime.types)을 포함하여 MIME 타입 설정을 로드합니다.

의미: Nginx가 요청된 파일의 확장자를 기반으로 적절한 Content-Type 헤더를 추가하도록 합니다.

 

3.2. default_type application/octet-stream;

해석: MIME 타입을 알 수 없는 파일에 기본으로 설정되는 MIME 타입을 지정합니다.

의미: 알 수 없는 파일 형식은 기본적으로 application/octet-stream으로 처리되며, 이는 브라우저가 파일을 다운로드하도록 유도합니다.

 

3.3. server_names_hash_bucket_size 256;

해석: 서버 이름 해시 버킷 크기를 설정합니다.

의미: 긴 도메인 이름이나 많은 수의 도메인을 처리하기 위해 메모리 최적화를 수행합니다. 도메인 이름 길이가 길거나 도메인이 많은 경우 이 값을 증가시킵니다.

 

3.4. include C:/nginx-1.26.2/conf/conf.d/*.conf;

해석: C:/nginx-1.26.2/conf/conf.d/ 경로에 있는 모든 .conf 파일을 포함합니다.

의미: 세부적인 설정을 별도의 파일로 관리할 수 있으며, 이 설정을 통해 Nginx는 여러 서버 블록(가상 호스트)을 쉽게 로드합니다.

 

nodejs 매핑 conf 파일 생성

위 nginx.conf 설정을 토대로 http include 에 기입되어 있는 conf.d 폴더에 각각 실행되고 있는 nodejs 의 도메인 주소와 localhost 에 포트별로 나눠져 있는 서버를 매핑시켜주는 conf 파일 생성을 할것입니다

 

server {
    listen 80;
    listen [::]:80;

    server_name your_domain;

    access_log D:/RESOURCES/로그가 저장될 폴더(생성해 줘야함)/access.log;
    error_log D:/RESOURCES/로그가 저장될 폴더(생성해 줘야함)/error.log;
    
    location / {
        proxy_pass http://127.0.0.1:3001;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        add_header 'Access-Control-Allow-Origin' 'http://your_domain';
    }

    # Let's Encrypt 인증 파일을 위한 경로
    location /.well-known/acme-challenge/ {
        root C:/nginx-1.26.2/html;  # 인증 파일이 저장될 경로
    }

    # HTTP로 접속 시 HTTPS로 리다이렉트
    return 301 https://$host$request_uri;
}

설명

1. listen 80;listen [::]:80;

    • 서버가 HTTP 요청을 수신할 수 있도록 IPv4와 IPv6를 모두 처리합니다.

 

 2. server_name your_domain;

    • 이 서버 블록은 도메인 요청을 처리합니다. your_domain 이라는 문자를 생성한 도메인 주소로 치환 하면 됩니다.

 

3. access_logerror_log

    • 클라이언트 요청 로그와 에러 로그를 저장하는 파일

 

4. location /

    • 루트 경로(/)로 들어오는 요청을 백엔드 서버(http://127.0.0.1:3001)로 전달.

    • proxy_set_header: 클라이언트의 IP 및 요청 정보를 백엔드 서버로 전달.

    • $host: 클라이언트가 요청한 호스트.

    • $remote_addr: 클라이언트의 실제 IP 주소.

    • $proxy_add_x_forwarded_for: 프록시를 거친 클라이언트들의 IP 목록.

    • $scheme: 요청 프로토콜(HTTP/HTTPS).

    • add_header: 프론트엔드 보안 정책을 설정. 여기서는 CORS를 허용(Access-Control-Allow-Origin)하여 해당 도메인

5. location /.well-known/acme-challenge/

    • Let’s Encrypt 인증 파일을 처리하기 위한 경로.

    • 인증 파일은 C:/nginx-1.26.2/html 디렉토리에 저장.

6. return 301 https://$host$request_uri;

    • 모든 HTTP 요청을 HTTPS로 리다이렉트.

    • 301은 영구 리다이렉트를 나타냅니다.

 

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    server_name your_domain;

    # SSL 인증서와 키 경로
    ssl_certificate D:/your_domain-chain.pem;  # SSL 인증서 경로
    ssl_certificate_key D:/your_domain-key.pem;  # SSL 개인 키 경로

    # SSL 설정
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers off;
    ssl_ciphers 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256';

    access_log D:/RESOURCES/로그가 저장될 폴더/access.log;
    error_log D:/RESOURCES/로그가 저장될 폴더/error.log;

    location / {
        proxy_pass http://127.0.0.1:3001;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        add_header 'Access-Control-Allow-Origin' 'http://your_domain';
    }
}

 

설명

1. listen 443 ssl;listen [::]:443 ssl;

    • HTTPS 요청을 처리하며 IPv4와 IPv6를 모두 지원.

    • SSL/TLS 프로토콜을 활성화.

2. ssl_certificatessl_certificate_key

    • ssl_certificate: SSL 인증서 경로 (your_domain-chain.pem).

    • ssl_certificate_key: SSL 개인 키 경로 (your_domain-key.pem).

3. ssl_protocolsssl_ciphers

    • ssl_protocols: 지원할 SSL/TLS 프로토콜을 지정. 최신 버전(TLSv1.2, TLSv1.3)만 활성화.

    • ssl_ciphers: 사용 가능한 암호화 알고리즘(암호군)을 정의.

    • ssl_prefer_server_ciphers: 클라이언트보다 서버의 암호군 우선 사용을 설정. 여기서는 off.

 

4. location /

    • HTTP 서버 블록과 동일. 백엔드 서버로 요청을 프록시합니다.

 

 

다음과 같이 정의하면 안전하게 SSL 연결을 할 수 있습니다

반응형

'Server' 카테고리의 다른 글

Nest.js 로그 서버를 구축해보자  (0) 2025.02.27
window 에서 nginx 설정 하기  (0) 2024.12.26

댓글