ubuntu配置nginx http点播服务器
编译安装
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx
make
sudo make install
完成后启动nginx:
sudo /usr/local/nginx/sbin/nginx
测试:
google-chrome http://127.0.0.1/
这个将打开nginx默认提供的html.index页面。
如果你已经安装了apache2,那么可能显示的是apache2的欢迎页,要使用nginx,需要stop apache2服务:
sudo service apache2 stop
编译支持http vod
要支持http点播,需要在编译的时候配置加上mp4和flv的支持
$ ./configure --prefix=/usr/local/nginx --with-http_ssl_module \
--add-module=../nginx-rtmp-module --with-http_flv_module --with-http_mp4_module
如果没有with-http_flv_module和with-http_mp4_module,启动nginx的时候会报错
$ cd /usr/local/nginx
$ sudo ./sbin/nginx
nginx: [emerg] unknown directive "mp4" in /usr/local/nginx/conf/nginx.conf:62
nginx.conf增加flv和mp4
编辑:/usr/local/nginx/conf/nginx.conf
location ~ \.flv$ { #flv 支持
root /opt/video/vod;
}
location ~ \.mp4$ { #MP4 支持
root /opt/video/vod/;
mp4;
mp4_buffer_size 10m;
mp4_max_buffer_size 200m;
}
加上这段,/opt/video/vod/下的文件就可以播放了,/opt/video/vod是刚开始自己建的目录,要测试其他音视频文件,还需要copy文件到/opt/video/vod/目录下,所以还需要修改下root目录到我的samples目录。
配置nginx root目录
修改root目录为:
root /home/hui/disk4t/media-test/samples;
在浏览器中访问,返回403 Forbidden,看了下log文件/home/hui/disk4t/media-test/samples/目录访问受限。
$ cat /usr/local/nginx/logs/error.log
2021/09/24 11:21:22 [error] 6840#0: *1 "/home/hui/disk4t/media-test/samples/index.html" is forbidden (13: Permission denied), client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "127.0.0.1"
在nginx.conf文件开头增加/home/hui/disk4t/media-test/samples
目录的权限,hui是这个目录的用户名。
user hui staff;
添加后自己的电脑可以访问了,试了下samples目录下的视频文件都可以播放了,连嵌套目录都可以播放了,把cconfigure中flv和mp4的支持项去掉发现也是可以的,而rtmp协议的话只能是root目录下的文件才可以播放。
播放命令:
ffplay rtmp://192.168.31.122/vod/discont-frag.mp4
ffplay http://192.168.31.122/FLV/asian-commercials-are-weird.flv
ffplay http://192.168.31.122/avi/ffmp3_bad_rate_detection.avi
ffplay http://192.168.31.122/avi/mp-avi-demuxer-fail-184453.avi
最后的nginx.conf如下:
#user nobody;
user hui staff;
worker_processes 1;
events {
worker_connections 1024;
}
rtmp
{
server
{
listen 1935;
chunk_size 4096;
application vod {
play /home/hui/disk4t/media-test/samples;
}
application live {
live on;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root /home/hui/disk4t/media-test/samples;
location / {
index index.html index.htm;
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root nginx-rtmp-module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
配置好后播放测试
ffplay http://127.0.0.1/mov/mp4/panda.mp4
ffplay http://127.0.0.1/hls/dts-ts-source/index.m3u8
ffplay http://192.168.31.124/mov/mp4/panda.mp4
补充了解下nginx的用户和组
$ ps aux | grep nginx
root 9913 0.0 0.0 40428 960 ? Ss 11:47 0:00 nginx: master process ./sbin/nginx
hui 9914 0.0 0.0 45256 5064 ? S 11:47 0:00 nginx: worker process
ps命令看到nginx有两个进程,一个是master,一个是worker。
Nginx 主进程(master process)会以 root 权限运行,之后主进程会读取nginx.conf 文件中的user模块的配置,nginx会使用这个指定的用户启动工作进程( worker process)。
那为什么主进程需要使用root?因为只有root可以监听小于1024的端口号,通常webserver 使用80/443端口,这也就是为什么需要root来运行了。
用户和组可以查看/etc/passwd和/etc/group文件,可以看到已经定义的用户和组。
参考