From b0e79ad207a56a618394853fa194ced107620984 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 7 Apr 2026 14:12:05 -0700 Subject: [PATCH] fix(admin): respect urlPrefix for root redirect and JS API calls (#8975) * fix(admin): respect urlPrefix for root redirect and JS API calls (#8967) Two issues when running admin UI behind a reverse proxy with -urlPrefix: 1. Visiting the prefix path without trailing slash (e.g. /s3-admin) caused a redirect to / instead of /s3-admin/ because http.StripPrefix produced an empty path that the router redirected to root. 2. Several JavaScript API calls in admin.js used hardcoded paths instead of basePath(), causing file upload, download, and preview to fail. * fix(admin): preserve query params in prefix redirect and use 302 Use http.StatusFound instead of 301 to avoid aggressive browser caching of a configuration-dependent redirect, and preserve query parameters. --- weed/admin/static/js/admin.js | 8 ++++---- weed/command/admin.go | 14 +++++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/weed/admin/static/js/admin.js b/weed/admin/static/js/admin.js index 2633cb5b2..1160790b5 100644 --- a/weed/admin/static/js/admin.js +++ b/weed/admin/static/js/admin.js @@ -1267,7 +1267,7 @@ async function submitUploadFile() { }); // Send request - xhr.open('POST', '/api/files/upload'); + xhr.open('POST', basePath('/api/files/upload')); xhr.send(formData); } catch (error) { @@ -1320,7 +1320,7 @@ function exportFileList() { // Download file function downloadFile(filePath) { // Create download link using admin API - const downloadUrl = `/api/files/download?path=${encodeURIComponent(filePath)}`; + const downloadUrl = basePath(`/api/files/download?path=${encodeURIComponent(filePath)}`); window.open(downloadUrl, '_blank'); } @@ -1786,7 +1786,7 @@ function createFileViewerContent(file, content) { if (file.mime.startsWith('image/')) { return `
- ${file.name}
`; @@ -1804,7 +1804,7 @@ function createFileViewerContent(file, content) { } else if (file.mime === 'application/pdf') { return `
-
`; diff --git a/weed/command/admin.go b/weed/command/admin.go index fca5524e1..31ddd7a2c 100644 --- a/weed/command/admin.go +++ b/weed/command/admin.go @@ -392,7 +392,19 @@ func startAdminServer(ctx context.Context, options AdminOptions, enableUI bool, addr := fmt.Sprintf(":%d", *options.port) var handler http.Handler = r if urlPrefix != "" { - handler = http.StripPrefix(urlPrefix, r) + stripped := http.StripPrefix(urlPrefix, r) + handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + // Redirect /prefix (no trailing slash) to /prefix/ + if req.URL.Path == urlPrefix { + target := urlPrefix + "/" + if req.URL.RawQuery != "" { + target += "?" + req.URL.RawQuery + } + http.Redirect(w, req, target, http.StatusFound) + return + } + stripped.ServeHTTP(w, req) + }) } server := &http.Server{ Addr: addr,