본문 바로가기
대외활동/DRACONIST-백엔드

09. https 도메인 연결하기 Draconist

by 피스타0204 2025. 3. 16.

 

이번 프로젝트는 react native로 애플리케이션을 만드는 것이었기 때문에 https 설정이 필요했습니다.

제일 처음 찾아봤던 방법은 aws route53을 이용하는 것이었습니다.

제가 진행했던 프로젝트는 규모가 작은 편이었어서 2만원대의 도메인 구매 비용이 발생한다는 점이 큰 단점으로 다가와 진행중 환불 요청을 하고, (aws의 도메인 구매는 환불불가 조항이 있기 때문에 결국 환불처리를 못받았습니다...) 가비아에서 제공하는 싼 가격의 .store 도메인을 사용하였습니다.

shop이나 store로 끝나는 도메인의 경우 싼 가격에 살 수 있다는 장점이 있고, 개발자라면 설정할 수 있는 부가기능이 없는 것 뿐이라 가비아에서 도메인을 구매하여 사용하는 것을 추천합니다.

 

자 이제 아래 링크글을 참고하여, aws route 53 호스팅 설정을 해봅시다. 여기서 설정하는 A유형이 nginx와 lets encrypt에서 사용됩니다.

https://thomass.tistory.com/85

 

aws route53 도메인 설정하기 (가비아)

aws route 53에 들어가서 호스팅역역-호스팅 영역 생성 클릭 타 사이트에서 구매한 도메인 이름을 입력 후 호스팅 영역 생성 만들면 유형 NS와 SOA를 가진 같은 레코드가 2개 생김 이값들을 도메인 구

thomass.tistory.com

 

호스팅 설정이 완료되었다면, ec2로 이동하여 nginx를 설치해줍니다.

Nginx 설치

nginx 설치 명령어

sudo apt update
sudo apt install nginx

#확인 명령어
systemctl status nginx

 

 

systemctl을 쳤을때 active-running이라고 뜬다면 제대로 설치된 것입니다.

 

Let’s Encrypt client 다운로드

sudo apt-get update
sudo apt-get install certbot
sudo apt-get install python-certbot-nginx

 

Ubuntu 18.04 이상 버전일 경우에는 아래와 같이 Python 3 버전으로 설치하여 사용할 수 있습니다.

sudo apt-get update
sudo apt-get install certbot
sudo apt-get install python3-certbot-nginx

 

Nginx SSL 설정

이제 SSL 인증서를 발급 받은 후, 웹 서버(Nginx)에서 브라우저에 전달할 SSL 인증서 정보를 설정해야 합니다.

Let’s Encrypt 클라이언트를 통해 인증서 발급 시 자동으로 SSL 설정이 가능하므로, Nginx 경로에 대한 설정만 해주면 됩니다.

 

이제 다음 명령어를 실행하여 nginx의 conf 파일을 열고, servername에 draconist.goodluckynews.store 처럼 호스팅할 때, A 레코드로 설정한 DNS 주소를 설정해야 합니다.

sudo vi /etc/nginx/sites-enabled/default
server {

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: <https://bugs.debian.org/773332>
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: <https://bugs.debian.org/765782>
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _; // 이 부분 변경하기

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        #
        #location ~ \\.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\\.ht {
        #       deny all;
        #}
}

 

 

모든 설정을 완료 후, 다음 명령어를 실행하여 SSL 인증서를 발급 받아 주세요.

sudo certbot --nginx -d {도메인 주소} // Public IP일 경우 .nip.io 추가하기

sudo certbot --nginx -d draconist.goodluckynews.store

해당 명령어를 실행하면 나오는 이메일 주소와 약관 동의 정보를 입력하라는 문구에 따라 입력해주세요.

 

이제 인증서 발급이 성공했다면 /etc/nginx/sites-enabled/default 가 변경됩니다.

vi /etc/nginx/sites-enabled/default

 

default conf 파일 스프링 리다이렉션

그 다음, 아래와 같이 HTTP -> HTTPS 리다이렉션, spring 서버 프록시 설정 등을 합니다.

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name draconist.goodluckynews.store; #우리가 설정한 도메인

# Spring 서버로의 프록시 설정
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                proxy_pass http://localhost:8080; # Spring 서버가 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;
        proxy_set_header X-Forwarded-Proto $scheme;
        }

        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
# 아래 내용이 자동으로 추가되었음
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/draconist.goodluckynews.store/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/draconist.goodluckynews.store/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {        # HTTP -> HTTPS 리다이렉션 추가
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

server { 
    if ($host = draconist.goodluckynews.store) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80 default_server;
        listen [::]:80 default_server;

        server_name draconist.goodluckynews.store;
    return 404; # managed by Certbot


}

 

안될시 체크사항

1) route53의 A 레코드에 설정된 값/트래픽 라우팅 대상이 ec2 퍼블릭주소(탄력적주소)와 같은지

 

2) 보안 그룹

- HTTP (80): 0.0.0.0/0, ::/0 (모든 IP에서 접근 가능)

-HTTPS (443): 0.0.0.0/0, ::/0 (모든 IP에서 접근 가능)