node.jsdockernginx-reverse-proxyvpsufw

vite-typescript-express-ssr-react - API not working on VPS


first time setting up a VPS with Docker to serve my Node application.

I am currently struggling to get my applications API to work on my VPS. I am using one container for both frontend and backend. When building locally and running in production everythink works fine, using http://localhost:7456/api... for fetching from the api. but when I run my application on the VPS I get:

API error: Network Error
POST http://localhost:7456/api/auth/register net::ERR_NAME_NOT_RESOLVED

I also tried using http://container-name:7456 to fetch from the api but that didn't work either. I used these Firewall settings to adress docker bypassing UFW. I then allowed the following ports

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)

Port 22 for ssh connections.

I tried adding the express server port 7456 to solves the API error, unfortunately it doesn't work.

Port 80 is for my nginx reverse proxy. It forwards every incoming requests from the domain to my application running on http://container-name:7456. (I set up a docker network using bridge driver.)

Do I need to allow any other Ports for my api to work?

UPDATE:

Added the domain, http://localhost:7456, http://container-name:7456 and http://nginx-container-name:80 to CORS origin

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;

Solution

  • well that was such a rookie mistake had to use

    https://example.com:7456/api/auth/register not https://localhost:7456/api/auth/register