Skip to content

Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring

4.4 KB
Raw Blame
# Homelable served under a subpath (__BASE__) instead of the root of the origin.
#
# Generated at image build time by Dockerfile.frontend when VITE_BASE_PATH is set:
# __BASE__ is the prefix with its trailing slash, __BASE_NO_SLASH__ without it. A
# default build keeps docker/nginx.conf and this file is never read.
#
# Like docker/nginx.conf, the result is installed as an nginx template, so
# ${BACKEND_UPSTREAM} is only rendered at container start (see Dockerfile.frontend).
#
# The static build is copied to /usr/share/nginx/html__BASE__, so every block below
# uses plain `root` semantics — `alias` plus `try_files` mis-resolves $uri.
#
# Both reverse-proxy styles work against this config:
#   - prefix forwarded intact  (proxy_pass http://homelable;)  -> the __BASE__ blocks
#   - prefix stripped          (proxy_pass http://homelable/;) -> the trailing blocks
server {
    listen 80;
    server_name _;
    root /usr/share/nginx/html;
    index index.html;
    # Relative Location headers — the port and scheme belong to whatever proxy is
    # in front, not to this server block.
    absolute_redirect off;

    # --- prefix forwarded intact ------------------------------------------------
    location = __BASE_NO_SLASH__ {
        return 301 __BASE__;
    }

    # WebSocket (must come before the API block to take priority)
    location __BASE__api/v1/status/ws/ {
        proxy_pass http://${BACKEND_UPSTREAM}/api/v1/status/ws/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location __BASE__api/ {
        proxy_pass http://${BACKEND_UPSTREAM}/api/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    # Interactive API docs — see docs/api.md. Swagger UI asks for the schema at
    # the origin root (/openapi.json), which the stripped-prefix block below
    # serves, so the page works under the prefix too.
    location __BASE__docs {
        proxy_pass http://${BACKEND_UPSTREAM}/docs;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location __BASE__redoc {
        proxy_pass http://${BACKEND_UPSTREAM}/redoc;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location = __BASE__openapi.json {
        proxy_pass http://${BACKEND_UPSTREAM}/openapi.json;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    # Legacy /ws/ path
    location __BASE__ws/ {
        proxy_pass http://${BACKEND_UPSTREAM}/ws/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }

    # SPA fallback under the prefix
    location __BASE__ {
        try_files $uri $uri/ __BASE__index.html;
    }

    # --- prefix already stripped by the front proxy -----------------------------
    # No redirect to __BASE__ here: the front proxy would strip it again and loop.
    location /api/v1/status/ws/ {
        proxy_pass http://${BACKEND_UPSTREAM};
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location /api/ {
        proxy_pass http://${BACKEND_UPSTREAM};
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location /docs {
        proxy_pass http://${BACKEND_UPSTREAM};
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location /redoc {
        proxy_pass http://${BACKEND_UPSTREAM};
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location = /openapi.json {
        proxy_pass http://${BACKEND_UPSTREAM};
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location /ws/ {
        proxy_pass http://${BACKEND_UPSTREAM};
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }

    location / {
        root /usr/share/nginx/html__BASE_NO_SLASH__;
        try_files $uri $uri/ /index.html;
    }
}