vite-typescript-express-ssr-react - API 无法在 VPS 上运行

问题描述 投票:0回答:1

第一次使用 Docker 设置 VPS 来为我的 Node 应用程序提供服务。

我目前正在努力让我的应用程序 API 在我的 VPS 上运行。 我使用一个容器作为前端和后端。当在本地构建并在生产中运行时,everythink 工作正常,使用

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 获取,但这也不起作用。 我使用这些防火墙设置来解决绕过 UFW 的 docker。 然后我允许以下端口

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)

用于 ssh 连接的端口 22。

我尝试添加express服务器端口7456来解决API错误,不幸的是它不起作用。

端口 80 用于我的 nginx 反向代理。 它将来自域的每个传入请求转发到我运行的应用程序

http://container-name:7456
。 (我使用桥接驱动程序设置了 docker 网络。)

我需要允许任何其他端口才能让我的 api 工作吗?

更新:

将域名

http://localhost:7456
http://container-name:7456
和 http://nginx-container-name:80 添加到 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;
node.js docker nginx-reverse-proxy vps ufw
1个回答
0
投票

好吧,这是一个菜鸟错误,必须使用

https://example.com:7456/api/auth/register 不是 https://localhost:7456/api/auth/register

© www.soinside.com 2019 - 2024. All rights reserved.