[실무개발]/DevOps, Linux, Analytics

python 장고 웹소켓 통신위한 daphne 환경 구성

_niel 2023. 11. 14. 12:01

Nginx 환경 설정

server {
        server_name _;

        location /api {
                include uwsgi_params;
                rewrite ^/api(.*)$ $1?$args break;
                proxy_pass http://unix:/run/gunicorn.sock;
        }

        location /ws {
                proxy_http_version 1.1;
                proxy_set_header    Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_redirect off;
                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_pass http://daphne-server;
        }
}

upstream daphne-server {
        server 127.0.0.1:8443;
}

 

upstream 으로 daphne 다중 클러스터 로드밸런싱

 

Daphne service 등록

vi /etc/systemd/system/daphne.service
[Unit]
Description=daphne daemon

[Service]
User=ubuntu
Group=ubuntu
WorkingDirectory=/opt/web/{project_name}

Environment="CUDA=cuda:0"
Environment="PATH=/home/wisysta/anaconda3/envs/{env_name}"
ExecStart=/home/wisysta/anaconda3/envs/{env_name}/bin/daphne \
          -b 0.0.0.0 -p 8443 {django_service_name}.asgi:application

ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
Restart=on-abort
PrivateTmp=true

[Install]
WantedBy=multi-user.target