본문 바로가기
IT일반

AWS EC2 Linux 2에 nginx 프락시 서버 설정하기 SSL 까지

by xavi2019 2021. 12. 8.

서버 설정

# Git 설치
sudo yum install git -y

# NginX 설치
sudo amazon-linux-extras install nginx1
# Nginx 시작
sudo service nginx start


# 참고 nginx 홈
# /usr/share/nginx/

nginx 도메인 설정

초기 설정=

   
# nginx 설정파일 편집
sudo vi /etc/nginx/nginx.conf

--> 아래내용 삽입 < --
    server {
        listen       80;
        listen       [::]:80;
        server_name  {도메인};
        root         /usr/share/nginx/html;
    
        # hide server info
        server_tokens off;
    
        # Protect XSS Attack
        add_header X-XSS-Protection "1; mode=block";
    
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
    
        location / {
                try_files $uri $uri/ @nodejs;
        }
    
        location @nodejs {
                add_header author "{TeraWebServer}";
                proxy_redirect off;
                proxy_http_version 1.1;
                proxy_pass http://127.0.0.1:5000;
                proxy_set_header Host $host ;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    
    
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
        
# 웹서버 재시작
sudo service nginx restart

Let's Encrypt 무료 SSL 인증서 설정

도메인 설정이 먼저 되어야 함. 

cd ~
sudo wget -r --no-parent -A 'epel-release-*.rpm' http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/
sudo rpm -Uvh dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-*.rpm
sudo yum-config-manager --enable epel*
sudo yum repolist
sudo yum install -y certbot
sudo yum install certbot-nginx
sudo service nginx stop
sudo certbot --nginx

이후 필요한 정보 입력

크론탭에 추가

sudo vi /etc/crontab
#  아래 라인 추가
  13      1,13    *       *       *       root    certbot renew --no-self-upgrade
  
sudo systemctl restart crond

(선택사항) 스왑파일 설정 (디스크를 메모리로 사용)

sudo yum update -y
sudo dd if=/dev/zero of=/swapfile bs=128M count=32
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon -s
sudo vi /etc/fstab
# 에디터가 뜨면 아래 추가 
/swapfile swap swap defaults 0 0

홈페이지 파일 수정

sudo vi /usr/share/nginx/html/index.html


<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
        API Proxy Server
</body>
</html>

댓글