우분투에서 Nginx로 Reverse Proxy 설정하기

유형찬·2022년 12월 9일
2

우분투와 Nginx

1. Nginx 설치하기

sudo apt-get update
sudo apt-get install nginx

2. Nginx 설정하기

sudo vi /etc/nginx/sites-available/default
server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://localhost:8080/;
    }
}

이렇게만 해줘도 localhost:80 으로 접근하면 localhost:8080 으로 리다이렉트 되는 것을 확인할 수 있습니다.

그래서 여러 서버를 띄워서 로드밸런싱을 하고 싶을 수도 있습니다.

3. Nginx 로드밸런싱 설정하기

sudo vi /etc/nginx/sites-available/default

upstream backend {
    server localhost:8080;
    server localhost:8081;
    server localhost:8082;
}

server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://backend/;
    }
}

간단하게 말하자면 localhost로 들어오는 요청을 localhost:8080, localhost:8081, localhost:8082 로 분산시키는 것입니다.

4. Nginx 설정 적용하기

sudo nginx -s reload

또는

sudo service nginx restart

5. Nginx 에 도메인 연결하기

sudo vi /etc/nginx/sites-available/default
server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:8080/;
    }
}

server_name 이 외부에서 접근할 도메인입니다.

DNS 설정을 했다면 현재 우분투 서버의 IP 주소로 접근 할 수 있겠죠

거기서 오는 요청을 Nginx 에서 localhost:8080 으로 리다이렉트 시켜주는 것입니다.

6. Nginx 에 SSL With Let's Encrypt 적용하기

1. Certbot 설치하기

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx

2. Certbot 실행하기

sudo certbot
sudo certbot --nginx

보통 이 아래를 많이 쓴다!!

sudo certbot --nginx -d accounts.example.com 
example.com 은 도메인이다. 자신의 도메인을 넣어 주면 된다. 

option command

sudo certbot renew --dry-run
certbot으로 생성한 SSL 인증서 갱신 할 수 있는지 여부 확인 하기 갱신이 되는 건 아님 
sudo certbot renew
certbot 으로 생성한 SSL 인증서 갱신 
sudo certbot delete
certbot으로 생성한 SSL 인증서 삭제 

7. Nginx conf 확인 후 Https로 리다이렉트 하기

sudo vi /etc/nginx/sites-available/default
upstream tomcatAuth {
        ip_hash;
        server 127.0.0.1:8100; # account.example.com 으로 들어온 요청은 127.0.0.1:8100 으로 리다이렉트
    
}

server {
        # 
        server_name accounts.example.com;
        location / {
            proxy_pass http://tomcatAuth; # 위 upstream 에서 설정한 이름

            proxy_set_header    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;

            proxy_redirect  off;
            charset utf-8;


        }
        # 이 아래는 certbot 으로 자동으로 만들어지게 된다. 
listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/accounts.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/accounts.example.com/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




}
# 이 아래는 자동으로 만들어진다. certbot에 의해서 자동으로 만들어짐 https 로 리다이렉트 시키는 부분
server {
    if ($host = accounts.example.com) {

        return 301 https://$host$request_uri;
    } # managed by Certbot



        server_name accounts.gdsc-dju.com;
    listen 80;
    return 404; # managed by Certbot


}

OPTION 8. 여러 서버 켜서 여러 도메인으로 연결 하기

sudo vi /etc/nginx/sites-available/example.conf
upstream example {
        ip_hash;
        server
[...]

default 는 삭제 해야 한다.

sudo rm /etc/nginx/sites-enabled/default

그냥 conf 파일을 만들어서 위에 있는 내용을 자기 입맛대로 고쳐 쓰면 된다.

필요한 도메인 만큼 conf 파일을 만들어서 사용하면 된다.

9. Nginx 재시작 하기

sudo service nginx restart
profile
rocoli에요

2개의 댓글

comment-user-thumbnail
2022년 12월 9일

웅찬이 쵝오

답글 달기
comment-user-thumbnail
2022년 12월 9일

형 멋져요..
2빠

답글 달기