NGINX PROXY

วันนี้จะมาพูดถึงการทำ web streaming > nginx proxy > object storage มาดูรายละเอียดว่าต้องทำยังไงบ้าง เพื่อป้องกันการนำลิ้งตรงไปใช้งาน

web streaming

ส่วนของ web streaming มีสิ่งที่ต้องเตรียมดังนี้

1.เขียนสคิปสำหรับสร้าง uuid v4 โดยกำหนดให้รันทุกๆ 00:00 ของทุกวัน

<?php
// สร้าง UUIDv4
function generateUUIDv4() {
    return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        mt_rand(0, 0xffff), mt_rand(0, 0xffff),
        mt_rand(0, 0xffff),
        mt_rand(0, 0x0fff) | 0x4000,
        mt_rand(0, 0x3fff) | 0x8000,
        mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
    );
}


// สร้าง UUIDv4
$uuid = generateUUIDv4();


// บันทึก UUIDv4 ลงในไฟล์ uuid.txt
$file = fopen("uuid.txt", "w");
if ($file) {
    fwrite($file, $uuid);
    fclose($file);
    echo "UUIDv4 ถูกสร้างและบันทึกลงในไฟล์ uuid.txt แล้ว";
} else {
    echo "เกิดข้อผิดพลาดในการสร้างไฟล์";
}
?>


Run ทุก 00:00 ของทุกวัน

0 0 * * * https://domain.com/get-uuid.php


2.ปรับแก้ code ส่วนของการเรียก player เพื่อเรียก ลิ้งที่มาจาก nginx proxy

<?php
$videoUrl = $movies_info->video_url;
$videoFilename = pathinfo(parse_url($videoUrl, PHP_URL_PATH), PATHINFO_FILENAME);
$uuid = file_get_contents('https://domain.com/uuid.txt');
$newVideoUrl = "http://x.x.x.x/get/$uuid/$videoFilename.mp4";
echo $newVideoUrl;
?>



nginx proxy

getKey from https://domain.com/uuid.txt

ใช้ python เขียนค่า nginx.conf เพื่อกำหนด key ให้กับ proxy

import requests
import os


# ดึง key จาก URL
url = "https://domain.com/uuid.txt"
response = requests.get(url)
key = response.text.strip()

# ตรวจสอบว่า key ไม่ว่างเปล่า
if not key:
    print("Error: Key is empty")
    exit(1)

# สร้างหรืออัพเดตไฟล์ nginx.conf
config = f"""
worker_processes  1;


events {{
    worker_connections  1024;
}}

http {{
    include       mime.types;
    default_type  application/octet-stream;
    resolver 8.8.8.8 8.8.4.4; # Google Public DNS

    sendfile        on;
    keepalive_timeout  65;

    server {{
        listen 80 default_server;
        listen [::]:80 default_server;

        # ปรับแต่งการจัดการ URL สำหรับสตรีม
        location ~ ^/get/{key}/(.+)\.mp4$ {{
            proxy_pass https://sgp1.vultrobjects.com:443/movies/$1.mp4;
            proxy_set_header Host sgp1.vultrobjects.com;
            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;
        }}


        # การจัดการหน้าเออเรอร์
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {{
            root   html;
        }}
    }}
}}
"""

# บันทึกไฟล์คอนฟิก
config_file_path = "/usr/local/nginx/conf/nginx.conf"
with open(config_file_path, "w") as f:
    f.write(config)

# รีสตาร์ท NGINX หรือรีโหลดการตั้งค่า
os.system("sudo /usr/local/nginx/sbin/nginx -s reload")

print("Nginx config file has been created or updated.")



object storage

ส่วนนี้ยังไม่ต้องแก้ไขอะไร

0
132