docker nginx로 http static file listing/download server 만들기

IT기술/cloud, docker|2021. 1. 15. 23:21

2019-12-05 작성

 

file download server가 필요할 때가 있다. 일시적으로 사용할 때는

다음과 같이 python(혹은 ruby, nodejs) 서버를 사용한다.

python -m SimpleHTTPServer
python3 -m http.server

하지만 file server를 유지해야할 때는 test서버를 쓰기가 좀 그렇다.(nohup 명령어를 사용해서 계속 사용할 수는 있다..)

 

그래서 여러가지 방안을 고민해봤는데(samba, linux repo 등), 용도가 http download라면 웹서버가 가장 쉬운 것 같다. 그래서 쉽게 docker nginx container를 사용하려고 하니 docker Nginx Official 페이지에 다음과 같이 simple static content를 위한 명령어가 설명돼 있다.

 

서버에서 아래와 같이 실행 하고 [/내경로]에 파일을 저장하면 "http://ip:port/파일명"으로 파일을 다운 받을 수 있게 된다. 그러면 다운로드 서버는 끝.

$ docker run --name nginx -v [/내경로]:/usr/share/nginx/html:ro -d -p [내포트]:80 nginx

 

그 다음, [/내경로]에 있는 파일 목록을 웹상에서 보고 싶다.
우선 docker nginx container에 접속해 vim을 설치한다.

$ docker exec -it [container_name] bash
$ apt-get update
$ apt-get upgrade
$ apt-get install vim

 

그 다음 nginx 설정 파일을 아래과 같이 수정한다.

vim /etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    ###### 여기가 핵심
    server {
        location / {
            root /usr/share/nginx/html;
            autoindex on;
        }
    }
    ######

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

변경 후 nginx에 설정을 적용해준다.

$ nginx -t # 설정 테스트
$ nginx -s reload # 설정 반영

 

그러면 "http://ip:port/ "에서 file 목록을 확인할 수 있다. 만약 보안 문제로 목록을 보고 싶지 않다면 autoindex off로 변경한다.

 

참고사이트: https://blog.leocat.kr/notes/2018/01/08/nginx-simple-file-listing-server

'IT기술 > cloud, docker' 카테고리의 다른 글

AWS boto3 시작하기  (0) 2021.01.16
docker로 mysql 5.7 설치하기  (0) 2021.01.15

댓글()