Skip to content
GwiyeomGo Tech Blog
About GwiyeomGo

nginx 도메인 등록1

INFRA, 20255 min read

배경

route53에 도메인을 등록했다 1.호스팅 영역> 기존 gwiyoem.org 도메인선택 2.레코드 생성 새 서브도메인 (예: test-service.gwiyeom.org) → EC2 퍼블릭 IP로 연결

이후 postman 으로 호출했지만 404 에러가 발생했다

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0 (Ubuntu)</center>
</body>
</html>

EC2에서 Nginx 실행 중 인데 EC2 안의 Nginx가 새 도메인을 처리하지 못해서 발생했다

Postman or 브라우저
Route53 (도메인 네임서버)
도메인이 연결된 IP (예: EC2 퍼블릭 IP)
EC2 인스턴스 내 Nginx
Nginx가 백엔드 앱 (예: Golang, Node 등)으로 요청 전달

Nginx가 뭐고 왜 도메인을 지정해야하지?

  1. Nginx = 웹 서버이자 리버스 프록시 서버 = 안내 데스크 요청을 받아서 적절한 서버(앱)로 전달해주는 배달원

https://test-service.gwiy eom.org 요청-> 서버(EC2) 도착하면 Nginx 가 "이 요청은 NestJS 서버로 보내야겠군~" 하면서 localhost:3000 로 전달 = 리버스 프록시

  1. 서버(EC2)에 도착했을 때 Nginx가 어떻게 처리할지 알려주기 위해! 어떤 요청을 어떤 앱에 연결해줘야 하는지 알기 위해 서브 도메인 등록 필요

Nginx에 서브도메인 등록하는 방법

  1. 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
  1. 파일열기 sudo nano /etc/nginx/nginx.conf 파일이 없는 상황 ㅠ
  2. default만 사용중으로 확인됨! ls /etc/nginx/sites-available

default default.bak

default 파일 열기 sudo nano /etc/nginx/sites-available/default

  1. 편집 Nginx 서버 설정에 test-service.gwiyeom.org라는 서브도메인을 추가 Certbot으로 HTTPS 인증서를 설정 을 위한 값 추가
# 서브도메인을 추가
server {
server_name test-service.gwiyeom.org;
location / {
proxy_pass http://127.0.0.1:4000;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
#
server {
if ($host = test-service.gwiyeom.org) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name test-service.gwiyeom.org;
listen 80;
return 404; # managed by Certbot
}
  • Let's Encrypt(Certbot)를 이용해 HTTPS 인증서를 발급받고,HTTP 요청은 HTTPS로 리디렉션 처리하는 것.

Ctrl + O → 저장 Enter → 파일 이름 확인하고 저장 Ctrl + X → 종료

  1. nginx에 적용
  • Nginx 설정 파일에 문법 오류가 있는지 검사하는 명령어 sudo nginx -t
  • Nginx가 설정 파일을 다시 읽고 적용하도록 "리로드"시키는 명령어 sudo systemctl reload nginx "nginx야, 새로 바뀐 설정 파일 좀 반영해서 다시 작동해줘~~~"

HTTPS 인증서를 Certbot으로 추가 방법

Nginx 서버 설정에 새로 추가된 서버 도메인 내용에 다음 명령어 실행시 Certbot이 자동으로 채워준다 = HTTPS(SSL) 적용하고 싶다면 이 도메인에 대해 실제 SSL 인증서를 새로 발급

  1. SSL 인증서를 새로 발급 sudo certbot --nginx -d test-service.gwiyeom.org -> Congratulations! You have successfully enabled HTTPS on

TIP) 이름 문제 발생?

도메인 이름 규칙 (중요)
도메인 이름은 RFC 표준에 따라 다음 규칙을 따라야 한다:
✅ 허용: 영문 소문자 (a-z), 숫자 (0-9), 하이픈 (-)
❌ 금지: 언더스코어(_), 공백, 특수문자
test_service.gwiyeom.org ← ❌ 잘못된 도메인 이름

(질문)docker 사용시 nginx가 꼭 필요한 건가?

Docker로 단순 개발, 테스트 중 -> ❌ 필요 없음 (브라우저가 직접 포트로 접근)
여러 서비스를 운영하거나 도메인 연결할 때 -> ✅ Nginx 필요!
HTTPS(SSL) 인증서를 사용하고 싶을 때 -> ✅ Certbot + Nginx 필요
React/Next 같은 프론트도 함께 서빙할 때 -> ✅ Nginx에서 정적 파일 서빙 좋음
© 2025 by GwiyeomGo Tech Blog. All rights reserved.