티스토리 뷰

반응형

NGINX란?

'차세대 웹서버'라고 불리는 nginx는 대표적인 웹서버인 Apache의 문제점을 해결하면서 만들어진 웹서버로 Apache의 독주를 막는 위협적인 존재입니다.

NGINX 실행

기본 nginx 실행

# Docker를 이용한 기본 nginx 실행
docker container run -d --name nginx -p 8080:80 nginx

# nginx 설정 파일 디렉토리 찾기
find / -name nginx.conf

# 설정 파일 내용 확인
cat /etc/nginx/nginx.conf
cat /etc/nginx/conf.d/default.conf

수정된 nginx 실행

nginx의 server 설정을 위해서는 nginx 컨테이너 내의 /etc/nginx/conf.d/default.conf 파일을 수정해야 합니다.

그런데 nginx 컨테이너 내에서 vi나 vim 편집기의 사용이 불가능하니, 로컬 환경에서 default.conf을 작성한 뒤 해당 파일을 볼륨으로 설정해 적용하겠습니다.

# default.conf 파일 작성
cd /opt/gopath/src/github.com/hyperledger/
mkdir nginx
vi default.conf
# Load Balancing
upstream target-server {
  least_conn;
  server <서버의 IP주소. ex: 172.192.45.3>:4000;
  server <서버의 IP주소. ex: 172.192.45.3>:4001;
  server <서버의 IP주소. ex: 172.192.45.3>:4002;
}

server {
    listen       80;
    server_name  localhost;
    
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
        proxy_next_upstream error http_503;
        # proxy_pass http://<서버의 IP주소. ex: 172.192.45.3>:4000;
        proxy_pass http://target-server;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

upstream 설정을 통해 Load Balancing을 설정합니다.

least_conn 옵션은 현재 연결이 가장 적은 서버로 요청을 전달하는 역할을 합니다.

server에 로드밸런싱 할 서버의 주소를 입력합니다. 여기에서는 3 개의 Rest API를 설정합니다.

# 수정된 nginx 실행
docker container run -d --name nginx -p 8080:80 -v /opt/gopath/src/github.com/hyperledger/nginx/default.conf:/etc/nginx/conf.d/default.conf nginx

참고 사이트

nginx 개념

nginx 설정

반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함