728x90

프록시 설정 이유

하나의 서비스를 이루는 서버가 여러 개 있을 때,

사용자는 각 서버가 어떤 역할을 하는지 파악하고 원하는 서버에 요청을 해야할까? 

ex)

http://localhost:5000/service1/~~

http://localhost:5050/service2/~~

 

중간에 프록시 서버를 두면 프록시서버가 사용자의 요청을 분기처리하여 서비스를 호출한다

사용자는 프록시서버만 알면 된다

ex)

http://localhost:80/service1/~~

http://localhost:80/service2/~~

 

 

윈도우 nginx 설치

 

설정파일 설정

※ 사전준비 : 기동되고 있는 서버의 url

 

1. {nginx설치경로}/conf/sites-enabled/customizing.conf 파일 생성

upstream spring-frontend {
    server localhost:8080;
}
upstream flask-backend {
    server localhost:5000;
}
upstream fastapi-backend {
    server localhost:5050;
}

server {
    listen 80;
    proxy_max_temp_file_size 0;
    client_max_body_size 256M;

    location / {
        proxy_pass http://spring-frontend;
        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 REMOTE-HOST $remote_addr;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }
    location /service1/ {
        proxy_pass http://flask-backend;
    }
    location ~^/service2/ {
        proxy_pass http://fastapi-backend;
    }
    location ~^/(docs|openapi.json)$ {
        proxy_pass http://fastapi-backend;
    }
}
  • upstream > 연결할 서버 url 정의
    • 예시 :
      upstream spring-frontend {server localhost:8080;}
  • server > location에 url mapping
    • 예시 : localhost/service1/ 로 들어오는 요청은 flask-backend(http://localhost:5000/service1)로 연결
      location /service1/ { proxy_pass http://flask-backend; }

2. {nginx설치경로}/conf/nginx.conf 파일에 위에서 만든 conf 파일 불러오도록 설정

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include mime.types;
    include sites-enabled/*.conf;
}

3. nginx 재기동

nginx -s stop # 멈추기
nginx # 시작

 

접속 확인

위의 설정파일에서 localhost/docslocalhost:5050/docs 연결했음 > 브라우저에서 localhost/docs에 접속하여 localhost:5050/docs 를 접속했을 때와 같은 서버가 뜬다면 성공

 

 

 

 

nginx 기초

nginx > 웹서버 정적 파일을 처리하는 HTTP 서버로서의 역할 HTML, CSS, Javascript, 이미지와 같은 정보들을 웹브라우저에 전송하는 역할 응용프로그램 서버에 요청을 보내는 리버스 프록시로서의 역할

bigseok.tistory.com

728x90

'Python > Web' 카테고리의 다른 글

FastAPI 개발자가 직접 개발한 FastAPI backend 프로젝트 구조  (0) 2024.03.03
WSGI와 ASGI 단순 비교  (0) 2024.02.28
FastAPI 비동기 처리  (1) 2024.02.26
FastAPI async 비동기 동시 처리  (1) 2024.02.26
nginx 기초  (0) 2022.10.25

+ Recent posts