logoStephen's 기술블로그

포스트 검색

제목, 태그로 포스트를 검색해보세요

#008 #location 라우팅

#008 #location 라우팅
Docker
성훈 김
2024년 6월 12일
목차

👉 ex05 : 로케이팅

notion image
Dockerfile 생성
Dockerfile 생성
notion image
HTML 폴더 생성
HTML 폴더 생성
 

🔹 Dockerfile 작성

JavaScript
FROM nginx

COPY html /usr/share/nginx/html
COPY conf/nginx.conf /etc/nginx/conf.d/default.conf

ENTRYPOINT ["nginx", "-g", "daemon off;"]
전체 동작 설명
전체 동작 설명
  1. 이미지 기반 설정: nginx 이미지를 기반으로 새로운 이미지를 빌드
  1. 정적 파일 복사: 현재 빌드 컨텍스트의 html 디렉토리 안에 있는 모든 파일을 /usr/share/nginx/html 디렉토리로 복사하여 Nginx가 이 파일들을 서빙
  1. Nginx 설정 파일 복사: 현재 빌드 컨텍스트의 conf/nginx.conf 파일을 Nginx 설정 파일로 복사하여 Nginx의 동작을 설정.
  1. 엔트리포인트 설정: 컨테이너가 시작될 때 Nginx를 포그라운드 모드에서 실행하여 컨테이너가 종료되지 않도록 함
 

👉 nginx.conf 구조

notion image
location 에서 / 요청이 들어오면 root로 가서 index를 실행한다는 라우팅이 가능하다.
location 에서 / 요청이 들어오면 root로 가서 index를 실행한다는 라우팅이 가능하다.
notion image
conf폴더 생성 
50x.html 생성
conf폴더 생성 50x.html 생성
 

🔹 nginx.conf 전체코드

JavaScript
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
}
 

🔹 index.html 전체 코드

JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Welcome Nginx</h1>
</body>
</html>
 
notion image
다시 작성해서 실행
다시 작성해서 실행
 
notion image
루트 경로 / 로 요청했는데도, 페이지가 잘 반환되었다.
에러페이지 자동 라우팅은 잘 모르겠다.
루트 경로 / 로 요청했는데도, 페이지가 잘 반환되었다. 에러페이지 자동 라우팅은 잘 모르겠다.