nginx 修改响应Body并代理流媒体服务分享播放页
需求描述
需求场景,部署了一个视频平台,其存在分享页,现在需要通过nginx代理到分享页进行视频播放。注意:分享页中会访问一个接口,返回body里面会携带具体的视频流播放地址,但这相关的播放地址是直接指向目标服务器,可以通过设置Host来改变IP,但协议返回就存在问题了。
使用nginx代理,需要同步让返回body里面的内容也指向nginx,然后代理到目标服务器。
实现
需要验证nginx是否存在 --with-http_sub_module 模块,这个好像是自带了的,其可以修改响应头body里面的内容。
nginx -V | grep with-http_sub_module
nginx配置
worker_processes 4;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
client_max_body_size 100M;
keepalive_timeout 65;
server {
listen 443 ssl;
server_name local.theorydance.com _;
ssl_certificate D:/Environment/mkcert/local.theorydance.com.pem;
ssl_certificate_key D:/Environment/mkcert/local.theorydance.com-key.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers on;
error_page 404 /custom_404.html;
error_page 500 502 503 504 /50x.html;
# 分享页内部会发起的接口请求,就是这里需要修改相应body里面的内容
location = /api/v1/stream/start {
proxy_set_header Accept-Encoding "";
# nginx 默认不会proxy host参数,需要手动指定
proxy_set_header Host $host;
sub_filter 'http://' 'https://';
sub_filter 'ws://' 'wss://';
sub_filter 'webrtc://' 'webrtcs://';
sub_filter_once off; # 一次替换,还是多次重复替换
sub_filter_types application/json text/html;
proxy_pass http://192.168.1.187:10000;
}
# 页面中默认的视频流播放方式会通过websocket方式建立连接,此处只配置了websocket,如果是其他方式,需要多复制几个location /sms/xxx 具体的代理采用不同的配置区分即可
location /sms/ {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
# WebSocket长连接配置
proxy_read_timeout 600s; # 延长超时时间
proxy_send_timeout 600s;
# 代理缓冲配置
proxy_buffering off;
proxy_buffer_size 16k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
# 添加X-Forwarded-*头信息,传递客户端真实信息
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_pass http://192.168.1.187:10000;
}
# 播放页及需要的静态资源,可以写需要的完整资源location配置,这里直接简写偷懒了
location / {
proxy_pass http://192.168.1.187:10000;
}
}
}
不使用sub_filter之前接口返回内容
{
"AudioEnable": false,
"CDN": "",
"CascadeSize": 0,
"ChannelID": "50000000210100000021",
"ChannelName": "红坝机场040Y机场摄像头",
"ChannelPTZType": 0,
"CloudRecord": false,
"DeviceID": "50000000210100000021",
"Duration": 28068,
"FLV": "http://local.theorydance.com/sms/34020000002026000002/flv/hls/50000000210100000021_50000000210100000021.flv",
"HLS": "http://local.theorydance.com/sms/34020000002026000002/hls/50000000210100000021_50000000210100000021/live.m3u8",
"InBitRate": 303,
"InBytes": 1124936015,
"NumOutputs": 2,
"Ondemand": false,
"OutBytes": 152986190,
"RTMP": "rtmp://192.168.1.187:11935/hls/50000000210100000021_50000000210100000021",
"RTPCount": 1405439,
"RTPLostCount": 0,
"RTPLostRate": 0,
"RTSP": "rtsp://192.168.1.187:12000/50000000210100000021_50000000210100000021",
"RecordStartAt": "",
"RelaySize": 0,
"SMSID": "34020000002026000002",
"SnapURL": "/sms/34020000002026000002/snap/50000000210100000021/50000000210100000021.jpg?t=1786672244131088498",
"SourceAudioCodecName": "",
"SourceAudioSampleRate": 0,
"SourceVideoCodecName": "h264",
"SourceVideoFrameRate": 30,
"SourceVideoHeight": 720,
"SourceVideoWidth": 1280,
"StartAt": "2026-08-14 02:11:24",
"StreamID": "stream:50000000210100000021:50000000210100000021",
"Transport": "TCP",
"VideoFrameCount": 841979,
"WEBRTC": "webrtc://local.theorydance.com/sms/34020000002026000002/rtc/50000000210100000021_50000000210100000021",
"WS_FLV": "ws://local.theorydance.com/sms/34020000002026000002/ws-flv/hls/50000000210100000021_50000000210100000021.flv"
}
使用sub_filter之后接口返回内容
{
"AudioEnable": false,
"CDN": "",
"CascadeSize": 0,
"ChannelID": "50000000210100000021",
"ChannelName": "红坝机场040Y机场摄像头",
"ChannelPTZType": 0,
"CloudRecord": false,
"DeviceID": "50000000210100000021",
"Duration": 28068,
"FLV": "https://local.hbgrand.com/sms/34020000002026000002/flv/hls/50000000210100000021_50000000210100000021.flv",
"HLS": "https://local.hbgrand.com/sms/34020000002026000002/hls/50000000210100000021_50000000210100000021/live.m3u8",
"InBitRate": 303,
"InBytes": 1124936015,
"NumOutputs": 2,
"Ondemand": false,
"OutBytes": 152986190,
"RTMP": "rtmp://192.168.1.187:11935/hls/50000000210100000021_50000000210100000021",
"RTPCount": 1405439,
"RTPLostCount": 0,
"RTPLostRate": 0,
"RTSP": "rtsp://192.168.1.187:12000/50000000210100000021_50000000210100000021",
"RecordStartAt": "",
"RelaySize": 0,
"SMSID": "34020000002026000002",
"SnapURL": "/sms/34020000002026000002/snap/50000000210100000021/50000000210100000021.jpg?t=1786672244131088498",
"SourceAudioCodecName": "",
"SourceAudioSampleRate": 0,
"SourceVideoCodecName": "h264",
"SourceVideoFrameRate": 30,
"SourceVideoHeight": 720,
"SourceVideoWidth": 1280,
"StartAt": "2026-08-14 02:11:24",
"StreamID": "stream:50000000210100000021:50000000210100000021",
"Transport": "TCP",
"VideoFrameCount": 841979,
"WEBRTC": "webrtcs://local.hbgrand.com/sms/34020000002026000002/rtc/50000000210100000021_50000000210100000021",
"WS_FLV": "wss://local.hbgrand.com/sms/34020000002026000002/ws-flv/hls/50000000210100000021_50000000210100000021.flv"
}

浙公网安备 33010602011771号