Windwos下编译Nginx并添加http-flv-modul

2023-08-11  本文已影响0人  高呼吾名

一.源码

二.工具

三.源码结构组织

  1. 新建文件夹NginxCode
  2. 将第一步源码里列的文件解压到NginxCode文件夹下,同级即可
    [图片上传失败...(image-4ac7eb-1691834357606)]

四.安装工具程序

默认路径安装完即可,如不是默认路径,可能需要手动设置环境变量,根据自己的实际情况决定要不要设置环境变量

五.生成Makefile文件

  1. 打开msys2.exe,cd到NginxCode文件夹
  2. 执行下面代码

执行前需要把 --with-pcre --with-zlib --with-openssl 的路径改成自己的文件
--prefix 对应的路径就是编译好以后的nginx.exe 所在的目录,这个要一致,否则无法启动nginx

auto/configure \
--with-cc=cl \
--with-debug \
--with-stream \
--with-http_ssl_module \
--with-cc-opt=-DFD_SETSIZE=1024 \
--with-pcre=../pcre2-10.42 \
--with-zlib=../zlib-1.2.13 \
--with-openssl=../openssl-3.1.2 \
--with-openssl-opt=no-asm \
--prefix=E:/nginx \
--sbin-path=nginx.exe \
--conf-path=conf/nginx.conf \
--pid-path=logs/nginx.pid \
--http-log-path=logs/access.log \
--error-log-path=logs/error.log \
--http-client-body-temp-path=temp/client_body_temp \
--http-proxy-temp-path=temp/proxy_temp \
--http-fastcgi-temp-path=temp/fastcgi_temp \
--http-scgi-temp-path=temp/scgi_temp \
--http-uwsgi-temp-path=temp/uwsgi_temp \
--add-module=../nginx-http-flv-module

六. 使用nmake编译nginx.exe

  1. 打开命令行,这里最好使用vs文件夹下的命令行工具

我自己电脑上用powershell 会编译失败

[图片上传失败...(image-32ba9a-1691834357606)]

  1. 切换到源码根目录 执行 nmake –f objs/Makefile

如果nmake的环境变量添加不进去可以直接指定nmake的执行程序

E:\VisualStudio2022\VC\Tools\MSVC\14.35.32215\bin\Hostx64\x64\nmake.exe -f objs/Makefile

如果不知道nmake的路径在哪,可以通过上面vs下打开的那个控制台用 where nmake 命令来打印路径

七.组织nginx程序目录结构

  1. 等待nmake编译完成后,nginx的源码文件夹下的objs文件下会生成nginx.exe执行程序
  2. 创建上述--prefix 对应的文件夹作为程序的根目录
  3. 复制编译好的nginx.exe 执行程序和nginx源码文件夹下的conf contrib html 到新建的文件夹下
  4. 创建logs temp 两个文件夹
  5. 给新建的nginx文件夹完全控制权限

八. http-flv 服务的nginx配置文件

worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

#pull rtmp://58.200.131.2:1935/livetv/hunantv;

#rtmp_auto_push on;
#rtmp_auto_push_reconnect 1s;
#rtmp_socket_dir /tmp;
#rtmp  模块配置
rtmp {
    out_queue    4096;
    out_cork     8;
    max_streams  128;
    timeout      10s;
    
    server {
        listen 1935;  #obs推流端口
        
  
        application live {  
            live on;
            #wait_key on;
            #interleave off;  
        }
        
        ping 0s;

        application hls {
            gop_cache off;              # 禁用GOP缓存以减少视频首帧等待时间
            live on;                    # 开启直播模式
            hls on;                     # 开启HLS功能
            meta copy;                  # 将原始媒体文件的元数据直接拷贝到输出的HLS文件中
            session_relay on;           # 开启会话转发模式,可用于实现转发、复制等功能
            drop_idle_publisher 10s;    # 如果超过10秒没有活跃的发布者连接,则断开连接
            interleave off;             # 关闭HLS流的交错模式
            wait_key on;                # 当等待关键帧时暂停输出
            hls_fragment 2s;            # 指定每个HLS片段的持续时间为2秒
            hls_playlist_length 10s;    # 指定HLS播放列表的长度为10秒
            hls_nested on;              # 允许嵌套目录结构

            recorder chunked {
                record all;                        # 录制所有输入流
                record_interval 60m;               # 指定每个录制文件的最大长度为60分钟
                record_path www/record;            # 指定录制文件保存的路径
                record_suffix _%Y-%m-%d-%H-%M-%S.flv;   # 录制文件的文件名后缀,使用时间戳
            }
        }
        
    }
}

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;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8075;   #设置得ip端口
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
        }
        
        location /live {
            flv_live on;
            chunked_transfer_encoding  off; #open 'Transfer-Encoding: chunked' response
            add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
            add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
            add_header Access-Control-Allow-Headers X-Requested-With;
            add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
            add_header 'Cache-Control' 'no-cache';
        }
        
        location /flv {
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
                add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
                add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Range';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                return 204;
            }
            add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
            add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
            add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Range';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            alias www/yamdi_record;  
            autoindex on; 
        }
        location /image {  
            add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
            add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
            add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Range";
            add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
            alias www/image;  
            autoindex on; 
        }
        location /urecord {
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
                add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
                add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Range';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                return 204;
            }       
            add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
            add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
            add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
            add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
            alias www/urecord;  
            autoindex on; 
        }

 
    }
    

    server {
        listen 8089;#设置得nginx本地端口
        server_name 127.0.0.1;
        location / {
        
                
        
                proxy_set_header    Host    $host;
                proxy_set_header    Remote_Addr $remote_addr;
                proxy_set_header    X-Real-IP $remote_addr;
                proxy_set_header    X-Forwarded-For $remote_addr;
    
                
                proxy_connect_timeout 1;
                add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
            add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
            add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
            add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
        }
        
    }

}

推流地址为:rtmp://127.0.0.1/live/test
拉流地址为:http://127.0.0.1:8075/live?port=1935&app=live&stream=test

如果使用obs推流 推流地址为:rtmp://127.0.0.1/live 推流码为:test

上一篇下一篇

猜你喜欢

热点阅读