Fork me on GitHub

unknown directive "mp4" in /etc/nginx/nginx.conf

unknown directive "mp4" in /etc/nginx/nginx.conf


 

1、问题背景

想通过nginx实现mp4格式视频的播放功能。

在nginx配置文件(/etc/nginx/nginx.conf)中的配置如下:

server{
   listen 801;  #监听端口
   location ~* .*\.mp4 {
        root /data/video/;   #视频保存的目录
        mp4;
        mp4_buffer_size 1m;#处理mp4初始内存大小
        mp4_max_buffer_size 50m;#处理mp4最大内存大小
   }
}

其中, /data/video/ 目录下存放 mp4格式的视频文件

 

2、问题现象

使用如下命令重启nginx,出现报错。

1)nginx重启命令

systemctl restart nginx

 

2)nginx报错信息

通过查看nginx的错误日志,可以看到报错信息:unknown directive "mp4" in /etc/nginx/nginx.conf   

错误信息翻译为: /etc/nginx/nginx.conf中的未知指令“mp4”

 

 

3、问题原因

网上找了一些资料,有说是配置文件的编码不是UTF-8导致的,但是通过如下命令,可以看到 nginx.conf 文件的编码格式就是 UTF-8。

file -i nginx.conf

 

由此可见,并非文件编码问题。

其实,从nginx的错误日志中可以看到线索,unknown directive "mp4" (未知指令mp4”),说明nginx缺少 http_mp4 模块。

 

 

4、解决方案

重新编译安装nginx,并在 ./configure 命令后追加 --with-http_mp4_module 

1)进入nginx的源文件目录

cd /usr/local/nginx-1.22.1

/usr/local/nginx-1.22.1 是个人的nginx源文件目录,此目录根据实际情况而定。

 

2)配置增加 --with-http_mp4_module 模块

./configure --prefix=/etc/nginx \
            --sbin-path=/usr/sbin/nginx \
            --modules-path=/usr/lib64/nginx/modules \
            --conf-path=/etc/nginx/nginx.conf \
            --error-log-path=/var/log/nginx/error.log \
            --pid-path=/var/run/nginx.pid \
            --lock-path=/var/run/nginx.lock \
            --user=nginx \
            --group=nginx \
            --build=CentOS \
            --http-log-path=/var/log/nginx/access.log \
            --with-http_stub_status_module\
            --add-module=/usr/local/modsecurity-nginx \
            --with-http_ssl_module \
            --with-http_mp4_module

其中,

--add-module=/usr/local/modsecurity-nginx 用于实现ModSecurity安全加固的,如果不涉及,可以去除此行;

--with-http_ssl_module 模块用于实现 http_ssl 功能,如果不涉及,可以去除此行。

 

3)编译安装 nginx

make && make install

 

4)重启 nginx 服务

systemctl restart nginx

 

5)查看nginx 状态

systemctl status nginx

 

6)查看nginx模块信息

nginx -V

增加 http_mp4 模块的前后,对比效果如下:

 

7)补充

如果想增加对“flv”格式的支持,同理,需要在nginx 的 ./configure 中增加 http_flv 模块。

--with-http_flv_module

 

 

PS:

参考博客如下:

https://blog.csdn.net/huqi666/article/details/120607615

https://blog.csdn.net/hua0721/article/details/128074879

posted @ 2023-01-06 17:07  龙凌云端  阅读(774)  评论(0编辑  收藏  举报