Development Environment
- Ubuntu 14.04 server
- nginx-1.8.1
- nginx-rtmp-module
Installing Nginx Dependencies
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install openssl libssl-dev
Configuring and Compiling Nginx
Use the default nginx configuration and incorporate the RTMP module:
./configure --add-module=../nginx-rtmp-module-master
make
sudo make install
Testing Nginx Installlation
Navigate to the installation directory /usr/local/nginx and execute ./sbin/nginx. All subsequent commands should run from this directory as it serves as the relative path for configuration files.
Access your browser and enter localhost in the address bar. If you see the default nginx welcome page, your server installation was successful.
On-Demand Video Service Configuration
With the nginx server operational, we can now establish a video on-demand service. Open the nginx.conf file and add RTMP configuration:
worker_processes 1;
events {
worker_connections 1024;
}
rtmp { # RTMP service
server {
listen 1935; # Service port
chunk_size 4096; # Data transfer block size
application vod {
play /opt/video/vod; # Video file storage location
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
The directory /opt/video/vod has been configured as the video file storage location. Add your video files there (for example, sample.mp4).
After placing the files, restart nginx:
sudo ./sbin/nginx -s reload
Open a video player like VLC, select "Media" → "Open Network Stream", and enter the streaming URL: rtmp://localhost/vod/sample.mp4. Click play to start viewing.
Live Streaming Service Configuration
Building upon the on-demand configuration, add live streaming capabilities. Two modifications are required: first, add an application section under RTMP services (named live for streaming), and second, add two location blocks in the HTTP section.
worker_processes 1;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
chunk_size 4096;
application vod {
play /opt/video/vod;
}
application live { # First addition for live streaming
live on;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location /stat { # Second addition - statistics endpoint
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl { # Statistics stylesheet
root /usr/local/nginx/nginx-rtmp-module/;
}
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
After these changes, restart nginx and verify the configuration by accessing your server. You should see the live applictaion listed in the statistics.
To test streaming, use OBS (Open Broadcaster Software):
- Configure OBS with media source settings
- Set up the streaming output with the correct server path
- Use the streaming URL:
rtmp://localhost/live/test
Monitor the stream status at http://localhost/stat to confirm the server is receiving the broadcast. Viewers can access the stream using the URL rtmp://localhost/live/test.
Time-Shifted Playback Configuration
To enable replay of previously streamed content, configure HLS (HTTP Live Streaming) which creates time-shifted playback functionality:
worker_processes 1;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
chunk_size 4096;
application vod {
play /opt/video/vod;
}
application live {
live on;
hls on; # Enable HLS for time-shifted playback
wait_key on; # Protect video segments from artifacts
hls_path /opt/video/hls; # Segment storage location
hls_fragment 10s; # Duration of each segment
hls_playlist_length 60s; # Total replay duration (1 minute)
hls_continuous on; # Continuous mode
hls_cleanup on; # Remove excess segments
hls_nested on; # Nested mode
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/local/nginx/nginx-rtmp-module/;
}
location /live { # Additional location for HLS
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /opt/video/hls;
expires -1;
add_header Cache-Control no-cache;
}
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
Restart nginx after configuration. Since nginx needs write permissions for segment files, ensure appropriate directory permissions. The system typically runs under nobody user without a group, so grant necessary permissions to /opt/video/hls.
Once streaming begins, you'll see segment files created in the HLS directory along with an index.m3u8 playlist file. Access the stream via http://localhost/live/test/index.m3u8.
For time-shifted viewing, individual .ts segment files can be accessed directly for specific time periods.