Skip to Content
Suffering builds character
아카이브8.Nginx특징정적 리소스 호스팅7. MIME 타입으로 Content-Type 적용하기

7. MIME 타입으로 Content-Type 적용하기

Nginx는 각 MIME 타입에 대한 값들을 별도의 파일로 명시해두었음

따라서 개발자는 nginx.conf 파일에 정적 리소스의 Content-Type별 값들을 일일이 명시해주지 않아도 됨

Note

MIME 타입
MIME type은 파일의 형식, 확장자를 나타내는 문자열로 파일과 같이 송수신되며, 전송되는 컨텐츠의 형식을 나타내기 위해 사용

예를 들면 오디오 파일은 audio/ogg로 그림 파일은 image/png로 분류할 수 있음

적절한 MIME 타입을 적용하기

1. Nginx가 미리 정의한 파일을 통해 Content-Type 적용하기

MIME 타입이 정의된 파일의 경로 조회

terminal
# 현재경로: C:\nginx cd conf ls

fastcgi.conf koi-utf mime.types nginx.conf.backup uwsgi_params
fastcgi_params koi-win nginx.conf scgi_params win-utf

mime.types: MIME 타입들이 정의된 파일

파일의 내용 조회

terminal
cat mime.types
mime.types
types { text/html html htm shtml; text/css css; text/xml xml; #... }

→ text/html 부터 css 등 다양한 리소스별 컨텐츠 타입들이 이미 정의되어 있음

설정 파일을 통해 Nginx가 mime.types 파일을 기반으로 컨텐츠를 서빙할 수 있도록 적용

nginx.conf
events {} http {     include mime.types; # mime.types 파일을 설정 파일에 포함하도록 적용 # 직접 작성했던 types 블록 제거 # types { # text/html html; # text/css css; # }         server {         listen 80;         server_name localhost;                 root nginx-practice;         index index.html;     } }
💡
Tip

mime.types의 경로
현재 mime.types 파일은 nginx.conf 파일과 같은 경로에 위치해있기 때문에 nginx.conf에 파일명만 표기해도 동작함

설정 파일 재적용 후 다시 테스트

nginx.conf
nginx -s reload

실행 결과

terminal
curl -I localhost/style.css # 스타일 시트 요청 HTTP/1.1 200 OK Server: nginx/1.26.2 Date: Sun, 12 Jan 2025 05:03:46 GMT Content-Type: text/css # Content-type이 text/css로 적용됨 Content-Length: 521 Last-Modified: Sun, 12 Jan 2025 04:59:04 GMT Connection: keep-alive ETag: "67834c18-209" Accept-Ranges: bytes

→ 개발자가 직접 types 블록으로 명시하지 않아도 Content-Type이 잘 적용되었음

Last updated on