How to Configure Nginx for Direct MP4 Streaming (Step-by-Step Guide)
Learn how to compile Nginx with the MP4 module and set up a server block to stream MP4 files directly, including required commands, configuration snippets, and restart procedures, enabling seamless offline video playback without downloading.
nginx configure MP4 file playback
To enable direct MP4 streaming via Nginx, you need to compile Nginx with the --with-http_mp4_module (and optionally other modules) and then add a server block that handles .mp4 files.
Steps to compile Nginx:
$ wget https://nginx.org/download/nginx-1.20.0.tar.gz
$ mv nginx-1.20.0.tar.gz /usr/local/
$ cd /usr/local
$ tar -xf nginx-1.20.0.tar.gz
$ cd nginx-1.20.0
$ ./configure --with-http_mp4_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-file-aio --with-http_flv_module
$ cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
$ make && make installMake sure to back up existing binaries before recompiling. The crucial flag is --with-http_mp4_module which adds MP4 support.
Nginx configuration:
server {
listen 82;
server_name 127.0.0.1;
charset utf-8,gbk;
root /usr/local/nginx/html/files; # your file directory
autoindex on;
autoindex_localtime on;
location ~ \.mp4$ {
mp4; # enable MP4
mp4_buffer_size 1m;
mp4_max_buffer_size 5m;
}
}After editing /usr/local/nginx/conf/nginx.conf, restart Nginx (stop, start, or reload) using:
停止:/usr/local/nginx/sbin/nginx -s stop
启动:/usr/local/nginx/sbin/nginx
重启:/usr/local/nginx/sbin/nginx -s reloadNow you can access the MP4 file via the configured server and it will play directly in the browser.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
