VPS上Docker部署Vite-TS-Express-SSR-React应用API无法访问求助
问题:Docker部署Node应用API无法访问
首次在VPS上通过Docker部署单容器承载前后端的Node应用,本地运行正常,使用http://localhost:7456/api...调用API无问题,但在VPS上运行时出现以下错误:
API error: Network Error POST http://localhost:7456/api/auth/register net::ERR_NAME_NOT_RESOLVED
尝试改用http://container-name:7456调用API同样无效。已处理Docker绕过UFW的安全问题,开放的端口如下:
22/tcp ALLOW Anywhere 7456/tcp ALLOW Anywhere 80/tcp ALLOW Anywhere 22/tcp (v6) ALLOW Anywhere (v6) 7456/tcp (v6) ALLOW Anywhere (v6) 80/tcp (v6) ALLOW Anywhere (v6)
其中22端口用于SSH连接,80端口分配给Nginx反向代理,负责将域名请求转发到运行在http://container-name:7456的应用(已通过bridge驱动创建Docker网络)。开放Express服务端口7456后问题仍未解决,后续更新了CORS源配置:
import cors from "cors"; import express from "express"; const securityMiddleware = express(); // Use CORS middleware securityMiddleware.use( cors({ origin: [ "http://gamenights.de", "http://container-name:7456", "http://localhost:7456", "http://nginx-container-name:80", ], credentials: true, }) ); export default securityMiddleware;
请问是否需要开放其他端口以解决API无法访问的问题?
内容的提问来源于stack exchange,提问作者Louis Eiden
相关产品推荐
相关产品推荐

