fix(docker): proxy nginx to gunicorn over TCP instead of unix socket

nginx workers are dropped to the `romm` user while gunicorn creates
/tmp/gunicorn.sock as root. This only worked because gunicorn's default
umask leaves the socket world-writable; on some hosts (e.g. OMV) the
worker cannot connect() and every /api request 502s with
"connect() to unix:/tmp/gunicorn.sock failed (13: Permission denied)".

Proxy over loopback TCP (127.0.0.1:5000), which gunicorn already binds,
so there is no socket file owner/mode to mismatch. Readiness now polls
the TCP port instead of waiting for the socket file.

Fixes #3809

Generated-By: PostHog Code
Task-Id: 3cb72cda-eb77-4bc0-97ab-cde365a031c9
This commit is contained in:
Georges-Antoine Assi
2026-07-18 14:44:08 -04:00
parent 97d9289c39
commit 0ca1eb5529
2 changed files with 16 additions and 14 deletions

View File

@@ -86,28 +86,31 @@ run_startup() {
fi
}
wait_for_gunicorn_socket() {
debug_log "Waiting for gunicorn socket file..."
wait_for_gunicorn() {
local host="127.0.0.1"
local port="${DEV_PORT:-5000}"
debug_log "Waiting for gunicorn on ${host}:${port}..."
local wait_seconds=${WEB_SERVER_GUNICORN_WAIT_SECONDS:=30}
local retries=$((wait_seconds * 2))
while [[ ! -S /tmp/gunicorn.sock && retries -gt 0 ]]; do
# Temporarily disable errexit while polling the TCP port.
set +o errexit
while ((retries > 0)); do
if (echo >/dev/tcp/"${host}"/"${port}") 2>/dev/null; then
set -o errexit
debug_log "Gunicorn is ready and accepting connections"
return 0
fi
sleep 0.5
((retries--))
done
set -o errexit
if [[ -S /tmp/gunicorn.sock ]]; then
debug_log "Gunicorn socket file found"
else
warn_log "Gunicorn socket file not found after waiting ${wait_seconds}s!"
fi
warn_log "Gunicorn did not become ready after waiting ${wait_seconds}s!"
}
# Runs our main process and creates a corresponding PID file
start_bin_gunicorn() {
# cleanup potentially leftover socket
rm /tmp/gunicorn.sock -f
# Wire LOGLEVEL into Gunicorn's logging config so it respects the env var.
local gunicorn_level="${LOGLEVEL^^}"
local gunicorn_log_config="/tmp/gunicorn/logging.conf"
@@ -126,7 +129,6 @@ start_bin_gunicorn() {
otel_prefix wrap api
"${wrap[@]}" gunicorn \
--bind=0.0.0.0:"${DEV_PORT:-5000}" \
--bind=unix:/tmp/gunicorn.sock \
--pid=/tmp/gunicorn.pid \
--forwarded-allow-ips="*" \
--worker-class uvicorn_worker.UvicornWorker \
@@ -143,7 +145,7 @@ start_bin_gunicorn() {
# Commands to start nginx (handling PID creation internally)
start_bin_nginx() {
wait_for_gunicorn_socket
wait_for_gunicorn
info_log "Starting nginx"
if [[ ${EUID} -ne 0 ]]; then

View File

@@ -81,7 +81,7 @@ http {
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
upstream wsgi_server {
server unix:/tmp/gunicorn.sock;
server 127.0.0.1:5000;
}
include /etc/nginx/conf.d/*.conf;