devops

docker volume을 이용하여 Nginx 배포하기(가비아 도메인 연결)

yougeun 2024. 4. 2. 17:14
728x90

1.가비아도메인 A 레코드 설정

1-1. DNS관리툴 접속

 

My 가비아 -> DNS 관리 툴에서 설정 할 도메인을 선택합니다.

1-2 DNS 설정

 

레코드 설정을 누른 후 타입에 A 호스트에 www,@을 입력하고 값/위치에 public ip를 입력하고 저장합니다.

2.Nginx 이미지 Pull

docker pull nginx

 

위 명령어를 통해 nginx 이미지를 들고옵니다.

3.Nginx 볼륨 설정

1.nginx.conf

#nginx.conf 파일 생성
vim nginx.conf
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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;

        sendfile        on;
        #tcp_nopush     on;

        keepalive_timeout  65;

        #gzip  on;

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

2. domain.conf 설정

#default.conf 파일 생성
vim domain.conf
server {
        listen       80;
        listen  [::]:80;
        server_name  domain;

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

        location / {
                root   /usr/share/nginx/html;
                index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                root   /usr/share/nginx/html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
                #    proxy_pass   http://127.0.0.1;
                #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
                #    root           html;
                #    fastcgi_pass   127.0.0.1:9000;
                #    fastcgi_index  index.php;
                #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
                #    include        fastcgi_params;
                #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
                #    deny  all;
                #}
        #yougeun
}

 

server.name에 가비아에서 산 domain을 입력합니다.

nginx 컨테이너가 사라질 시 default.conf도 사라지기 때문에 설정할 domain.conf를 volume으로 설정해주어야합니다.

4.nginx log 디렉토리 생성

mkdir log

 

nginx 컨테이너가 사라질 시 로그도 사라지기 때문에 volume을 이용하여 log를 저장할 디렉토리를 생성해줍니다.

5.docker 실행

docker run -d -p 80:80 -v/home/yougeun/project/nginx/default.conf:/etc/nginx/conf.d/default.conf -v/home/yougeun/project/nginx/nginx.conf:/etc/nginx/nginx.conf -v/home/yougeun/project/nginx/log:/var/log/nginx --name nginx-server nginx

 

domain.conf 파일은 /etc/nginx/conf.d/default.conf

nginx.conf 파일은 /etc/nginx/nginx/conf.d

log 디렉토리는 /var/log/nginx로 volume을 설정해주면됩니다.

 

6. 확인

[domain]:8888

 

docker exec -it nginx-server bash

 

위 명령어로 nginx 서버로 접속 해 volume으로 설정한 파일들이 정상적으로 적용되었는지 확인합니다.

 

728x90