Operations 3 min read

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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Configure Nginx for Direct MP4 Streaming (Step-by-Step Guide)

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 install

Make 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 reload

Now you can access the MP4 file via the configured server and it will play directly in the browser.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

StreamingNGINXServer Configurationmp4http_mp4_module
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.