cd /news/developer-tools/set-base-url-for-tubearchivist-with-… · home topics developer-tools article
[ARTICLE · art-10562] src=gist.github.com ↗ pub= topic=developer-tools verified=true sentiment=· neutral

Set base URL for TubeArchivist with Nginx

This article provides configuration code for setting a custom base URL for TubeArchivist using Nginx. The setup involves a reverse proxy configuration that rewrites requests from `/multimedia/tubearchivist/` to the TubeArchivist server, along with a JavaScript filter that modifies HTML, CSS, and JS responses to replace absolute paths with the custom base URL prefix.

read2 min views26 publishedFeb 15, 2024

reverse-proxy.conf

  This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

Learn more about bidirectional Unicode characters

Show hidden characters

js_path "/etc/nginx/conf.d/";

js_import main from ta_filter.js;

js_set $body_hash main.get_hash;

server {

    location /multimedia/tubearchivist/ {

        rewrite ^/multimedia/tubearchivist(.*)$ $1 break;

        proxy_pass http://{{subnets.multimedia}}.0.14:8000;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header Host $http_host;

        proxy_set_header Accept-Encoding identity;

        proxy_redirect ~^(.*)$ /multimedia/tubearchivist$1;

        js_body_filter main.apply_base_url;

        add_trailer Body-Hash $body_hash;

    }

}

ta_filter.js

  This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

Learn more about bidirectional Unicode characters

Show hidden characters
var hash = "";

var res = "";

var buf = 0;

// Modify the response body using regular expressions

function apply_base_url(r, data, flags) {

    let requestUrl = r.uri;

    let fileExtension = getFileExtension(requestUrl)

    if (fileExtension === "" || fileExtension === "css" || fileExtension === "js") {

        // Replace URLs in API responses

        if (requestUrl.includes("/api/")) {

            data = data.replace(/("[^"\n\r\f\v]*"(?=\s*:):")(\/.*?")/g, '$1/multimedia/tubearchivist$2');

        } else {

            // Fix some hardcoded /api in JS scripts

            if (fileExtension === "js") {

                data = data.replace(/`(\/api.*)/g, '`/multimedia/tubearchivist$1');

            }

            data = data.replace(/(src|href|action)=["'](\/.*?)["']/g, '$1="/multimedia/tubearchivist$2"');

            data = data.replace(/'(\/.*)'/g, '\'/multimedia/tubearchivist$1\'');

        }

    }

    if (data.length) buf++;

    res += data; // Collect the entire response,

    if (flags.last) { //  until we get the last byte.

        try {

            hash = require('crypto').createHash('sha1').update(res).digest('base64');

            r.sendBuffer(res, flags);

            ngx.log(ngx.INFO, `FILTERED ${res.length} bytes in ${buf} buffers`);

        } catch (e) {

            ngx.log(ngx.ERR, `ERROR ${e}`);

            r.sendBuffer("", flags);

        }

    }

}

// Extract the file extension from the URL

function getFileExtension(url) {

    let match = url.match(/\.([a-z0-9]+)(?:[\?#]|$)/i);

    return match ? match[1].toLowerCase() : '';

}

function get_hash() {

    return hash;

}

export default {

    apply_base_url,

    get_hash

}
── more in #developer-tools 4 stories · sorted by recency
── more on @tubearchivist 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/set-base-url-for-tub…] indexed:0 read:2min 2024-02-15 ·