Skip to content

Instantly share code, notes, and snippets.

@CSRaghunandan
Last active February 20, 2020 06:06
Show Gist options
  • Save CSRaghunandan/d2b65eb7249633e7979525115fd66b00 to your computer and use it in GitHub Desktop.
Save CSRaghunandan/d2b65eb7249633e7979525115fd66b00 to your computer and use it in GitHub Desktop.
nginx conf for rtmp hls
#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 {
# add proxy caches
proxy_cache_path /tmp/mycache keys_zone=mycache:70m;
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;
gzip_http_version 1.1; # turn on gzip for http 1.1 and higher
gzip_comp_level 5; # inc compresion level, and CPU usage
gzip_proxied any; # enable gzip for proxied requests (e.g. CDN)
gzip_types text/html;
gzip_types text/plain;
gzip_types text/css;
gzip_types application/javascript;
gzip_types application/json;
gzip_types application/vnd.ms-fontobject;
gzip_types application/x-font-ttf;
gzip_types font/opentype;
gzip_types image/svg+xml;
gzip_types image/x-icon;
gzip_types image/jpeg;
gzip_types video/mp4;
# 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 {
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 /hls {
# Disable cache
add_header Cache-Control no-cache;
# CORS setup
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length';
# allow CORS preflight requests
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
}
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 X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_redirect off;
}
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;
}
}
}
rtmp {
server {
listen 1935; # Listen on standard RTMP port
chunk_size 4000;
application show {
live on;
# Turn on HLS
hls on;
hls_path /mnt-hls/hls/;
hls_fragment 3;
hls_playlist_length 60;
# disable consuming the stream from nginx as rtmp
deny play all;
}
}
}
@ashwinprasadme
Copy link

#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 {
# add proxy caches
proxy_cache_path /tmp/mycache keys_zone=mycache:70m;

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;
gzip_http_version 1.1; # turn on gzip for http 1.1 and higher
gzip_comp_level 5;     # inc compresion level, and CPU usage
gzip_proxied any;      # enable gzip for proxied requests (e.g. CDN)

gzip_types text/html;
gzip_types text/plain;
gzip_types text/css;
gzip_types application/javascript;
gzip_types application/json;
gzip_types application/vnd.ms-fontobject;
gzip_types application/x-font-ttf;
gzip_types font/opentype;
gzip_types image/svg+xml;
gzip_types image/x-icon;
gzip_types image/jpeg;
gzip_types video/mp4;

# 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 {

    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 /hls {
# Disable cache
add_header Cache-Control no-cache;

    # CORS setup
    add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Expose-Headers' 'Content-Length';

    # allow CORS preflight requests
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
    }

    types {
        application/vnd.apple.mpegurl m3u8;
        video/mp2t ts;
    }
}


    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 X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    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;

    }
}

}

rtmp {
server {
listen 1935; # Listen on standard RTMP port
chunk_size 4000;

    application show {
        live on;
        # Turn on HLS
        hls on;
        hls_path /mnt-hls/hls/;
        hls_fragment 3;
        hls_playlist_length 60;
        # disable consuming the stream from nginx as rtmp
        deny play all;
    }
}

}

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