2022. 5. 16. 13:28ㆍInfra
1. Docker pull 을 통하여 nginx, tomcat 9 이미지를 다운로드 한다.
- docker pull nginx
- docker pull tomcat:9
2. Nginx 를 실행한다.
- docker run -d -p 80:80 -p 443:443 --restart always --name nginx nginx:1.18.0
- d 속성 : 백그라운드로 컨테이너를 나가도 계속해서 실행하게한다.
- p 속성 : 해당포트로 접속시 컨테이너의 해당포트로 맵핑시켜준다.
3. Tomcat 을 실행한다.
- docker run -d -i -p 8080:8080 -v /docker/tomcat/upload:/var/upload --name tomcat tomcat:9
4. /etc/nginx/nginx.conf 파일을 수정한다.
- conf파일을 수정하기 위해서 apt-get update
- apt-get install vim 을하여 수정할수 있는 vim 에디터를 다운로드한다.
- ssl 설정을 하기위해 인증서 파일을 nginx로 복사한다
- docker cp ./인증서파일명 nginx:/etc/nginx/key/인증서파일명
- docker cp ./인증서키 nginx:/etc/nginx/key/인증서키
- include /etc/nginx/conf.d/*.conf; 부분을 꼭 제거해주세요. (제거하지 않으면 기본 설정을 타게됩니다.)
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;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
root /usr/share/nginx/html; //정적파일 위치
location /api {
proxy_pass http://tomcat; //api로 맵핑될경우 톰캣으로 연동
}
location /img {
root /usr/share/nginx/html; //img로 맵핑될경우 정적파일안 img폴더 연결
}
}
server {
listen 443;
root /usr/share/nginx/html;
ssl on; //ssl 연동
ssl_certificate /etc/nginx/key/인증서;
ssl_certificate_key /etc/nginx/key/인증서키;
location /api {
proxy_pass http://tomcat;
}
location /img {
root /usr/share/nginx/html;
}
}
upstream tomcat {
server 172.17.0.2:8080; //docker inspect tomcat을 하여 tomcat 컨테이너의 ip 입력
}
}
5. 정적파일(html,css,js,img)들을 nginx로 복사한다.
- docker cp ./index.html nginx:/usr/share/nginx/html/
6. war파일을 톰캣으로 복사한다.
- docker cp ./ROOT.war tomcat:/usr/local/tomcat/webapps/ROOT.war
- 여기서 tomcat:9 버전 이상은 스프링부트가 webapps 안에서 바로 실행이 안되는 문제가 있어 톰캣9버전을 사용했다.
7. 접속 확인
- 기본 URL로 접속시 web-server(정적)에 접근하는 것을 확인할수 있었으며,
- /api URL로 접속시 WAS(스프링부트)에 접근하여 데이터를 들고오는 것을 확인할수 있었다.
'Infra' 카테고리의 다른 글
Docker Redis Spring-Boot 연결하기 (0) | 2022.04.21 |
---|