From b213d1e73cbd53e5714f798d4eeb620e2125cabe Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:40:26 -0600 Subject: [PATCH] Nginx listen and cache header fixes (#25092) * only add the ipv6 listener for wildcard listen addresses With IPv6 enabled, nginx also listened on [::] for both ports regardless of the configured listen address, so an internal port bound to 127.0.0.1 or [::1] was still reachable on every IPv6 address, and a [::]:port value rendered a duplicate listen that kept nginx from starting. The [::] listener is now added only for a bare port, *:port or 0.0.0.0:port, and a specific address binds only that address. * mark authorized media responses private in nginx --- .../main/rootfs/usr/local/nginx/conf/nginx.conf | 4 +++- .../rootfs/usr/local/nginx/get_nginx_settings.py | 9 ++++++++- .../usr/local/nginx/templates/listen.gotmpl | 12 ++++++------ docs/docs/configuration/advanced/system.md | 2 +- frigate/test/test_network_config.py | 15 +++++++++++++++ frigate/util/config.py | 12 ++++++++++++ 6 files changed, 45 insertions(+), 9 deletions(-) diff --git a/docker/main/rootfs/usr/local/nginx/conf/nginx.conf b/docker/main/rootfs/usr/local/nginx/conf/nginx.conf index 1c6b9f2f47..768e0c71f8 100644 --- a/docker/main/rootfs/usr/local/nginx/conf/nginx.conf +++ b/docker/main/rootfs/usr/local/nginx/conf/nginx.conf @@ -183,7 +183,7 @@ http { } expires 7d; - add_header Cache-Control "public"; + add_header Cache-Control "private"; autoindex on; root /media/frigate; @@ -205,6 +205,7 @@ http { video/mp4 mp4; } + add_header Cache-Control "private"; autoindex on; autoindex_format json; root /media/frigate; @@ -216,6 +217,7 @@ http { video/mp4 mp4; } + add_header Cache-Control "private"; autoindex on; autoindex_format json; root /media/frigate; diff --git a/docker/main/rootfs/usr/local/nginx/get_nginx_settings.py b/docker/main/rootfs/usr/local/nginx/get_nginx_settings.py index d2abcd693a..920843ac48 100644 --- a/docker/main/rootfs/usr/local/nginx/get_nginx_settings.py +++ b/docker/main/rootfs/usr/local/nginx/get_nginx_settings.py @@ -8,7 +8,11 @@ from typing import Any from ruamel.yaml import YAML sys.path.insert(0, "/opt/frigate") -from frigate.util.config import find_config_file, parse_listen_address +from frigate.util.config import ( + find_config_file, + ipv6_listen_address, + parse_listen_address, +) sys.path.remove("/opt/frigate") @@ -51,6 +55,9 @@ for key, default in (("internal", 5000), ("external", 8971)): listen_config[key] = value listen_config[f"{key}_port"] = port + listen_config[f"{key}_ipv6"] = ( + ipv6_listen_address(value, port) if ipv6_config["enabled"] else None + ) if listen_config["internal_port"] == listen_config["external_port"]: print( diff --git a/docker/main/rootfs/usr/local/nginx/templates/listen.gotmpl b/docker/main/rootfs/usr/local/nginx/templates/listen.gotmpl index 5141227d5f..121c950110 100644 --- a/docker/main/rootfs/usr/local/nginx/templates/listen.gotmpl +++ b/docker/main/rootfs/usr/local/nginx/templates/listen.gotmpl @@ -1,12 +1,12 @@ -# Internal (IPv4 always; IPv6 optional) +# Internal; a wildcard address also listens on [::] when IPv6 is enabled listen {{ .listen.internal }}; -{{ if .ipv6.enabled }}listen [::]:{{ .listen.internal_port }};{{ end }} +{{ if .listen.internal_ipv6 }}listen {{ .listen.internal_ipv6 }};{{ end }} # intended for external traffic, protected by auth {{ if .tls.enabled }} - # external HTTPS (IPv4 always; IPv6 optional) + # external HTTPS listen {{ .listen.external }} ssl; - {{ if .ipv6.enabled }}listen [::]:{{ .listen.external_port }} ssl;{{ end }} + {{ if .listen.external_ipv6 }}listen {{ .listen.external_ipv6 }} ssl;{{ end }} ssl_certificate {{ .tls.cert_path }}/fullchain.pem; ssl_certificate_key {{ .tls.cert_path }}/privkey.pem; @@ -30,7 +30,7 @@ listen {{ .listen.internal }}; root /etc/letsencrypt/www; } {{ else }} - # (No tls) default to HTTP (IPv4 always; IPv6 optional) + # (No tls) default to HTTP listen {{ .listen.external }}; - {{ if .ipv6.enabled }}listen [::]:{{ .listen.external_port }};{{ end }} + {{ if .listen.external_ipv6 }}listen {{ .listen.external_ipv6 }};{{ end }} {{ end }} diff --git a/docs/docs/configuration/advanced/system.md b/docs/docs/configuration/advanced/system.md index f9d0971215..ed78f3b510 100644 --- a/docs/docs/configuration/advanced/system.md +++ b/docs/docs/configuration/advanced/system.md @@ -287,7 +287,7 @@ Frigate exposes a few networking options. IPv6 and the listen ports are set in t ### Enabling IPv6 -By default Frigate listens on IPv4 only. To also listen on IPv6 (on port `5000`, and on `8971` when TLS is configured), enable it in the `networking` configuration. +By default Frigate listens on IPv4 only. To also listen on IPv6 (on port `5000`, and on `8971` when TLS is configured), enable it in the `networking` configuration. A listen port bound to a specific address stays on that address only. diff --git a/frigate/test/test_network_config.py b/frigate/test/test_network_config.py index 199421371f..3a1fe6e0aa 100644 --- a/frigate/test/test_network_config.py +++ b/frigate/test/test_network_config.py @@ -5,6 +5,7 @@ import unittest from pydantic import ValidationError from frigate.config.network import ListenConfig +from frigate.util.config import ipv6_listen_address, parse_listen_address class TestListenConfig(unittest.TestCase): @@ -104,5 +105,19 @@ class TestListenConfig(unittest.TestCase): ListenConfig(internal=value) +class TestIPv6ListenAddress(unittest.TestCase): + def test_wildcard_adds_ipv6_wildcard(self): + for raw in (5000, "*:5000", "0.0.0.0:5000"): + with self.subTest(raw=raw): + value, port = parse_listen_address(raw) + self.assertEqual(ipv6_listen_address(value, port), "[::]:5000") + + def test_specific_address_adds_nothing(self): + for raw in ("127.0.0.1:5000", "localhost:5000", "[::1]:5000", "[::]:5000"): + with self.subTest(raw=raw): + value, port = parse_listen_address(raw) + self.assertIsNone(ipv6_listen_address(value, port)) + + if __name__ == "__main__": unittest.main(verbosity=2) diff --git a/frigate/util/config.py b/frigate/util/config.py index 7db9e728a7..3cbd8fef88 100644 --- a/frigate/util/config.py +++ b/frigate/util/config.py @@ -195,6 +195,18 @@ def parse_listen_address(value: int | str) -> tuple[int | str, int]: return f"{host}:{port}", port +def ipv6_listen_address(value: int | str, port: int) -> str | None: + """Return the IPv6 wildcard address to add for a parsed listen value. + + Only an IPv4 wildcard gains the extra IPv6 listener. A specific address + binds only that address, and nginx would let a [::] listener override it. + """ + if isinstance(value, int) or value.startswith(("*:", "0.0.0.0:")): + return f"[::]:{port}" + + return None + + def find_config_file() -> str: """Return the path of the config file to use.