From 7722e3601253d773814418b7fb406341588c89db Mon Sep 17 00:00:00 2001 From: Hizkia Felix Winata Date: Thu, 27 Jan 2022 21:35:30 +0800 Subject: [PATCH] feat(nginx): fix build and run scripts --- scripts/nginx/README.md | 28 +++++++++++++++++++---- scripts/nginx/build.sh | 8 ++----- scripts/nginx/restream.conf | 44 ++++++++++++++++++++++++------------- scripts/nginx/run.sh | 11 ++++++++++ 4 files changed, 66 insertions(+), 25 deletions(-) create mode 100755 scripts/nginx/run.sh diff --git a/scripts/nginx/README.md b/scripts/nginx/README.md index 2bcf6b7..20f6077 100644 --- a/scripts/nginx/README.md +++ b/scripts/nginx/README.md @@ -1,9 +1,29 @@ # nginx caching reverse proxy config -Check the `restream.conf` file for a good-enough caching reverse proxy setup -with nginx. It should just work with the default nginx that ships with most -distros. It can be copied straight to `/etc/nginx/nginx.conf` and it _should_ -just work. +This is a good-enough example configuration for a caching reverse proxy for HLS +live streams. + +## See it in action + +```bash +# Download and compile a local version of nginx +$ ./build.sh + +# Run the example configuration +$ ./run.sh +``` + +Once running, check out [http://localhost:8080](http://localhost:8080). By The +example config proxies to `restream.dev`, so you should see a blank player page. +Feel free to check out the `restream.conf` file and tune it to your liking. + +The build script doesn't litter your system with random junk. When you're done +you can just wipe the `./nginx` folder. + +## Merging with your existing nginx + +If you don't wanna bother merging the config, you can just use your existing +nginx to reverse-proxy to this one. If you want to merge it with an existing nginx config, you can copy these directives to the `http` block: diff --git a/scripts/nginx/build.sh b/scripts/nginx/build.sh index 3a92094..4445d57 100755 --- a/scripts/nginx/build.sh +++ b/scripts/nginx/build.sh @@ -2,12 +2,8 @@ set -e -tmpdir="$(mktemp -d)" -prefix="$1" - -if [ -z "$prefix" ]; then - prefix="$(pwd)/nginx" -fi +tmpdir="$(mktemp -d -p .)" +prefix="$(pwd)/nginx" mkdir -p "$prefix" diff --git a/scripts/nginx/restream.conf b/scripts/nginx/restream.conf index 0d0b211..78866af 100644 --- a/scripts/nginx/restream.conf +++ b/scripts/nginx/restream.conf @@ -3,6 +3,8 @@ worker_processes auto; events { worker_connections 128; } +error_log /dev/stderr; + http { # Basic settings sendfile on; @@ -11,17 +13,25 @@ http { server_tokens off; keepalive_timeout 300s; types_hash_max_size 2048; + include ./nginx/conf/mime.types; default_type application/octet-stream; + # Logging settings + access_log /dev/stdout; + # Set DNS resolver resolver 1.1.1.1 8.8.8.8 valid=300s ipv6=off; resolver_timeout 10s; # TODO: Update cache path to your cache directory proxy_cache_path - /var/cache/nginx/restream + # cache directory (e.g. /var/cache/nginx/proxy_cache) + ./cache use_temp_path=off + # the name of the cache, if you change this + # you have to update the proxy_cache directive below keys_zone=restream_cache:10m + # maximum size of the cache, feel free to tune it max_size=20g inactive=10m; @@ -29,18 +39,22 @@ http { # TODO: Add your upstream here. For HTTP-only # upstreams, omit the :443, and change the # https:// to http:// in the proxy_pass directive. - server stream.example.com:443; + server restream.dev:443; } - # HTTPS configuration + # Server configuration server { - listen 443 ssl http2; - listen [::]:443 ssl http2; + # Example SSL config + # listen 443 ssl; + # listen [::]:443 ssl; + # server_name example.com; + # ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; + # ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; - # TODO: Add your server name here - server_name example.com; - ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; - ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; + # Or for non-SSL + listen 8080; + listen [::]:8080; + server_name _; # TODO: change with your domain name proxy_redirect off; proxy_http_version 1.1; @@ -74,11 +88,11 @@ http { } # Optional: redirect all requests to https - server { - listen 80; - listen [::]:80; - server_name _; + # server { + # listen 80; + # listen [::]:80; + # server_name _; - return 307 https://$server_name$request_uri; - } + # return 307 https://$server_name$request_uri; + # } } diff --git a/scripts/nginx/run.sh b/scripts/nginx/run.sh new file mode 100755 index 0000000..98a8795 --- /dev/null +++ b/scripts/nginx/run.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +script_dir=$(readlink -f $(dirname $0)) + +echo "Starting nginx" +echo "Press Ctrl+C to stop" +mkdir -p "$script_dir/nginx/cache" +"$script_dir/nginx/sbin/nginx" \ + -c "$script_dir/restream.conf" \ + -g "daemon off;" \ + -e /dev/stderr