본문 바로가기
Infra/Docker_K8S

nginx tomcat 연동하기

by 골든크랩 2022. 4. 1.
728x90
반응형

작성중...아래 글 읽어볼것.

https://st-soul.tistory.com/78

 

 

1. 이미지 끌어오기...

docker run -dit --name tomcat -p 8080:8080 -e TZ=Asia/Seoul tomcat

 

2. 컨테이너 환경 확인
docker exec -it tomcat bash

 

 

■ 중요 : docker-compose.yml 파일을 만들고 실행하니 구동이 안되는 현상이 발생했다.

volumes 설정을 잡을때는 반드시 동일 폴더를 local 에 만들어 놓고, 관련 파일도 만들어놔야 한다.

그래서...

docker-compose.yml 파일의 volumes 에 아래와 같이 설정한다면...

    volumes:
      - ./tomcat/conf:/usr/local/tomcat/conf

컨테이너에서 아래명령으로 로컬로 복사해 놓아야한다.  디렉토리명까지만 입력하면...디렉토리가 통채로 복사된다.

docker cp tomcat:/usr/local/tomcat/conf conf

 

3. 응용app 디렉토리 만들고 마운트하기

 volumes:
      - ./tomcat/conf:/usr/local/tomcat/conf     

      - ./tomcat/webapps:/usr/local/tomcat/webapps

 

4. 샘플코드 만들기

webapps 아래 test 디렉토리를 만들고...index.jsp 파일로 저장한다.

<!DOCTYPE html>

<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Skyler's Login Page</title>
</head>
<body>
  <h1>First JSP</h1>
  <br>
  <%= new java.util.Date() %>
</body>
</html>

 

5. 브라우저에서 테스트 하기

6. 여기까지가 tomcat 설치와 관련된 부분이고, tomcat과 nginx 연동은 nginx의 default.conf 파일을 이용해서 함.

내가 삽질한 부분은 아래 붉은색  proxy_pass 인데 처음에 http://localhost:8080 으로 하면서 nginx 에서 tomcat 으로 접속을 하지 못했다.

 

2022/04/03 04:39:23 [error] 30#30: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "localhost"

 

늘 그렇지만....해결책을 찾지 못해 고민한 시간에 비해 원인은 간단했다...바로 tomcat과 nginx 가 별도의 컨테이너로 있다보니(즉, 이들은 서로 다른 시스템인것이다) 당연히 localhost 를 사용하면 서로를 찾지 못한다는 것이였다.ㅠㅠ

 

그래서, 실제 절대주소를 맵핑해서 주니 바로 문제가 해결됨.

 

========================================================================

server {
        listen 80;
        #root /var/www/html;
        #index index.html index.htm index.php;

        server_name localhost;

     error_log  /var/log/nginx/error.log warn;
access_log /var/log/nginx/access.log;



# php 로 끝나는 경우의 처리
        #location ~ \.php$ {
# index index.html index.htm index.php;
        #        try_files $uri =404;
        #        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #        fastcgi_pass php:9000;
        #        fastcgi_index index.php;
        #        include fastcgi_params;
        #        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #        fastcgi_param PATH_INFO $fastcgi_path_info;
        #}

location / {
        # nginx와 tomcat 이 별도의 container 인 경우는 localhost나 127.0.0.1 를 사용하면 안됨
proxy_pass http://172.18.0.3:8080;
charset utf-8;
index index.jsp;
proxy_redirect                   off;
proxy_set_header Host            $host;
proxy_set_header X-Real-IP       $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;

# http-method 허용범위 : Java기반 GET/HEAD/POST/PUT
limit_except GET HEAD POST PUT {
deny all;
}
}

location ~ \.(css|js|jpg|jpeg|gif|png|html)$ {
proxy_pass http://172.18.0.3:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}

location ~ \.jsp$ {
proxy_pass http://172.18.0.3:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}

#error_page 404 /404.html;
        #    location = /40x.html {
        #}
#
        #error_page 500 502 503 504 /50x.html;
        #    location = /50x.html {
        #}

}

========================================================================

 

 

 

 

 

 

 

 

 

 

728x90
반응형

댓글