Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Nginx configuration for serving mp4 videos
#user nobody;
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log off;
sendfile_max_chunk 512k;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
server {
# add proxy caches
proxy_cache_path /tmp/mycache keys_zone=mycache:70m;
listen *:9000 ;
#listen [::]:80 ipv6only=on;
root /usr/local/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name _;
location /rtc {
#try_files $uri $uri/ /index.html;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:8088/janus;
}
location /rtcapp {
# enable thread pools for livestream
aio threads=default;
proxy_pass http://localhost:8188;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
location ~ \.flv$ {
# enable thread pool
aio threads=default;
flv;
}
location ~/.mp4 {
root /media;
mp4;
mp4_buffer_size 1M;
mp4_max_buffer_size 3M;
# enable thread bool
aio threads=default;
# enable caching for mp4 videos
proxy_cache mycache;
proxy_cache_valid 200 300s;
proxy_cache_lock on;
# enable nginx slicing
slice 1m;
proxy_cache_key $host$uri$is_args$args$slice_range;
proxy_set_header Range $slice_range;
proxy_http_version 1.1;
# Immediately forward requests to the origin if we are filling the cache
proxy_cache_lock_timeout 0s;
# Set the 'age' to a value larger than the expected fill time
proxy_cache_lock_age 200s;
proxy_cache_use_stale updating;
}
}
}
@vmauro74
Copy link

vmauro74 commented Jul 4, 2020

Hi ... I'm new in NGINX,
I might need some information regard mp4 media .. if I link my VLC Client to http:/myserver/video.mp4 I be able to play video but it use TCP session ... id like to make UDP session instead TCP ... there is some way to do that?
regards

@CSRaghunandan
Copy link
Author

CSRaghunandan commented Jul 5, 2020

Hi ... I'm new in NGINX,
I might need some information regard mp4 media .. if I link my VLC Client to http:/myserver/video.mp4 I be able to play video but it use TCP session ... id like to make UDP session instead TCP ... there is some way to do that?
regards

I don't think thats possible with nginx HTTP MP4 server. It does not provide any option for media transport layer configuration.

Any particular reason you prefer UDP instead of TCP? You might want to look at nginx-vod-module. Maybe that has that capability? I'm not sure

@student1043
Copy link

student1043 commented Oct 12, 2020

I have used this on a fresh ubuntu 18.04 nginx setup and it didn't work when I start nginx there is no response when accessing my VPS IP, and I already added a video file to /media directory, any solution?
Found a solution, I want to ask something else, how can I secure the videos so that they can only be played from my website, the solutions I found block different people's IP which means that it's blocking everyone instead of blocking people that are accessing the file directly I want them to only work on my website

@insinfo
Copy link

insinfo commented Jan 27, 2022

I currently have this configuration below for an nginx reverse proxy server that points to another application server, I would like to enable mp4 pseudo-streaming on this nginx reverse proxy server. since the videos are on the other application server the "10.0.0.72:85" does this work on a reverse proxy server? Because I heard that the mp4 module only works on the server where the videos are hosted, is this correct?

#experimenteriodasostras.com.br HTTP
# Expires map
map $sent_http_content_type $expires {
    default off;
    text/html epoch;
    text/css max;
    application/javascript max;
    ~image/ max;
}
server {
    listen 80;
    listen [::]:80;
    server_name experimenteriodasostras.com.br;
    return 301 https://www.experimenteriodasostras.com.br$request_uri;
}
server {
    listen 80;
    listen [::]:80;
    server_name www.experimenteriodasostras.com.br;
    return 301 https://www.experimenteriodasostras.com.br$request_uri;
}
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name experimenteriodasostras.com.br;
    ssl_certificate /var/www/html/config/www.experimenteriodasostras.com.br_chained.crt;
    ssl_certificate_key /var/www/html/config/www.experimenteriodasostras.com.br.key;
    return 301 https://www.experimenteriodasostras.com.br$request_uri;
}
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    #expires $expires;
    location / {
        proxy_pass http://10.0.0.72:85;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_redirect off;
    }

    ssl_certificate /var/www/html/config/www.experimenteriodasostras.com.br_chained.crt;
    ssl_certificate_key /var/www/html/config/www.experimenteriodasostras.com.br.key;
   
    client_max_body_size 64M;
    server_name www.experimenteriodasostras.com.br;
}

@CSRaghunandan
Copy link
Author

CSRaghunandan commented Feb 5, 2022

@insinfo yes, the videos that need to be served for nginx mp4 video module has to be located on the same machine where the nginx server is running.

You operate nginx as both a reverse proxy and a mp4 VOD server.

@leslietoo
Copy link

leslietoo commented May 10, 2022

@CSRaghunandan Could you please tell me what nginx modules needed for your conf file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment