새소식

🍪 Study/웹

Nginx - Streamlit 프록시 설정 방법

  • -
728x90

Streamlit과 Nginx 각각에서의 설정을 해줘야 하다.

 

Streamlit

아무것도 설정할 필요 없다. 기본값으로 8501번 포트에서 streamlit app이 실행된다.

만약 port 등을 설정하고 싶다면, 프로젝트 디렉토리 내 .streamlit/config.toml 에서 설정할 수 있다.

# 예시
[server]
port=8000

 

Nginx

/etc/nginx/nginx.conf 에서 nginx가 지켜볼 port와 이 port로 들어오는 요청을 전달할 주소, 즉, streamlit이 실행되고 있는 주소를 연결해줘야 한다.

 

  1. http.server 설정을 아래와 같이 해준다.
    • proxy_pass에는 streamlit app이 실행되고 있는 주소를 넣어줘야 한다.
      • 따로 설정해준 게 없다면, http://localhost:8501일 것이다.
    • /_stcore/stream 에 대해 설정해줘야 한다.
      • 만약, 설정하지 않았다면, /_stcore/stream 의 웹소켓 연결이 계속해서 실패하게 된다.
        이 경우, /_stcore/stream, /_stcore/health, /_stcore/host-config 로의 요청이 무한히 발생한다.
# nginx.conf
# ...

http {
    # Server Configs
    server {
        listen 80;
        listen [::]:80;

        charset utf-8;

        server_name domain.com; # or public ip address

        location / {
            proxy_pass http://localhost:8501;
            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;
            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;
        }

        location /_stcore/stream {
            proxy_pass http://localhost:8501/_stcore/stream;
            proxy_http_version 1.1;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_read_timeout 86400;
        }
    }

    # ... Additional Settings
}

 

  1. nginx.conf 파일을 수정했다면, 실행에 이상이 없는 지 검사해준다.
sudo nginx -t

>> 출력에 successful이 나타나면, 문제가 없는 것이다.

 

 

  1. nginx 서버를 재시작해준다.
sudo service nginx restart
728x90
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.