Nginx serving static media video files

1. Let’s create a simple Nginx server, this post does just that.

2. Let’s create a folder on the server with a sample video. The following uses curl to download a sample mp4 file from samples-videos.com, you can also add a video to this directory by uploading a video file from your local pc.

cd /home
mkdir my_videos
cd my_videos
sudo curl https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4 --output bunny.mp4

3. To serve the media file, add this in the Nginx config file located at /etc/nginx/sites-available/default, it should be added before all other location configs.

location /media/ {
  alias /home/my_videos/;
}

4. Verify that after step 3, the config file should look like this.

server {
  listen 80;
  server_name YOUR_SERVER_EXTERNAL_IP_ADDRESS_or_YOUR_DOMAIN_NAME;

  listen 443 ssl;

  ssl_certificate /etc/nginx/ssl/nginx.crt;
  ssl_certificate_key /etc/nginx/ssl/nginx.key;
  
  location /media/ {
    alias /home/my_videos/;
  }

  location / {
    proxy_pass "http://127.0.0.1:8080";
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_cache_bypass $http_upgrade;
  }
}

5. Restart Nginx.

sudo service nginx restart

6. Open a browser and verify that the video from step 2 can be played. Suppose your server ip address is 123.456.78.9, the video link should be.

http://123.456.78.9/media/bunny.mp4
https://123.456.78.9/media/bunny.mp4

7. To play the video from the video url with Exoplayer in Android, see this post.

Search within Codexpedia

Custom Search

Search the entire web

Custom Search