Gunicorn & Nginx reverse-proxy 설정 삽질기

허진수·2022년 4월 9일
0

gunicorn 과 nginx를 이용하여 reverse-proxy를 설정할 경우 보통 unix 소켓을 이용하여 하게 됩니다.
이때 wsgi폴더안에 wsgi파일이 여러개 있는 구조인데 간혹 파일을 못찾는 경우가 있습니다.
이때 --chdir 옵션 을 이용하여 경로를 이동시켜주면 문제가 해결됩니다.


[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=jenkins
Group=jenkins
WorkingDirectory=/{프로젝트 폴더}
ExecStart=/{프로젝트 폴더}/venv/bin/gunicorn \
        --workers 3 \
        --bind unix:/run/gunicorn.sock \
        --preload \
        --chdir /{프로젝트 폴더} \
        config.wsgi.prod:application

[Install]
WantedBy=multi-user.target

Nginx에서 일반적으로 reverse proxy를 설정 할 경우 location / 에서 proxy를 설정하는 경우가 많은데,
다른 uri를 reverse-proxy하려고 하면 조금 바뀝니다.

server{
	listen 80;
    location /api/ {
    	include proxy_param;
        proxy_pass http://unix:/run/gunicorn.sock:/;
    }
}

특히 unix 소켓을 사용하는 경우에는 http://unix:/{gunicorn에서 설정한 소켓 위치}:/; 와 같이
뒤에 :/를 붙여주게 되면 http://localhost:80/api/abc라는 경로로 요청이 올 경우에
http://localhost:80/api/ 이 부분이 http://unix:/{gunicorn에서 설정한 소켓 위치}:/ 이부분으로 대체가 되어 gunicorn 소켓으로 요청이 전달됩니다. 이부분 설정을 잘못하면 uri 경로가 잘못되어 여러가지 문제를 겪을 수도 있습니다(/가 두개 들어가는 등...)

잘못된 내용 있으면 지적 부탁 드립니다.

profile
안녕하세요

0개의 댓글