Termux中安装nginx,实现播放的302重定向。

一、Nginx安装与配置

1.在 Termux 中安装 Nginx

1
2
pkg update -y
pkg install nginx -y

2.替换 Nginx 配置文件

安装完成后,Nginx 的配置文件默认藏在 Termux 的深处。我们需要把里面的内容换成咱们的“劫持分流”规则。

2.1直接在 Termux 里用 nano 编辑配置文件:

1
nano /data/data/com.termux/files/usr/etc/nginx/nginx.conf

进入编辑界面后,把里面原有的代码全部删干净(如果你连着蓝牙键盘,狂按删除;或者长按屏幕选全选删除)。

把下面这段我为你写好的终极分流配置,原封不动地粘贴进去:

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
32
33
34
35
36
37
38
39
40
41
42
worker_processes  1;
events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

# 这里就是你的“新 VIP 车道”
server {
listen 8097;
server_name localhost;

# 🚨 核心劫持区:看到 Infuse 请求播放流,立刻拦下!
# 把它转交给本地 5001 端口的 Python 劫持脚本(咱们下一步来搞定它)
location ~* /videos/(.*)/(stream|original) {
proxy_pass http://127.0.0.1:5001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

# 🟢 普通通行区:海报、菜单、刮削,统统老老实实交给 8096 的 Emby 本尊
location / {
proxy_pass http://127.0.0.1:8096;
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_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Protocol $scheme;
proxy_set_header X-Forwarded-Host $http_host;

# 必须加上这段,否则 Emby 网页端的 WebSocket 会断,导致进度条不同步
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}

粘贴好之后,保存退出(在 nano 里按 Ctrl + X,然后按 Y,最后按 回车)。

2.2或者用其它方法从电脑传递文件然后CP

首先在电脑新建nginx.conf,然后粘贴如上代码保存。通过openlist上传至termux根目录下,再运行如下操作。

把根目录的配置,强行覆盖到 Nginx 真正的老巢去:

1
cp ~/nginx.conf $PREFIX/etc/nginx/nginx.conf

核心大招:一键清洗 Windows 的隐形换行符(\r):

1
sed -i 's/\r$//' $PREFIX/etc/nginx/nginx.conf

3.启动并测试 Nginx

1
2
# 检查配置文件语法有没有错
nginx -t

如果输出显示 syntax is ok 和 test is successful,说明完美!

接着启动 Nginx:

1
nginx

(如果以后修改了配置,只需要运行 nginx -s reload 重载即可)

验收成果
现在,你的交通枢纽已经建好了!
你可以把你手机穿透工具(Cloudflared 或 frpc)里原本指向 8096 的端口,改成 8097。

你现在去外网用浏览器打开你的域名,你会发现:网页能完美打开 Emby,海报墙刷得飞快!
这就证明 Nginx 已经成功把正常流量代理给 Emby 了,而且你完全感觉不到 Nginx 的存在。

你先走到这一步,测试一下海报墙能不能正常刷出来。确认没问题了跟我说,我们来写最后那个跑在 5001 端口的 极简 Python 302 劫持脚本,彻底打通 Infuse 的任督二脉!

二、植入“302 劫持大脑” (5001 端口)

Nginx 拦截到 Infuse 的 /stream 播放请求后,会把请求扔给 127.0.0.1:5001。
我们现在就来写这个跑在 5001 端口上的极简 Python 脚本。它的逻辑堪称流氓但极度有效:

它收到请求后 ➜ 向 Emby 查一下这个视频的物理路径 ➜ 发现是 .strm 文件 ➜ 直接把文件里的直链抠出来,当场给 Infuse 甩一个 302 强制重定向!

1.获取 Emby API 密钥
为了让脚本能跟 Emby 对话查路径,你需要:
进入你的 Emby 网页端后台 ➜ 左侧菜单拉到底找 [API 密钥] ➜ 点击 [+ 新 API 密钥] ➜ 随便取个名字(比如 Hijack)➜ 复制那一长串密钥。

2.创建nginx_302.py
在 Termux 里(你可以和 cas_server.py 放在同一个目录下),新建一个脚本:

1
nano nginx_302.py

把下面这段代码粘进去,并把第二行的 EMBY_API_KEY 替换成你刚复制的密钥:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from flask import Flask, request, redirect
import requests
import os
import re
import logging

# ==========================================
# 🤫 静音配置:安全屏蔽每一次播放时的刷屏日志
# ==========================================
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)

app = Flask(__name__)

# ==========================================
# ⚙️ 核心配置区
# ==========================================
EMBY_HOST = "http://127.0.0.1:8096"
EMBY_API_KEY = "这里填入你的Emby_API密钥" # 👈 保持你之前的密钥
CAS_SERVER_LOG_API = "http://127.0.0.1:5000/api/remote_log"
# ==========================================

def send_log(msg, level="INFO"):
"""不仅在终端打印,还把日志发送到 cas_server 的网页控制台"""
print(f"[{level}] {msg}")
try:
requests.post(CAS_SERVER_LOG_API, json={"msg": msg, "level": level}, timeout=2)
except:
pass

@app.route('/', defaults={'path': ''})
@app.route('/<path:full_path>')
def catch_all(full_path):
match = re.search(r'/videos/(\d+)/(stream|original)', request.path, re.IGNORECASE)

if not match:
return redirect(f"{EMBY_HOST}{request.full_path}", code=302)

item_id = match.group(1)

try:
query_url = f"{EMBY_HOST}/emby/Items?Ids={item_id}&Fields=Path&api_key={EMBY_API_KEY}"
res = requests.get(query_url, timeout=5).json()

items = res.get('Items', [])
if not items:
return redirect(f"{EMBY_HOST}{request.full_path}", code=302)

file_path = items[0].get('Path', '')
file_name = file_path.split('/')[-1] if file_path else "未知文件"

if file_path and file_path.lower().endswith('.strm') and os.path.exists(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
strm_url = f.read().strip()

send_log(f"🚀 [Nginx劫持] 强行纠正 Infuse 走向直链 -> {file_name}", "SUCCESS")
return redirect(strm_url, code=302)
else:
send_log(f"▶️ [正常放行] 常规文件交回 Emby 处理 -> {file_name}", "INFO")
return redirect(f"{EMBY_HOST}{request.full_path}", code=302)

except Exception as e:
send_log(f"❌ [劫持出错] 已启动兜底放行机制: {e}", "ERROR")
return redirect(f"{EMBY_HOST}{request.full_path}", code=302)

if __name__ == '__main__':
send_log("🚦 Infuse 302 劫持网关已安全启动!(运行于 5001 端口)", "SUCCESS")
app.run(host='127.0.0.1', port=5001)

保存退出(Ctrl + X ➜ Y ➜ 回车)。
3.启动它,见证奇迹!

1
python nginx_302.py

现在,打开你的 Infuse,连上你改过端口的新地址,随便点开一部我们转存的秒传剧。
你看一眼 Termux 的屏幕,只要弹出 🚀 [劫持成功] 强行纠正 Infuse 走向直链…,恭喜你,你已经彻底打通了整个家庭影院圈子里最难搞定的 iOS 全局直链播放!

快去试!我已经迫不及待想听你的好消息了!

三、把启动服务加入PM2进程管理

1.nginx_302.py

1
pkill nginx_302
1
cd ~/tcloud && pm2 start nginx_302.py --name "nginx_302" --interpreter python

重启这个 PM2 进程并重置崩溃计数:

1
pm2 restart 8 --update-env

2.nginx

Nginx 的默认性格是“深藏功与名”。一启动,它就会自己潜入系统后台(这叫 Daemon 守护进程),把控制权交还给终端。
但是 PM2 是个强迫症保安,它必须一直盯着进程的肉身(前台运行)。如果 Nginx 潜入了后台,PM2 就会以为它死了!然后 PM2 就会疯狂地去拉起它,拉起又潜入后台,PM2 又以为它死了……最终导致 Nginx 无限重启报错

完美解决方案(只需 3 步)
要让 PM2 乖乖接管 Nginx,我们只需要在启动时加一个参数:daemon off;,强迫 Nginx 站在前台让 PM2 盯着。

2.1杀掉目前在后台偷偷跑的 Nginx(清理战场):

1
pkill nginx

2.2让 PM2 用“前台模式”接管 Nginx:

1
pm2 start "nginx -g 'daemon off;'" --name nginx

(注意看引号,外面是双引号,里面是单引号,原封不动复制进去执行)

2.3保存 PM2 的进程列表(防重启丢失):

1
pm2 save

你会看到一张极其舒适的表格!你的 cas_server、nginx_302 (5001) 和 nginx 整整齐齐地排在一起,全都是绿色的 online!

以后如果改了 Nginx 的配置文件,你再也不用去敲原生的 Nginx 命令了,直接用 PM2 统一管理:

重启 Nginx: pm2 restart nginx

看 Nginx 日志: pm2 logs nginx

停止 Nginx: pm2 stop nginx

你的整个家庭影院底层架构,到这一步已经彻底完成了“大一统”,完美闭环!