{"slug": "set-base-url-for-tubearchivist-with-nginx", "title": "Set base URL for TubeArchivist with Nginx", "summary": "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.", "body_md": "reverse-proxy.conf\n\n      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.\n      \nLearn more about bidirectional Unicode characters\n\n \n    Show hidden characters\n\njs_path \"/etc/nginx/conf.d/\";\n\njs_import main from ta_filter.js;\n\njs_set $body_hash main.get_hash;\n\nserver {\n\n    location /multimedia/tubearchivist/ {\n\n        rewrite ^/multimedia/tubearchivist(.*)$ $1 break;\n\n        proxy_pass http://{{subnets.multimedia}}.0.14:8000;\n\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n\n        proxy_set_header X-Forwarded-Proto $scheme;\n\n        proxy_set_header X-Real-IP $remote_addr;\n\n        proxy_set_header Host $http_host;\n\n        proxy_set_header Accept-Encoding identity;\n\n        proxy_redirect ~^(.*)$ /multimedia/tubearchivist$1;\n\n        js_body_filter main.apply_base_url;\n\n        add_trailer Body-Hash $body_hash;\n\n    }\n\n}\n\nta_filter.js\n\n      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.\n      \nLearn more about bidirectional Unicode characters\n\n \n    Show hidden characters\n\nvar hash = \"\";\n\nvar res = \"\";\n\nvar buf = 0;\n\n// Modify the response body using regular expressions\n\nfunction apply_base_url(r, data, flags) {\n\n    let requestUrl = r.uri;\n\n    let fileExtension = getFileExtension(requestUrl)\n\n    if (fileExtension === \"\" || fileExtension === \"css\" || fileExtension === \"js\") {\n\n        // Replace URLs in API responses\n\n        if (requestUrl.includes(\"/api/\")) {\n\n            data = data.replace(/(\"[^\"\\n\\r\\f\\v]*\"(?=\\s*:):\")(\\/.*?\")/g, '$1/multimedia/tubearchivist$2');\n\n        } else {\n\n            // Fix some hardcoded /api in JS scripts\n\n            if (fileExtension === \"js\") {\n\n                data = data.replace(/`(\\/api.*)/g, '`/multimedia/tubearchivist$1');\n\n            }\n\n            data = data.replace(/(src|href|action)=[\"'](\\/.*?)[\"']/g, '$1=\"/multimedia/tubearchivist$2\"');\n\n            data = data.replace(/'(\\/.*)'/g, '\\'/multimedia/tubearchivist$1\\'');\n\n        }\n\n    }\n\n    if (data.length) buf++;\n\n    res += data; // Collect the entire response,\n\n    if (flags.last) { //  until we get the last byte.\n\n        try {\n\n            hash = require('crypto').createHash('sha1').update(res).digest('base64');\n\n            r.sendBuffer(res, flags);\n\n            ngx.log(ngx.INFO, `FILTERED ${res.length} bytes in ${buf} buffers`);\n\n        } catch (e) {\n\n            ngx.log(ngx.ERR, `ERROR ${e}`);\n\n            r.sendBuffer(\"\", flags);\n\n        }\n\n    }\n\n}\n\n// Extract the file extension from the URL\n\nfunction getFileExtension(url) {\n\n    let match = url.match(/\\.([a-z0-9]+)(?:[\\?#]|$)/i);\n\n    return match ? match[1].toLowerCase() : '';\n\n}\n\nfunction get_hash() {\n\n    return hash;\n\n}\n\nexport default {\n\n    apply_base_url,\n\n    get_hash\n\n}", "url": "https://wpnews.pro/news/set-base-url-for-tubearchivist-with-nginx", "canonical_source": "https://gist.github.com/DarkFighterLuke/4561b6bfbf83720493dc59171c58ac36", "published_at": "2024-02-15 22:07:54+00:00", "updated_at": "2026-05-23 02:33:45.329049+00:00", "lang": "en", "topics": ["developer-tools", "open-source"], "entities": ["TubeArchivist", "Nginx"], "alternates": {"html": "https://wpnews.pro/news/set-base-url-for-tubearchivist-with-nginx", "markdown": "https://wpnews.pro/news/set-base-url-for-tubearchivist-with-nginx.md", "text": "https://wpnews.pro/news/set-base-url-for-tubearchivist-with-nginx.txt", "jsonld": "https://wpnews.pro/news/set-base-url-for-tubearchivist-with-nginx.jsonld"}}