개념
Tomcat = 자바 서블릿 컨테이너, 웹 애플리케이션 서버
Jetty = 자바 서블릿 컨테이너, 웹 애플리케이션 서버
Nginx = 웹 서버
Tomcat
: 자바 애플리케이션을 위한 대표적은 오픈소스 WAS (Web Application Server)
: 오픈소스임
1. 서버 사용 방법
: SpringBoot에 내장되어 별도의 설치 필요 없음
: 단, apache2.4나 Nginx 등 프록시서버 실행중이어야 함
: 아래 dependencies가 있다면 자동 실행
implementation 'org.springframework.boot:spring-boot-starter-web'
2. 사용 해제 방법
implementation ('org.springframework.boot:spring-boot-starter-web') {
exclude module: 'spring-boot-starter-tomcat'
}
Jetty
: 이클립스 재단의 HTTP 서버이자 자바 서블릿 컨테이너
: 타 웹 애플리케이션 대비 적은 메모리 사용 (가볍고 빠름)
: 경량 웹 애플리케이션이라 소형장비, 소규모 프로그램에 적합
: 오픈소스임
1. 서버 사용 방법
: tomcat을 사용 중지하고 아래의 dependencies 추가
implementation ('org.springframework.boot:spring-boot-starter-web') {
exclude module: 'spring-boot-starter-tomcat'
}
implementation ('org.springframework.boot:spring-boot-starter-jetty')
Nginx
: 웹 서버 소프트웨어
: 클라이언트에게 정적 리소스를 빠르게 응답하기 위해 프록시 서버로 사용
: 트래픽이 많은 웹 사이트의 확장성을 위해 개발된 고성능 웹 서버
: 비동기 이벤트 기반으로 적은 자원으로 높은 성능과 높은 동시성을 위해 개발
: 다수의 클라이언트 연결을 효율적으로 처리
: 리버스 프록시 서버로 사용가능
: 클라이언트와 서버 사이에 배치하여 무중단 배포 가능
: 오픈소스
: SpringBoot - Nginx 연동해서 사용
1. Nginx설치
1) MacOS의 경우
$ brew update
$ brew install nginx
2) Windows의 경우
: Stable version 다운로드 https://nginx.org/en/download.html 후 압축해제
2. Nginx실행
1) MacOS의 경우
$ brew services start nginx
2) Windows의 경우
: 압축해제한 폴더에서 Nginx.exe 실행 => 추가정보 => 실행
3. Nginx 실행 확인
1) MacOS의 경우 : http://localhost:8080
2) Windows의 경우 : http://localhost:80 (80번 포트 생략 가능)
4. 설정파일 수정
1) MacOS의 경우
$ nginx -t
$ nano /opt/homebrew/etc/nginx/nginx.conf // nginx.conf가 있는 경로에서 nginx.conf를 나노로 편집
2) Windows의 경우
: nginx-1.20.2 (압축해제한 폴더) => conf => nginx.conf
: 터미널에서는 다음과 같이 진행
$ cd ~
$ nano Desktop/nginx-1.22.0/conf/nginx.conf
3) 파일 수정
...
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost: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;
}
...
5. Nginx 재시작
1) MacOS
$ brew services restart nginx
2) Windows
: cmd => nginx압축해제 폴더로 이동 => 명령어 입력
$ cd Desktop/nginx-1.22.0
$ nginx -s reload
6. Nginx 종료
1) MacOS
$ brew services stop nginx
2) Windows cmd
$ nginx -s stop
Load Balancer
: 클라이언트 - Nginx - SpringBoot로 이뤄진 프록시 서버 구조에서, Nginx가 로드 밸런서 역할 (로컬환경 기준)
1. 기본 사용 방법
: 프로젝트 빌드 => 빌드 파일 실행 => 다른 포트번호로 빌드파일 실행
: 빌드는 인텔리제이의 gradle에서 boot jar나 build로도 가능
./gradlew build
java -jar sample-0.0.1-SNAPSHOT.jar
java -Dserver.port=8081 -jar sample-0.0.1-SNAPSHOT.jar
2. NGINX 설정 파일 수정 (nginx.conf)
: 위에서 바꾼 이전 파일 변경 내용은 지우고 진행
: backend라는 서버 그룹을 만든 뒤 그룹 자체로 전달하는 구조 (서버 그룹 이름은 다른 이름으로 변경가능)
: 80번 포트=nginx의 포트. 8080과 8081은 localhost 포트
: 브라우저에서 localhost 접속 후 새로고침하면 두 포트가 번갈아 나옴
$ cd ~
$ nano Desktop/nginx-1.22.0/conf/nginx.conf
http {
upstream backend {
server localhost:8080;
server localhost:8081;
}
location / {
proxy_pass http://backend;
}
}
*참고 공식 문서 : https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/
'Codestates [Back-end] > 데일리 로그 [TIL]' 카테고리의 다른 글
22.10.12 Spring WebFlux - 기본 코드 구조 / MVC vs WebFlux (0) | 2022.10.12 |
---|---|
22.10.12 Spring WebFlux - 개념 / 인터페이스 코드 (0) | 2022.10.12 |
22.10.11 Cloud 운영전략 - 프록시 서버 / 로드 밸런서 / 오토 스케일링 (0) | 2022.10.11 |
22.10.06 Cloud - 배포 자동화 [Step 3. 서버 환경 변수 설정] (0) | 2022.10.07 |
22.10.07 Cloud - 배포 자동화 [Step 4. GitHub Actions] [진행중] (0) | 2022.10.06 |