-
[Docker + nginx] 로 static 파일 서버 만들기 (Windows)nginx 2020. 11. 19. 19:32반응형
Windows에서 docker와 nginx 로 static 파일 서버 만드는 방법 공유하겠습니다.
Docker Hub에 있는 nginx 이미지를 기반으로 서버를 구축했습니다.
nginx - Docker Hub
Supported tags and respective Dockerfile links 1.17.6, mainline, 1, 1.17, latest 1.17.6-perl, mainline-perl, 1-perl, 1.17-perl, perl 1.17.6-alpine, mainline-alpine, 1-alpine, 1.17-alpine, alpine 1.17.6-alpine-perl, mainline-alpine-perl, 1-alpine-perl, 1.17
hub.docker.com
Docker 관련해서는 하기 subicura님 블로그를 참고하시면 많은 도움이 될것 같습니다.
subicura.com/2017/01/19/docker-guide-for-beginners-1.html
초보를 위한 도커 안내서 - 도커란 무엇인가?
도커를 처음 접하는 시스템 관리자나 서버 개발자를 대상으로 도커 전반에 대해 얕고 넓은 지식을 담고 있습니다. 도커가 등장한 배경과 도커의 역사, 그리고 도커의 핵심 개념인 컨테이너와 이
subicura.com
1. project 목록
- Dockerfile
- nginx.conf
- static file을 저장하는 폴더: static_files
2. Dockerfile 작성
Docker Image의 /etc/nginx/nginx..conf 파일을 현재 directory의 nginx.conf파일로 대체 합니다.
하기 파일을 작성하는 이유는 container에서 nginx docker에서 기본적으로 제공하는 nginx.conf를 사용하는것이 아니라 새롭게 작성한 conf 파일을 사용하겠다는것입니다.
FROM nginx COPY nginx.conf /etc/nginx/nginx.conf
3. nginx.conf 작성
nginx.conf파일은 nginx의 설정파일입니다.
docs.nginx.com/nginx/admin-guide/basic-functionality/managing-configuration-files/
NGINX Docs | Creating NGINX Plus and NGINX Configuration Files
Understand the basic elements in an NGINX or NGINX Plus configuration file, including directives and contexts.
docs.nginx.com
events {} http { server { listen 8080; root /static_files; location /files/ { add_header Content-disposition 'attachment; filename="$1"'; } location / { index index.html index.htm; } } }
- server는 8080 port를 사용함.
- 해당 server의 root 폴더는 /statick_files.
- localhost:8080 url에 대해서는 root 폴더의 index.html 를 send 함. index.html이 없으면 index.htm 파일을 send 함.
- localhost:8080/files/${filename} 에 대해서는 root 폴더의 ${filename} file를 attachment 설정하여 client에 보냄(.html, .txt 파일도 browser에서 attachment로 보고 download하게 됨)
4. docker image build 및 실행
- 빌드: docker build -t test-nginx .
- test-nginx라는 image를 생성 함.
- 실행: docker run --name test -it -v D:\source\nginx\data:/static_files -p 8080:8080 test-nginx
- --name: 실행하는 container 명을 지정. 여기에서 container 이름이 test 임.
- -v: volume 지정. local에 있는 폴더를 container의 지정 폴더로 mount하게 됨. 여기에서는 local의 D:\source\nginx\data 폴더를 container의 /statci_files로 mount함.
- -p local의 port와 container의 port를 연결 함.
- Troubleshooting: 저는 Windows에 cygwin을 깔고 pwd로 얻은 local 폴더위치를 위의 -v 명령에 넣어서 실행했는데 해당 폴더가 container에 mount되지 않았습니다. 어떻게든 되지 않아서 혹시나 해서 powershell에서 위와 똑같이 실행하니 제대로 mount 되었습니다. 참고하시기 바랍니다.
위와 같이 docker를 실행하고 localhost:8080을 입력하면 index.html이 정상적으로 실행됨을 확인할 수 있습니다.
localhost:8080/files/index.html 로 입력하면 index.html이 다운로드 되는것도 확인할 수 있습니다.
반응형'nginx' 카테고리의 다른 글
nginx javascript 모듈 - njs (0) 2022.08.29 nginx 요청 처리 단계(Phases) 관련 (0) 2022.08.29