Skip to content

A private YouTube client: self-hosted web version (one docker compose) and native apps for Android, Windows, macOS, Linux and iPad/iPhone. No Google account, no tracking. Not affiliated with YouTube or Google.

3.7 KB
Raw Blame
# TubeHaus behind nginx.
# Include this file from nginx's http context (for example, via sites-enabled).
# Replace the hostname, TLS certificate paths and upstream addresses for your setup.
# This example assumes nginx can reach the TubeHaus server (which also asks YouTube
# itself and passes the video streams) on the same host. Keep TubeHaus's own authentication enabled or put an auth gate
# in front of the entire site before exposing it publicly.

map $http_accept $tubehaus_accepts_html {
    default 0;
    "~*text/html" 1;
}

server {
    listen 443 ssl http2;
    server_name tube.example.com;

    ssl_certificate     /etc/letsencrypt/live/tube.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/tube.example.com/privkey.pem;

    # Requests that are not an app page (see below) go to the TubeHaus server.
    error_page 418 = @tubehaus;

    # Privacy and hardening; also keep private instances out of search engines.
    add_header Referrer-Policy same-origin always;
    add_header X-Content-Type-Options nosniff always;
    add_header X-Frame-Options SAMEORIGIN always;
    add_header Permissions-Policy "interest-cohort=(), browsing-topics=()" always;

    location = /robots.txt {
        default_type text/plain;
        return 200 "User-agent: *\nDisallow: /\n";
    }

    # App assets.
    location ^~ /y/ {
        root /srv/tubehaus/web;
        add_header Cache-Control "no-cache";
        add_header Referrer-Policy same-origin always;
        add_header X-Content-Type-Options nosniff always;
        add_header X-Frame-Options SAMEORIGIN always;
        add_header Permissions-Policy "interest-cohort=(), browsing-topics=()" always;
        try_files $uri =404;
    }

    # Page navigations go to the TubeHaus app only for HTML GET/HEAD requests.
    # Anything else on those paths goes to the TubeHaus server.
    location ~ ^/(?:$|watch$|results$|shorts(?:/.*)?|channel/.*|c/.*|user/.*|@[^/]+(?:/.*)?|playlist$|feed/(?!channel/|playlist/|private(?:/|$)|webhook/).*|settings$|hashtag/.*)$ {
        if ($request_method !~ ^(GET|HEAD)$) { return 418; }
        if ($tubehaus_accepts_html = 0) { return 418; }
        if ($arg_classic != "") { return 418; }

        root /srv/tubehaus/web;
        add_header Cache-Control "no-cache";
        add_header Referrer-Policy same-origin always;
        add_header X-Content-Type-Options nosniff always;
        add_header X-Frame-Options SAMEORIGIN always;
        add_header Permissions-Policy "interest-cohort=(), browsing-topics=()" always;
        try_files /index.html =404;
    }

    # Everything else: the TubeHaus server (its API, YouTube's answers, the video streams, captions and
    # pictures). No buffering: streams and long polls pass as they come.
    location / {
        proxy_pass http://127.0.0.1:3010;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;
        proxy_request_buffering off;
        proxy_read_timeout 1h;
        proxy_send_timeout 1h;
        proxy_connect_timeout 60s;
        proxy_redirect off;
    }

    location @tubehaus {
        proxy_pass http://127.0.0.1:3010;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;
        proxy_request_buffering off;
        proxy_read_timeout 1h;
        proxy_send_timeout 1h;
        proxy_connect_timeout 60s;
        proxy_redirect off;
    }
}