自建NSTBrowser Docker容器后无可用API端点的问题求助
自建NSTBrowser Docker容器后无可用API端点的问题求助
最近我在本地使用app.nstbrowser.io的客户端时一切正常,对应的核心代码片段如下:
import { Proxy } from '@devflexi/proxies'; import { NstBrowserV2 } from 'nstbrowser-sdk-node'; import { chromium } from 'playwright'; import type { Browser, BrowserContext, Page } from 'playwright-core'; import { Protocol } from 'playwright-core/types/protocol.js'; import { NST_BROWSER_API_KEY, NST_BROWSER_URL } from '../../config.js'; import { logger } from '../../logger.js'; const usedProfileIds = new Set<string>(); export class ManagedBrowser { public readonly id: string; private browser!: Browser; private context!: BrowserContext; private page!: Page; private busy = false; private connected = false; private client: NstBrowserV2; private profileId!: string; constructor(id: string) { this.id = id; this.client = new NstBrowserV2(NST_BROWSER_API_KEY, { timeout: 60000, apiAddress: `http://${NST_BROWSER_URL}/api/v2`, }); } public isBusy(): boolean { return this.busy; } public markBusy(): void { this.busy = true; } public markFree(): void { this.busy = false; } public isAlive(): boolean { try { return this.connected && this.browser.contexts().length > 0 && !this.page.isClosed(); } catch { return false; } } public getPage(): Page { return this.page; } public getContext(): BrowserContext { return this.context; } public getBrowser(): Browser { return this.browser; } public async connect(): Promise<void> { const browserProfiles = await this.client.profiles().getProfiles(); const profile = browserProfiles.data.docs.find((profile) => !usedProfileIds.has(profile.profileId)); if (!profile) { throw new Error('No available profile found'); } this.profileId = profile.profileId; const browser = await this.client.cdpEndpoints().connectBrowser(profile.profileId, { headless: false, autoClose: true, clearCacheOnClose: true, }); this.browser = await chromium.connectOverCDP(browser.data.webSocketDebuggerUrl, { timeout: 15000 }); const contexts = this.browser.contexts(); this.context = contexts.length > 0 ? contexts[0] : await this.browser.newContext(); const existingPages = this.context.pages(); this.page = existingPages.length > 0 ? existingPages[0] : await this.context.newPage(); this.connected = true; } public async setProxy(proxy: Proxy): Promise<void> { const cdpClient = await this.page.context().newCDPSession(this.page); await cdpClient.send('Network.updateContextProxy' as unknown as keyof Protocol.CommandParameters, { proxyServer: `http://${proxy.username}:${proxy.password}@${proxy.host}:${proxy.port.toString()}`, }); } public async close(): Promise<void> { try { await this.client.browsers().stopBrowser(this.profileId); logger.debug(`[ManagedBrowser] Closed ${this.id}`); } catch (error) { logger.error(`[ManagedBrowser] - Error closing ${this.id}:`, error); } finally { usedProfileIds.delete(this.profileId); } } }
本地测试时我设置NST_BROWSER_URL=localhost:8848,代码能正常连接并调用API。
但当我准备部署到生产环境,尝试用官方提供的Docker命令自建NSTBrowser服务时:
docker run -it -e TOKEN=xxx -e PORT=8848 -p 8848:8848 --name nstbrowserless nstbrowser/browserless:latest
容器启动后,我发现所有API端点都无法访问——不管是在宿主机用curl请求,还是进入容器内部执行请求,所有尝试的端点都返回404错误。我已经确认端口映射没有问题,因为容器内测试也同样失败。
有没有朋友遇到过类似的问题?或者知道该如何排查和解决这个API端点不可用的问题?
内容来源于stack exchange
相关产品推荐
相关产品推荐

