I'm working on a Typescript
monorepo using turborepo
that holds multiple microservices (nextjs
, expressjs
, create-react-app
).
each of these microservices is served in its own PORT
.
In order to make the development experience more fluid, we decided to add a Nginx
server (in a docker
image) that will collect all the ports from each Microservice and reserve them all under one PORT
.
When I tried to add a Vite react app
and put it behind the same Nginx server, it didn't work because it is trying to access the files in node_modules in my local machine.
Does anyone have a workaround?
Below is a sample of my configs:
Nginx config file :
upstream imgproxy {
server imgproxy:3000;
}
server {
listen 80;
location / {
return 301 https://$host:3000$request_uri;
}
}
server {
listen 443 ssl;
client_max_body_size 240M;
ssl_certificate cert/localhost3000.crt;
ssl_certificate_key cert/localhost3000.key;
location /api/v1/auth {
proxy_pass http://host.docker.internal:4001;
}
location /dashboard {
proxy_pass https://host.docker.internal:3001;
}
location /api/v1/blogs {
proxy_pass http://host.docker.internal:4010;
}
location / {
proxy_pass http://host.docker.internal:4200;
}
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
include h5bp/tls/policy_balanced.conf;
# Custom error pages
include h5bp/errors/custom_errors.conf;
# Include the basic h5bp config set
include h5bp/basic.conf;
}
My vite.conf.ts
import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react-swc";
import fs from "fs";
import tsconfigPaths from "vite-tsconfig-paths";
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
return {
base: "/dashboard",
server: {
port: 3001,
https: {
cert: fs.readFileSync("../../nginx/cert/localhost3000.crt"),
key: fs.readFileSync("../../nginx/cert/localhost3000.key"),
},
},
plugins: [react(), tsconfigPaths()],
};
});
Nginx's errors :
2023/05/05 13:54:02 [error] 33#33: *2 open() "/etc/nginx/html/dashboard/node_modules/.vite/deps/react_jsx-dev-runtime.js" failed (2: No such file or directory), client: 172.20.0.1, server: , request: "GET /dashboard/node_modules/.vite/deps/react_jsx-dev-runtime.js?v=12d55949 HTTP/1.1", host: "localhost:3000", referrer: "https://localhost:3000/dashboard/src/index.tsx"
172.20.0.1 - - [05/May/2023:13:54:02 +0000] "GET /dashboard/node_modules/.vite/deps/react_jsx-dev-runtime.js?v=12d55949 HTTP/1.1" 404 178 "https://localhost:3000/dashboard/src/index.tsx" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36" "-"
172.20.0.1 - - [05/May/2023:13:54:02 +0000] "GET /dashboard/@react-refresh HTTP/1.1" 200 3393 "https://localhost:3000/dashboard" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36" "-"
2023/05/05 13:54:03 [error] 33#33: *2 open() "/etc/nginx/html/dashboard/node_modules/.vite/deps/react.js" failed (2: No such file or directory), client: 172.20.0.1, server: , request: "GET /dashboard/node_modules/.vite/deps/react.js?v=12d55949 HTTP/1.1", host: "localhost:3000", referrer: "https://localhost:3000/dashboard/src/index.tsx"
172.20.0.1 - - [05/May/2023:13:54:03 +0000] "GET /dashboard/node_modules/.vite/deps/react.js?v=12d55949 HTTP/1.1" 404 178 "https://localhost:3000/dashboard/src/index.tsx" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36" "-"
2023/05/05 13:54:03 [error] 35#35: *6 open() "/etc/nginx/html/dashboard/node_modules/.vite/deps/react-redux.js" failed (2: No such file or directory), client: 172.20.0.1, server: , request: "GET /dashboard/node_modules/.vite/deps/react-redux.js?v=12d55949 HTTP/1.1", host: "localhost:3000", referrer: "https://localhost:3000/dashboard/src/index.tsx"
172.20.0.1 - - [05/May/2023:13:54:03 +0000] "GET /dashboard/node_modules/.vite/deps/react-redux.js?v=12d55949 HTTP/1.1" 404 178 "https://localhost:3000/dashboard/src/index.tsx" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36" "-"
172.20.0.1 - - [05/May/2023:13:54:03 +0000] "GET /dashboard/@fs/C:/Users/nader/Documents/devProjects/boilerplate/packages/browser/core-ui/DarkModeProvider/index.tsx HTTP/1.1" 200 2026 "https://localhost:3000/dashboard/src/index.tsx" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36" "-"
On first glance, it seems like the react app that you are trying to deploy has not been dockerized properly. Even if you are only trying to forward the development port, you would still need the dockerfile to have all the necessary steps of setting up the react app like 'npm install' or 'yarn install' etc. If that is all clear, a smart solution to resolving it would be to create a volume inside the nginx container and giving it the files it needs from outside the container on the same path like this, if inside a docker compose file:
volume:
- react_app/node_modules : /etc/nginx/html/dashboard/node_modules