添加一个 video.conf
配置文件,用于放 mp4 文件 Nginx 配置(假设视频放在 /example/video/ 目录下)
# 编辑配置文件
vim /usr/local/nginx/conf/video.conf
# 配置文件内容
location /video/ {alias /example/video/;mp4;mp4_buffer_size 4m;mp4_max_buffer_size 20m;sendfile on;tcp_nopush on;# 启用范围请求add_header Accept-Ranges bytes;# 缓存控制expires 30d;add_header Cache-Control "public, max-age=604800";# 限速limit_rate_after 10m;limit_rate 2m;
}
配置完成后,在主配置文件中引用该配置
server {listen 80;server_name your-domain.com;location /app1 {# 其他配置...}location /app2 {# 其他配置...}include /usr/local/nginx/conf/video.conf
}
重启 nginx
systemctl restart nginx
如果报如下信息测试失败,那可能是因为你的 nginx 没有安装 mp4 模块,该模块不会默认安装
nginx: [emerg] unknown directive "mp4" in /usr/local/nginx/conf/video.conf:5
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
由于我的 nginx 是编译安装的,重新编译 nginx,查看目前的编译参数
/usr/local/nginx/sbin/nginx -V
如果有之前已经编译过的模块,重新编译时也要记得加上,例如下面的 ssl 模块
./configure --with-http_ssl_module --with-http_mp4_module
make
make install
编译完成后,重新启动 nginx 就可以通过 your-domain.com/video/video.mp4 访问了。
参考资料
- Module ngx_http_mp4_module
- Serve Static Content | NGINX Documentation
- Module ngx_http_core_module