ThisCalculator

Everyday

How to Stream Video with Nginx on a VPS

Updated · Standard formula, shown below

How to Stream Video with Nginx on a VPS

Setting up Nginx on a virtual private server (VPS) lets you deliver video files over HTTP so viewers can play or download them through a browser or media player. This guide covers a basic on-demand stream; it does not turn Nginx into a live-streaming encoder or automatically convert video formats. You’ll need a server with administrative access, a domain or server IP, and a video file encoded in a format viewers can play.

What You’ll Need

  • A Linux VPS with a supported distribution and sudo access.
  • A domain pointed to the VPS, if you want a memorable address and HTTPS. You can test with the server IP first.
  • Nginx and a firewall configuration that permits web traffic on ports 80 and 443.
  • A video file, ideally MP4 using H.264 video and AAC audio for broad browser compatibility.
  • An SSH client and basic familiarity with terminal commands.

Step-by-Step

  1. Connect to the VPS. From your computer, open a terminal and sign in with the account provided by your VPS host: ssh username@SERVER_IP. Replace both values with your account name and server address. Use an account with sudo privileges; avoid logging in as root for routine administration.
  2. Install Nginx. On Ubuntu or Debian, update package information and install the web server:
    sudo apt update
    sudo apt install nginx
    For another distribution, use its documented package manager and Nginx package. Check that Nginx is running with sudo systemctl status nginx. If it is not active, try sudo systemctl enable --now nginx.
  3. Allow web traffic. If a host firewall is enabled, allow HTTP and HTTPS using its supported rules. With UFW, for example, run sudo ufw allow 'Nginx Full', then check sudo ufw status. Also check the VPS provider’s network firewall or security-group settings; a server firewall rule alone may not open the provider’s network.
  4. Create a video directory. Keep media separate from Nginx’s configuration files. Create a directory under the standard web root:
    sudo mkdir -p /var/www/html/videos
    Set readable permissions so Nginx can serve files without making them writable by everyone:
    sudo chown -R www-data:www-data /var/www/html/videos
    sudo chmod 755 /var/www/html/videos
    On distributions where Nginx runs as a different account, identify it with ps -eo user,comm | grep nginx and adjust ownership if needed.
  5. Upload the video. From your local computer, use a secure copy command such as scp movie.mp4 username@SERVER_IP:/tmp/. Then move it into the serving directory on the VPS:
    sudo mv /tmp/movie.mp4 /var/www/html/videos/
    sudo chown www-data:www-data /var/www/html/videos/movie.mp4
    Use the correct Nginx account if it differs. Confirm the file name and capitalization, which matter in URLs.
  6. Test file delivery. Open http://SERVER_IP/videos/movie.mp4 in a browser, replacing the IP and file name. Depending on the browser, the video may play or download. You can also test the response from a terminal with curl -I http://SERVER_IP/videos/movie.mp4; a successful response normally includes status 200 and a video content type. A web server can deliver the file, but playback still depends on the codec and player.
  7. Set up a domain and HTTPS. Create or update your domain’s DNS A record to point to the VPS IP, and wait for the change to become available. Nginx’s default site may respond by IP, but for a domain it is better to configure a server block with that domain as its name and the intended document root. Install a certificate using a trusted certificate provider and its current Nginx instructions, then test both the HTTPS page and video URL. Do not send viewers login details or sensitive content over plain HTTP.
  8. Embed the video if desired. Create an HTML page in the web root and use a video element with a source URL:
    <video controls preload="metadata" width="800">
      <source src="/videos/movie.mp4" type="video/mp4">
      Your browser does not support embedded video.
    </video>
    Adjust the width to suit your page. The controls let viewers play, pause, and seek. Ensure the page and video use HTTPS together to avoid browser mixed-content warnings.
  9. Check range requests and capacity. Browsers commonly request only portions of a video to seek without downloading it all. Nginx normally supports byte-range requests for static files; inspect a response with curl -I -H 'Range: bytes=0-1' https://your-domain.example/videos/movie.mp4 and look for a partial-content response. Monitor VPS bandwidth, disk space, and load as viewers increase. A small VPS or limited connection may not handle many simultaneous viewers.

Common Mistakes

  • Using an incompatible codec: An MP4 extension does not guarantee browser compatibility. Check the actual video and audio codecs; re-encode a permitted source to H.264/AAC if needed.
  • Opening the wrong firewall: Traffic can be blocked by the operating system firewall, the VPS provider’s firewall, or both.
  • Making files world-writable: Nginx needs read access, not permission for every user to change the media. Avoid broad permissions such as chmod 777.
  • Expecting Nginx to create a live broadcast: Static-file delivery is on-demand streaming. Live ingest, transcoding, adaptive bitrate output, and chat or broadcast controls require additional software and configuration.
  • Ignoring bandwidth: Each viewer consumes outgoing data. A popular or high-bitrate file can exceed the VPS connection’s practical capacity.

Tips

  • Use clear file names without spaces, and keep a backup of the original media.
  • Use HTTPS, particularly when embedding video on a public site.
  • Choose a bitrate and resolution appropriate to the connection and expected audience. Large files take longer to start and use more bandwidth.
  • For a growing audience, consider a content delivery network or dedicated video-hosting service rather than relying on one VPS to serve every viewer.
  • Protect private videos with an appropriate access-control design; an unlisted URL alone is not reliable access control.

Troubleshooting

  • “Connection refused” or a timeout: Check that Nginx is active, the VPS IP is correct, and ports 80 or 443 are allowed in both firewall layers. Review sudo systemctl status nginx.
  • 404 Not Found: Check the URL, file name capitalization, and actual location with ls -l /var/www/html/videos/. Confirm the active Nginx site uses the expected document root.
  • 403 Forbidden: Check directory and file read permissions, plus permissions on every parent directory. Review Nginx’s error log with sudo tail -n 50 /var/log/nginx/error.log.
  • The file downloads but will not play: Test in another current browser and verify the codecs, not just the extension. Re-encode to a widely supported format if necessary.
  • Playback starts but seeking fails: Confirm the server responds to a range request with partial content. Check for a proxy or other configuration in front of Nginx that might alter range handling.
  • HTTPS certificate warning: Confirm the DNS record points to this server and the certificate covers the exact domain. Check Nginx configuration with sudo nginx -t before reloading it.

Frequently Asked Questions

Does Nginx support video streaming?
Yes. It can serve video files over HTTP, and browsers can request file ranges for playback and seeking. Nginx does not by itself encode or package video.

Can I stream live video with this setup?
No. These steps serve prerecorded files. Live streaming needs an ingest and encoding workflow, and often a separate streaming server or service.

Do I need a domain name?
No. You can test using the VPS IP address. A domain is more convenient for viewers and is generally needed to obtain a standard domain-based HTTPS certificate.

Which video format should I use?
MP4 with H.264 video and AAC audio is a practical starting point for broad browser support, but playback varies by browser, device, and codec support.

Can many people watch at once?
That depends on the VPS’s outgoing bandwidth, file bitrate, and other resource limits. Monitor traffic and performance, and move delivery to a CDN or video service if capacity is insufficient.

Is a public video URL private?
No. Anyone who obtains an unrestricted URL may be able to view the file. Use proper authentication or a service designed for protected delivery when access must be limited.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.