도커로 nginx https

airwalk·2024년 1월 12일

docker를 통해서 https로 웹서버를 열어야할 일이 생김
서비스를 여러개로 해야해서 docker-compose로 함.

HTTPS

  • HTTP + Secure(보안 기능) : HTTP 프로토콜 내용을 암호화

nginx

  • https 프로토콜로 웹서버를 올리기 위해서는 ssl인증서가 필요함.
  • 도메인이 없는 상황에서 사설인증서를 이용하기로 했음.
  • docker를 통해서 사설 인증서를 자동으로 발급 받아 이를 참조하기로 함.

코드

  • docker-compose

version: "3"
services:
  client:
    restart: always
    build:
      context: ./client
      dockerfile: Dockerfile
    image: client/app:1.0.1
    ports:
      - "${EXPOSE_PORT_WEB_APP}:80"
    volumes:
      - ./client/nginx.conf:/etc/nginx/sites-available/default.conf
    env_file: .env
  • Dockerfile
# nginx 이미지를 사용합니다. 뒤에 tag가 없으면 latest 를 사용합니다.
FROM nginx

# root 에 app 폴더를 생성
RUN mkdir /app

# work dir 고정
WORKDIR /app

# work dir 에 build 폴더 생성 /app/build
RUN mkdir ./build


# host pc의 현재경로의 build 폴더를 workdir 의 build 폴더로 복사
ADD ./build ./build

# ADD ./certs ./certs

RUN chmod 777 /etc/nginx/*

# nginx 의 default.conf 를 삭제
RUN rm /etc/nginx/conf.d/default.conf

RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/conf.d/nginx.key -out /etc/nginx/conf.d/nginx.crt -subj "/C=KR/ST=Daejeon/L=Daejeon/O=조직이름(선택)/CN=선택"

# host pc 의 nginx.conf 를 아래 경로에 복사
COPY ./nginx.conf /etc/nginx/conf.d

# 80 포트 오픈
EXPOSE 80

# container 실행 시 자동으로 실행할 command. nginx 시작함
CMD ["nginx", "-g", "daemon off;"]
  • nginx.conf
server {

    listen 443 ssl;

    ssl_certificate /etc/nginx/conf.d/nginx.crt;
    ssl_certificate_key /etc/nginx/conf.d/nginx.key;

    location / {
        root    /app/build;
        index   index.html;
        try_files $uri $uri/ /index.html;
    }

}

0개의 댓글