아래의 명령어로 nginx 를 설치한다.
sudo yum instal nginx
설치후 버전을 확인해 보자
nginx -v
nginx version: nginx/1.22.1
이제 Nginx가 설치되었으니 Spring Boot 로 띄운 서비스와 연동을 해보자.
먼저 nginx.conf 파일을 찾아보자.
sudo find / -name nginx.conf
필자는 아래와 같이 파일의 위치가 나와다.
/etc/nginx/nginx.conf
그럼 이제 nginx.conf 파일을 수정하도록 하자.
sudo vi /etc/nginx/nginx.conf
아래와 같은 부분을 발견할 수 있다.
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
여기에 아래와 같이 설정을 추가해 주자. 들어오는 요청에 대해서 localhost:8080 으로 보내겠다는 의미이다.
location / {
proxy_pass http://localhost:8080;
}
이제 nginx 설정에 문제가 없는지 문법을 검사해 보자.
sudo nginx -t
아래와 같이 떴다면 문법에는 이상이 없다는 의미이다.
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
이제 Nginx 서비스를 시작해보자.
sudo service nginx start
이제 nginx를 통해 80 포트로 이전 포스트에서 올려둔 스프링 서비스를 호출 가능하다.
웹브라우저에서 Nginx가 설치된 EC2 인스턴스의 퍼블릭IP 또는 퍼블릭DNS로 이전 포스트에서 올렸던 스프링부트 웹서비가 호출 가능한 것을 확인 할 수 있다.
nginx 명령어
service nginx status
service nginx start
service nginx stop
service nginx restart
service nginx reload