gsls200808 发表于 2023-4-28 16:03

通过nginx将内网源转发到ipv6外网

本帖最后由 gsls200808 于 2023-6-10 11:15 编辑

除了个别省份,大部分家宽都能用上ipv6了。越来越多的iptv都开始限制外省和外运营商了。
默认情况下光猫默认禁止ipv6入站,需要在光猫防火墙配置里关闭 IPv6 Session保护后才能开启。更安全的做法需要配置ipv6防火墙,只暴露必要的ip和服务,需要自己加硬件配置。
以orangepi搭建nginx转发 为例 cpu架构为armv7 系统为ubuntu20.04
1.安装docker和docker-comose
#安装docker
wget https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/armhf/docker-ce-cli_20.10.7_3-0_ubuntu-xenial_armhf.deb
wget https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/armhf/containerd.io_1.4.6-1_armhf.deb
wget https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/armhf/docker-ce_20.10.7_3-0_ubuntu-xenial_armhf.deb
sudo dpkg -i *.deb

#查看docker版本
docker version
#安装docker-compose
wget https://github.com/docker/compose/releases/docker-compose-linux-armv7
#拷贝到/usr/local/bin/目录
cp docker-compose-linux-armv7 /usr/local/bin/docker-compose
#可执行权限
chmod +x /usr/local/bin/docker-compose
#查看docker-compose版本
docker-compose version

2.安装nginx
编写docker-compose.yml配置文件,内容如下
version: "2.1"
services:
nginx:
    container_name: nginx
    image: 'nginx:1.20.0'
    ports:
      - '80:80'
    volumes:
      #- '${PWD}/nginx/html:/usr/share/nginx/html'
      - '${PWD}/nginx/conf.d:/etc/nginx/conf.d'
    environment:
      - TZ=Asia/Shanghai
    restart: always

保存,在yml文件同目录执行
docker-compose up -d

在当前目录下找到并进入/nginx/conf.d目录
创建文件default.conf,内容如下
server {
    listen       80;
    listen[::]:80;
    server_namelocalhost;

    location ~* \.(m3u8|ts|aac)$ {
      proxy_cache off;                  # 禁用代理缓存
      expires -1;                         # 禁用页面缓存
      proxy_pass http://example.com;      # 反代目标 URL
      sub_filter 'http://example.com/' 'http://$host/';   # 替换 m3u8 文件里的资源链接
      sub_filter_last_modified off;       # 删除原始响应里的浏览器缓存值
      sub_filter_once off;                # 替换所有匹配内容
      sub_filter_types *;               # 匹配任何 MIME 类型
    }
}

将example.com换成你需要代理的iptv域名或ip,注意域名和ip不要有重定向。有则需要做进一步处理。

最后执行docker restart nginx,使nginx配置生效。

查看本机ipv6
ifconfig
curl 6.ipw.cn
将你的iptv地址替换成你服务器的ipv6地址进行访问。

离家时可以通过4G网直接访问进行观看。如果是位于商场公司一般没有纯ipv6环境,可以考虑cloudflare 申请域名配置纯ipv6转纯ipv4 或者Zerotier tailscale组远程局域网访问。
转发的意义可以主要在于可以异地看到稳定的高清地面频道。大部分家宽上行是30M,高清一般码率为8M,足够3台设备观看了。
辽宁移动nginx配置,由于存在重定向,需要做跟踪重定向的配置,域名源可以代理全部,ip源只能代理部分。
server {
    listen       80;
    listen[::]:80;
    server_namelocalhost;

    location @handle_redirect {
      set $saved_redirect_location '$upstream_http_location';
      proxy_pass $saved_redirect_location;
    }


    location ~* \.(m3u8|ts|aac)$ {
      proxy_cache off;                  # 禁用代理缓存
      expires -1;                         # 禁用页面缓存
      proxy_pass http://hwottcdn.ln.chinamobile.com;      # 反代目标 URL
      sub_filter 'http://hwottcdn.ln.chinamobile.com/' 'http://$host/';   # 替换 m3u8 文件里的资源链接
      sub_filter_last_modified off;       # 删除原始响应里的浏览器缓存值
      sub_filter_once off;                # 替换所有匹配内容
      sub_filter_types *;               # 匹配任何 MIME 类型
      proxy_intercept_errors on;
      recursive_error_pages on;
      error_page 301 302 307 = @handle_redirect;
    }

}






1234sunzhe 发表于 2023-4-28 16:08

你说的内网源是组播吗?

gsls200808 发表于 2023-4-28 16:17

1234sunzhe 发表于 2023-4-28 16:08
你说的内网源是组播吗?

这里说的内网源是单播源

1234sunzhe 发表于 2023-4-28 16:48

gsls200808 发表于 2023-4-28 16:17
这里说的内网源是单播源

rtsp源?

gsls200808 发表于 2023-4-28 20:26

1234sunzhe 发表于 2023-4-28 16:48
rtsp源?

是http源。

1234sunzhe 发表于 2023-4-28 22:14

本帖最后由 1234sunzhe 于 2023-4-28 22:55 编辑

gsls200808 发表于 2023-4-28 20:26
是http源。
那不用这么麻烦,用Socat端口转发这个插件就行了

fanmingming 发表于 2023-4-28 22:49

rtsp的单播源一般只要是同省份同运营商可以直接播放,无需转发。

Canton星仔 发表于 2024-3-8 01:12

如果是rtsp源应该怎么操作

gsls200808 发表于 2024-3-13 18:38

Canton星仔 发表于 2024-3-8 01:12
如果是rtsp源应该怎么操作

如果只推送一个频道 vlc opensrs都可以,如果需要通过参数切换频道,nginx ffmpeg重新编码可以实现,不过目前网上没有成品

oojt 发表于 2024-9-10 07:53

大佬 使用这个代码 有卡断 基本是在30秒 或75秒 就断了

kksss 发表于 2024-9-14 15:09

大佬,请问下我的这个url 实际访问的话会跳转到另一个url才会播放,我这个nginx配置文件应该怎么写,http://hwltc.tv.cdn.zj.chinamobile.com/PLTV/88888888/224/3221236222/index.m3u8

gsls200808 发表于 2024-9-20 12:29

kksss 发表于 2024-9-14 15:09
大佬,请问下我的这个url 实际访问的话会跳转到另一个url才会播放,我这个nginx配置文件应该怎么写,http:/ ...

nginx 对于复杂的跳转也无能为力,只能通过写代码解决。openresty 或者golang
页: [1]
查看完整版本: 通过nginx将内网源转发到ipv6外网