«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

VIDEOCUBE

[Cube Project] Nginx + Python CGI 연동 준비 본문

프로젝트

[Cube Project] Nginx + Python CGI 연동 준비

라떼청년 2017. 12. 4. 02:13





Nginx 에 CGI 를 연결하여 MP4 를 요청이 왔을 때는 정적으로 Mp4 파일을 가지고 갈 수 있도록 하며

기타 HLS ( 이하 m3u8, ts ) DASH ( 이하 dash, m4v, m4a ) 요청 시에는 CGI 로 이동하여 변환하여 리턴이 가능하도록 진행 하려고 한다.


모든 요청은 Nginx 를 통해서 들어올 것이며 포트번호 8000으로 외부 접속은 하지 않도록 한다.


nginx 설치는 참조 하고

http://videocube.tistory.com/entry/Nginx-%EC%84%A4%EC%B9%98%ED%95%98%EA%B8%B0


CGI 설정을 하는 법을 다루도록 한다.

python 을 8000 으로 로드 시키는 법은 

http://videocube.tistory.com/entry/Python-%EC%84%9C%EB%B2%84-%EA%B5%AC%EC%B6%95%ED%95%98%EA%B8%B0

을 참조하기 바란다.


root@linux-01:/root> netstat -nlp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      978/nginx           

tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      889/sshd            

tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      965/master          

tcp        0      0 0.0.0.0:8000                0.0.0.0:*                   LISTEN      1045/python         

tcp        0      0 :::22                       :::*                        LISTEN      889/sshd            

tcp        0      0 ::1:25                      :::*                        LISTEN      965/master 


두개의 포트를 확인하면 nginx 설정을 다음과 같이 변경하자.

기존 설정에서 document 폴더를 다음으로 설정해 보자

nginx document path /data/web

.

├── Robotica.mp4

├── cgi-bin

│   ├── hello.py

│   └── test.pyc

└── index.html

폴더 구조는 위와 같다.

Robotica.mp4 는 테스트 할 mp4 파일이다.


편의를 위해 Python의 document 경로와 Nginx document 경로를 한곳으로 정한다.

vi /etc/nginx/nginx.conf

user  nginx;

worker_processes  1;


error_log  /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;



events {

    worker_connections  1024;

}



http {

    include       /etc/nginx/mime.types;

    default_type  application/octet-stream;


    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';


    access_log  /var/log/nginx/access.log  main;


    sendfile        on;

    keepalive_timeout  65;


    include /etc/nginx/conf.d/*.conf;

}


server {

    listen       80;

    server_name  localhost;

    location / {

        root   /data/web;

        index  index.html index.htm;

    }


    error_page   500 502 503 504  /50x.html;

    location = /50x.html {

        root   /usr/share/nginx/html;

    }

}

index.html 파일은 

다음과 같이 작성 하였다.

<html>

        <body>

                VideoCube Media Server

        </body>

</html>



DNS 는 media 를 등록 하였다.

웹에서 http://media.videocube.lab 을 호출해 보자


[설정 전]


[설정 후]


정상적으로 변경이 되었다.


다음은 Nginx 를 CGI 에 연결하기 > Proxy 를 이용할 예정이다

현재 80 포트에 연결했을 경우 document 폴더에는 


index.html, Robotica.mp4 파일이 있다.


우선 TEST 로 

Roboticat.mp4 이 호출이 될 경우 Python Port ( 8000 ) 의 cgi-bin 의 hello.py 를 호출 하여 웹에 뿌려 보도록 한다.


hello.py 는 

http://videocube.tistory.com/entry/Python-%EC%84%9C%EB%B2%84-%EA%B5%AC%EC%B6%95%ED%95%98%EA%B8%B0

에서 참조 바란다.


Browser > Web ( 80 ) > Python CGI ( 8000 ) > cgi-bin > hello.py 호출


server {

    listen       80;

    server_name  localhost;

    location / {

        root   /data/web;

        index  index.html index.htm;

    }


location ~ \.mp4$ {

        rewrite ^/(.*) /cgi-bin/hello.py break;

        proxy_pass http://127.0.0.1:8000;

    }


    error_page   500 502 503 504  /50x.html;

    location = /50x.html {

        root   /usr/share/nginx/html;

    }

}

cgi > 인자값을 전달하는 구조로

hello.py?file=Robotica.mp4 로 전달하는 방법을 구해보자

server {

    listen       80;

    server_name  localhost;

    location / {

        root   /data/web;

        index  index.html index.htm;

    }


location ~ \.mp4$ {

        rewrite ^/(.*) /cgi-bin/hello.py?file=$1 break;

        proxy_pass http://127.0.0.1:8000;

    }


    error_page   500 502 503 504  /50x.html;

    location = /50x.html {

        root   /usr/share/nginx/html;

    }

}



Request 기록

localhost.localdomain - - [04/Dec/2017 02:05:57] "GET /cgi-bin/hello.py?file=Robotica.mp4 HTTP/1.0" 200 -


Python 에서 file 을 받아 화면에 뿌려보자

vi /data/web/cgi-bin/hello.py 

#!/usr/bin/env python


import cgi


form = cgi.FieldStorage()


fileName = form.getvalue('file')


print "Content-type: text/html\n\n"

print '<html>'

print '<head>'

print '<title> VideoCube MediaSource </title>'

print '</head>'

print '<body>'

print '<h2> FileName = ' + fileName + '</h2>'

print '</body>'

print '</html>'


연동 되었다..


다음은 해당 파일을 분석하여 샘플링을 해보자~~~
















반응형
Comments