정리용/nginx

[Nginx] nginx 조작-2

hee-ya07 2024. 12. 22. 00:06

안녕하세요! 이어서 조작 해보겠습니다.

일단 다 지우고 이거 두개만 남겼어요 ㅋㅋ...

하나씩 알아가봐요.

1. MIME 타입 설정

1.1 MIME 타입 (Multipurpose Internet Mail Extensions Type)

: 인터넷을 통해 전송되는 데이터의 형식을 정의하는 표준

: WS가 client에게 보내는 파일의 종류를 명확하여 클라이언트의 파일 처리를 돕기 위해 설정

type/subtype

 

  • 타입 (Type): 파일의 주요 분류
  • 서브타입 (Subtype): 세부적인 파일 형식

1.2 설정 방법

: 직접 설정과 외부에서 정의된 것을 기준으로 매핑하는 방법이 있음

nginx/conf

구분 types include mime.types
설정 위치 nginx.conf 파일 내에서 직접 MIME 타입을 설정 외부 파일인 mime.types 파일을 포함
설정 방식 설정자가 직접 필요한 MIME 타입을 정의 Nginx에서 제공하는 기본 MIME 타입 설정을 사용
사용 예시 types { text/html html; text/css css; } include mime.types;

: 왠만한건 다 설정되어있기에 include mime.types로 설정 직접 선언하기 귀찮으니깐


2. HTTP context 속 정의

: 위의 1에서의 설정을 포함하는 http 블럭 설정을 더 해볼게요.
: server, location 설정이 필요

2.1 Location context 내용 추가

: location : location 내 root 디렉토리를 기반(Root)으로 서브 디렉토리 명시

  1. location 내 root 디렉토리 : ( Root 기준) location 서브 디렉토리가 시작되는 기준 디렉토리
  2. location 내 alias 디렉토리 : location 서브 디렉토리와 동일한 디렉토리 = Alias 별명
        // 원 경로
        location /routePath {
            root /Users/원 경로;
            index routePath.html;
        }
        location /asd {
             alias /Users/원 경로/routePath;
             index routePath.html;
        }

 

   3.  try_files : try-catch 와 유사하게 기본 페이지 설정존재하지 않을때의 페이지 순서 배치

        location /path2 {
            root /Users/원 경로;
            try_files /path2/path.html /path2/path2.html /index.html=404;
            // 1. 원경로 디렉토리의 path2 디렉토리의 path.html 탐색 후 없으면 
            // 2. 원경로 디렉토리의 path2 디렉토리의 path2.html 탐색 후 없으면 
            // 3. 404 
        }

 

    4. 정규표현식을 통한 location 정의

location ~* /count/[0-9] {
		root /Users/원경로
		try_files /index.html =404;
        // 원 경로/cout/[0-9](정규식 내용)을 갖는지 탐색
}

 

     5. Redirect : 실제 웹 브라우저에서 다른 페이지로 전환하는 명령어

location /crops {
		return 307 /routerPath; // root/crops시 -> location / routerPath 설정으로 넘어감
}

 

     6. Rewrite : 귀찮은 개발자를 위한 기존 설정 재활용

rewrite ^/number/(\w+) /count/$1;

location ~* /count/[0-9] {
		root /Users/원경로;
		try_files /index.html =404;
}

// ^/number/(하나 이상의 단어 문자 =영문자, 숫자, 밑줄 _)의 경로 입력 시
/ /count/(숫자_위의 값)으로 이동
// $uri를 쓰는 연습도..

 

 

다 캡쳐하는건 빡세서 2개만 ..ㅎㅎ..

http {
    include       mime.types;
    server {
        listen 8080;
        server_name  localhost;

        root C:/Users/보낼 파일의 경로(폴더명);
        // 만약 해당 폴더의 기본 html이름이 index.html이 아니면 "index 파일명.html" 설정

        location /routePath { // 라우팅 경로
            root C:/Users/보낼 파일의 경로(폴더명);
            index routePath.html; 
        }
    }
}
events {}

 

각 파일들이 들어감을 확인 할 수 있다.

 


2.* 엥?! css파일이 적용이 안되는데?!

- 최초 로드 시 Content-Type: text/css 가 아닌 Content-Type: text/plain 으로 나옴

-> MIME Types 설정 후 재로드(Reload) 시 캐시에 의해 여전히 Content-Type: text/plain 으로

-> 200 OK (from memory cache) : 강력 새로고침을 통해 캐시를 삭제

 

 


참조

ASAC 7기 수업자료