ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Infra] nginx reverse proxy 구성
    Infra 2020. 7. 6. 23:41

    지난 번에 forward proxy(포워드 프록시) reverse proxy(리버스 프록시)에 대해서 알아보았습니다.

    2020/07/05 - [Infra] - Forward proxy / Reverse proxy

     

    Forward proxy / Reverse proxy

    클라이언트가 특정 사이트(test.com)에 접근하려고 할 때 클라이언트 pc가 직접 연결하는 게 아니라 서버가 요청을 받아 특정 사이트에 연결하여 연결된 결과를 다시 클라이언트에 전달(forward) 해��

    developia.tistory.com

     

    이번엔 배운 내용을 적용해보고자.. nginx를 통해서 리버스 프록시를 구현해보았습니다.

     

    서버의 구성은 nginx-proxy : 리버스 프록시 서버, nginx-web, nginx-web2 : 하위 웹서버 입니다.

     

    nginx는 기본적으로 도커를 사용해서 구성하였습니다.

     

    구성도를 표현하자면 그림에 빨간색이라고 볼 수 있습니다.

    출처 : https://akal.co.kr/?p=1173

     

    docker로 할 수도 있지만 한 줄이 너무 길어지기 때문에 docker-compose를 통해서 작성하였습니다.

     

    작성한 내용은 다음과 같습니다.

    version: '3.8'
    services:
      nginx-proxy:
        container_name: nginx-proxy
        image: nginx:latest
        ports:
          - "80:80"
        volumes:
          - type: bind
            source: ./nginx-proxy/nginx.conf
            target: /etc/nginx/nginx.conf
        restart: always
      nginx-web:
        container_name: nginx-web
        image: nginx:latest
        expose:
          - "8080"
        volumes:
          - ./nginx-web/default.conf:/etc/nginx/conf.d/default.conf
        restart: always
      nginx-web2:
        container_name: nginx-web2
        image: nginx:latest
        expose:
          - "8081"
        volumes:
          - ./nginx-web2/default.conf:/etc/nginx/conf.d/default.conf
        restart: always
    

    docker-compose.yml 파일에 대한 내용이고 docker-compose up -d 명령어를 통해 도커를 실행합니다.

     

    실행하기 전에 내용을 간단히 보면 volume에 type을 선언한 부분이 있고 그냥 되어있는 부분도 있는 데, 같은 내용입니다.

     

    nginx-proxy > volume의 source는 호스트의 파일 위치이고, target은 컨테이터의 위치입니다.

     

    nginx-web,web2 > volume은 호스트 파일위치 : 컨테이터 파일 위치라고 보시면 됩니다.

     

    이렇게 구성한 이유는 version이 지금은 3.8인데 처음에 3으로 하다가 volume에 파일은 매핑이 안되어서 고생했는데 알고보니 버전 때문이었다는.. 사실상 nginx-web처럼 구현하시면 편할 것 같습니다.

     

    아무튼 이런식으로 해두면 컨테이터가 사라져도 볼륨은 공유되기 때문에 이런식으로 구성한 건데 컨테이너가 생기기전에 미리 nginx.conf 내용을 작성하는게 좋습니다.


    다음은 nginx-proxy의 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;
        upstream docker-nginx {
            server nginx-web:8080;
            server nginx-web2:8081;
        }
        server {
            listen 80;
            server_name localhost;
            location / {
                proxy_pass         http://docker-nginx;
                proxy_redirect     off;
                proxy_set_header   Host $host;
                proxy_set_header   X-Real-IP $remote_addr;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            }
        }
        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;
        keepalive_timeout  65;
        include /etc/nginx/conf.d/*.conf;
    }
    

    기본적인 nginx.conf 중 upstream 부분과 server의 listen, server_name, location 부분이 추가 되었습니다.

    upstream docker-nginx 안의 server nginx-web 이라는 부분이 있는 데 이 부분은 docker 컨테이너 이름으로 설정해야 됩니다.

     

    그리고 server 부분의 listen은 기다리고있는 80포트를 의미하고, server_name은 localhost로 지정하였고,

    location부분은 경로를 /로 들어올 경우 proxy_pass의 적힌 부분으로 프록시합니다. docker-nginx 라는 upstream을 지정했으므로 nginx-web, web2로 프록시를 진행합니다.

     

    다음으로 진행할 부분은 nginx-web, nginx-web2부분의 default.conf 설정입니다.

    server {
        listen      8080;
        server_name  localhost;
    
        #charset koi8-r;
        #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;
        #}
    }
    

    기본적인 nginx 부분에서 첫줄의 listen 부분을 8080로 바꾸었습니다. 추후에 WAS를 8080포트로 설정하려고 이렇게 바꾸었습니다. (이렇게 해서 되는지는 좀 해봐야 알 것 같습니다.)


    docker ps -a 명령어로 전체 컨테이너를 출력한 모습입니다.

     

    이런식으로 구성시 리버스 프록시 서버가 외부 80포트로부터 요청을 받으면 nginx-web, nginx-web2를 번갈아가며

     

    실행합니다.

     

    위에 사진이 nginx-proxy : 리버스 프록시 서버이고 왼쪽 아래가 nginx-web, 오른쪽 아래가 nginx-web2입니다.

     

    리버스 프록시 서버는 요청을 6번 받았지만 각각의 웹서버는 3번을 나눠서 받은 걸 확인할 수 있습니다.

     

     

    참고 : https://medium.com/sjk5766/docker-compose%EB%A1%9C-localhost-nginx-%EB%A6%AC%EB%B2%84%EC%8A%A4-%ED%94%84%EB%A1%9D%EC%8B%9C-%EA%B5%AC%EC%84%B1-8214d41a94fc

    https://akal.co.kr/?p=1173

     

    'Infra' 카테고리의 다른 글

    [Infra] Forward proxy / Reverse proxy  (0) 2020.07.05
Designed by Tistory.