Others/Ubuntu and Linux

(Ubuntu) Django + Gunicon + Nginx 연동하기 (2)

print(blue) 2022. 4. 28. 14:30

Nginx

무료 오픈 소스 웹서버 소프트 웨어가벼움과 높은 성능 목표로 웹서버, 리버스 프록시, 메일 프록시 등의 기능 가지고 있음

 

1. 일단 설치부터

(django_site) ubuntu@ip-172-31-11-144:~$ sudo apt-get install nginx

1-1. 버전도 확인

(django_site) ubuntu@ip-172-31-11-144:~$ nginx -v
nginx version: nginx/1.18.0 (Ubuntu)

설치하면 나면

[ 출처 ] https://leffept.tistory.com/283?category=950490 

 

[Django]Django + Nginx + Gunicorn 연동하기 2

지난번 시간에 Django + Gunicorn 을 연동하였으니 이제는 Nginx 도 붙여 하나의 서비스를 하기 위한 준비들을 해볼 것이다. 먼저 Nginx의 특징을 간단하게 알아보고 설치를 진행할 것이다. Nginx 란? Nginx

leffept.tistory.com

 

2. nginx의 설정 파일들이 위치한 디렉토리로 이동

/etc/nginx/sites-available 디렉토리 이동

(django_site) ubuntu@ip-172-31-11-144:~$ cd /etc/nginx/sites-available

 

(django_site) ubuntu@ip-172-31-11-144:/etc/nginx/sites-available$ ls
default # 최초 설치시 이것만 있음

 

2-1. 파이보 서브스에 대한 nginx 설정 파일 관리자 권한으로 작성하기

[ /etc/nginx/sites-available/프로젝트 ]

(django_site) ubuntu@ip-172-31-11-144:/etc/nginx/sites-available$ sudo nano mysite

server {
        listen 80;
        server_name 13.124.239.181; #아이피

        location = /favicon.ico { access_log off; log_not_found off; }

        location /static {
                alias /home/ubuntu/static;
        }		# 경로 확인하기 (pwd-명령어)

        location / {
                include proxy_params;
                proxy_pass http://unix:/tmp/gunicorn.sock;
        }
}
  • [ listen 80 ] 은 웹 서버를 80 포트로 서비스 한다는 의미

HTTP 프로토콜의 기본포트는 80

따라서 이제 http://13.124.239.181:8000/ 대신 포트를 생략하여 http://13.124.239.181 처럼 웹 브라우저에서 접속 할 수 있음

  • [ server_name ] 에는 나의 고정IP 등록해줌
  • [ location /static ] 은 정적 파일에 대한 설정으로 /static으로 시작되는 URL 요청은 Nginx가 /home/ubuntu/projects/mysite/static 디렉터리의 파일을 읽어서 처리한다는 설정이다.
  • [ location / ] 은 [ location /static ] 에서 설정한 것 이외의 모든 요청은 Gunicorn이 처리하도록 하는 설정이다. 
  • proxy_pass는 동적 요청이 발생하면 해당 요청을 Gunicorn의 유닉스 소켓으로 보내라는 설정이다.

이와 같은 설정을 통해 /static 으로 시작되는 URL은 Nginx가 처리하고 나머지 URL에 대해서는 Gunicorn이 처리하게 된다.

 

이제 작성한 mysite 파일을 Nginx가 환경 파일로 읽을 수 있도록 설정해야 되는디

3. /etc/nginx/sites-enabled 디렉토리 이동

(django_site) ubuntu@ip-172-31-11-144:/etc/nginx/sites-available$ cd /etc/nginx/sites-enabled
(django_site) ubuntu@ip-172-31-11-144:/etc/nginx/sites-enabled$ ls
default

#삭제
(django_site) ubuntu@ip-172-31-11-144:/etc/nginx/sites-enabled$ sudo rm default

#삭제됐는지 확인하구 안뜨면 ㅇㅋㅇㅋ
(django_site) ubuntu@ip-172-31-11-144:/etc/nginx/sites-enabled$ ls

#mysite 파일 링크하기
(django_site) ubuntu@ip-172-31-11-144:/etc/nginx/sites-enabled$ sudo ln -s /etc/nginx/sites-available/mysite

#그럼 mysite만 남은 걸 확인할 수 있음
(django_site) ubuntu@ip-172-31-11-144:/etc/nginx/sites-enabled$ ls
mysite

 

4. 설정하구 재시작하구 다시 설정하고 재시작하고 !

(django_site) ubuntu@ip-172-31-11-144:/etc/nginx/sites-enabled$ sudo systemctl restart nginx

 

하아 . . . .

502이 뜨면 . . . . 안되는데 , , , .

설정 파일에 오류가 있는지 확인해 보려면 아래 코드 입력하면 되는데

4-1. 설정 파일에 오류가 있는지 확인하기

(django_site) ubuntu@ip-172-31-11-144:/etc/nginx/sites-enabled$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

장난하노

근데두 502번이 뜸  . . . 모가 문제지 ?!

 

상태확인해보기

(django_site) ubuntu@ip-172-31-11-144:~$ service nginx status
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-04-28 07:42:33 UTC; 5s ago
       Docs: man:nginx(8)
    Process: 130376 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 130377 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 130378 (nginx)
      Tasks: 2 (limit: 1147)
     Memory: 2.3M
     CGroup: /system.slice/nginx.service
             ├─130378 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             └─130379 nginx: worker process

Apr 28 07:42:33 ip-172-31-11-144 systemd[1]: nginx.service: Succeeded.
Apr 28 07:42:33 ip-172-31-11-144 systemd[1]: Stopped A high performance web server and a reverse proxy server.
Apr 28 07:42:33 ip-172-31-11-144 systemd[1]: Starting A high performance web server and a reverse proxy server...
Apr 28 07:42:33 ip-172-31-11-144 systemd[1]: Started A high performance web server and a reverse proxy server.

아무래두 설치나 모 ... 설정을 건드렸나봄  . . .

 

(django_site) ubuntu@ip-172-31-11-144:/etc/nginx/sites-available$ vi /etc/nginx/sites-available/mysite

대표님이 전에 적어둔 코드

server {
        listen 80;
        server_name ec2-13-124-239-181.ap-northeast-2.compute.amazonaws.com;

        location = /favicon.ico { access_log off; log_not_found off; }

        location /static {
                alias /home/ubuntu/static;
        }

        location / {
                include proxy_params;
               # proxy_pass http://unix:/home/ubuntu/run/gunicorn.sock;
                #proxy_pass     http://ec2-13-124-239-181.ap-northeast-2.compute.amazonaws.com:8000;
                proxy_pass      http://0.0.0.0:8000;
                proxy_buffer_size       128;
                proxy_buffers           4 256k;
                proxy_busy_buffers_size 256k;
        }
}

 

http://13.124.239.181/

 

오류 부분은 . . 여기서 -> https://print-blue.tistory.com/15