AWS EC2와 Nginx를 이용해 서버를 배포하는 과정에서 지속적으로 발생하는 오류가 존재했다. 해결 과정을 블로그에 상세히 기록해보려 한다. 배포하는 과정은 아래 블로그에서 확인할 수 있다.
https://jihyun-devstory.tistory.com/47
[Infra] AWS EC2를 이용한 WAS 서버 배포
1. EC2 인스턴스 생성(1) AWS Region 서울로 설정IAM 사용자로 회원가입 후 로그인하면 아래와 같이 지역을 설정할 수 있다. 작성자는 Seoul로 설정했다. (2) 인스턴스 시작 버튼 누르기 EC2 서비스에 들
jihyun-devstory.tistory.com
1. SSH Operation timed out 에러
$ jihyun@MacBook-Pro ~ % ssh jihyun
ssh: connect to host 3.39.30.233 port 22: Operation timed out
서버 배포를 완료한 이후, SSH 접속을 다시 시도하는 과정에서 지속적으로 위와 같은 오류가 발생하였다. 심지어 SSH 접속이 될 때가 있고, 안될 때가 있었다. 아래 명령어를 입력해도 결과는 같았다.
$ ssh -i "aws-key.pem" ubuntu@ec2-3-39-30-233.ap-northeast-2.compute.amazonaws.com
ssh: connect to host 3.39.30.233 port 22: Operation timed out
구글링을 통해 여러 해결 방법을 찾아보았다. 다행히도 시도해볼만한 방안들이 존재했다.
(1) 다른 네트워크 환경에서 접속 시도
- 현재 접속해있는 네트워크 환경이 문제일 가능성이 있어 다른 Wi-Fi, 휴대폰 핫스팟 등을 사용해 확인
(2) Security Group 설정 확인
- AWS EC2 인스턴스의 Security Group 설정을 확인하고, IP 주소와 포트 22가 올바르게 설정되어 있는지 확인

(3) 로컬 방화벽 설정 확인(MacOS)
- MacOS의 시스템 환경설정에서 원격 로그인(SSH)이 활성화되어 있는지 확인
- 설정 → 일반 → 공유 → 원격 로그인 허용 → 모든 사용자로 바꿈


위 3가지 방안을 모두 시도해봤지만 에러는 해결되지 않았다. 결론적으로 아래 방안을 통해 에러를 해결할 수 있었다.
(4) 사용자 데이터 스크립트 실행
먼저 인스턴스를 중지하고 Actions → Instance Settings → Edit User data에 들어간다.

그 다음 아래 스크립트를 붙여넣기 하고, 인스턴스를 재시작 한다.
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
ufw disable
iptables -L
iptables -F
--//
스크립트의 내용은 다음과 같다.
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
- 클라우드 초기화 모듈(cloud-config)를 통해 인스턴스 시작 시 User Data 스크립트 항상 실행
ufw disable
- 우분투 방화벽(UFW) 비활성화
iptables -L
iptables -F
- 모든 방화벽 규칙 나열 및 초기화 해 네트워크 트래픽 제한 제거
참고 자료
https://stackoverflow.com/questions/41929267/locked-myself-out-of-ssh-with-ufw-in-ec2-aws
Locked myself out of SSH with UFW in EC2 AWS
I have an EC2 Instance with Ubuntu. I used sudo ufw enable and after only allow the mongodb port sudo ufw allow 27017 When the ssh connection broke, I can´t reconnect
stackoverflow.com
https://velog.io/@silver_cherry/AWS-EC2-SSH-%EC%A0%91%EC%86%8D-%EB%AC%B8%EC%A0%9C-%ED%95%B4%EA%B2%B0
AWS EC2 SSH 접속 문제 해결
인스턴스의 사용자 데이터를 수정, 방화벽(UFW) 설정을 재구성하여 SSH 접속 문제를 해결완료!
velog.io
https://suucong.tistory.com/61#google_vignette
[AWS] port 22: Operation timed out (ssh로 ec2 접속시 timeout 오류 해결)
ufw enable 후 접속이 안되는 오류가 생겼다. 왜냐면 내가 ufw 허용 포트를 지정해줄 때 80이랑 443만 허용해줘가지고,,, 이후에 다시 접속하려니까 저런 오류가 발생했다. 그래서 자료를 찾아보고 찾
suucong.tistory.com
2. 배포된 서버가 제대로 동작하지 않음

서버가 정상적으로 배포되었다면 아래와 같은 화면이 나와야하는데, 나의 화면은 위와 같았다.

이 부분은 생각보다 간단하게 해결할 수 있었다. Nginx 설정 파일에서 단 한 줄을 삭제하니 해결되었다.
server {
listen 80;
server_name aws-study.kro.kr;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# try_files $uri $uri/ /index.html;
}
}
try_files $uri $uri/ /index.html;
Nginx는 정적 파일을 서빙하고, WAS는 동적 요청을 처리한다. try_files $uri $uri/ /index.html;는 요청된 URL이 없을 경우 Nginx가 /index.html을 반환함을 의미한다. 이로 인해 WAS로 전달되어야 할 요청이 Nginx에서 차단되고, 결과적으로 백엔드 API 호출이 index.html로 응답된다. 따라서 해당 문장을 삭제함으로써, 요청이 없을 경우 index.html로 강제로 리다이렉트되지 않고 WAS로 요청이 정상적으로 전달되어 문제를 해결할 수 있었던 것이다.
'Backend > Infra' 카테고리의 다른 글
[Infra] 무중단 배포 적용하기(feat. Blud-Green Deployment) (1) | 2024.12.28 |
---|---|
[Infra] 배포한 서버에 CI/CD 적용하기(feat. Jenkins) (0) | 2024.12.28 |
[Infra] AWS EC2를 이용한 WAS 서버 배포 (0) | 2024.12.23 |
[Infra] 인프라 키워드 정리 Part 1(feat. Cloud Service, AWS, EC2) (4) | 2024.12.18 |
[Infra] 인프라 키워드 정리 Part 2(feat. SSH, Domain, WAS, Nginx) (0) | 2024.12.18 |