I was trying to setup two frontend react applications sharing a server written in nodejs. I tried setting up one frontend application it worked correctly but gets an error host not found in upstream "server" in /etc/nginx/conf.d/default.conf
when i add the other one.
here is my docker-compose
version: "3"
services:
postgres:
image: "postgres:latest"
environment:
- POSTGRES_PASSWORD=postgres_password
nginx:
depends_on:
- api
- client
- client2
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
ports:
- "3050:80"
api:
build:
dockerfile: Dockerfile.dev
context: "./server"
volumes:
- /app/node_modules
- ./server:/app
environment:
- PGUSER=postgres
- PGHOST=postgres
- PGDATABASE=postgres
- PGPASSWORD=postgres_password
- PGPORT=5432
client:
stdin_open: true
environment:
- CHOKIDAR_USEPOLLING=true
build:
dockerfile: Dockerfile.dev
context: ./client
volumes:
- /app/node_modules
- ./client:/app
client2:
stdin_open: true
environment:
- CHOKIDAR_USEPOLLING=true
build:
dockerfile: Dockerfile.dev
context: ./client2
volumes:
- /app/node_modules
- ./client2:/app
nginx default.conf
upstream client {
server client:3000;
}
upstream client2 {
server client:3000;
}
upstream api {
server api:5000;
}
server {
listen 80;
location / {
proxy_pass http://client;
}
location /login {
proxy_pass http://client2;
}
location /sockjs-node {
proxy_pass http://client;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location /api {
rewrite /api/(.*) /$1 break;
proxy_pass http://api;
}
}
apart from the above my two frontend react applications (client, client2) also contains an nginx directory with default.conf
client/nginx/default.conf
server{
listen 3000;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
client2/nginx/default.conf
one of my client Dockerfile.dev
FROM node:14.14.0-alpine
WORKDIR /app
COPY ./package.json ./
RUN npm i
COPY . .
CMD ["npm", "run", "start"]
one of my client Dockerfile
FROM node:14.14.0-alpine as builder
WORKDIR /app
COPY ./package.json ./
RUN npm i
COPY . .
RUN npm run build
FROM nginx
EXPOSE 3000
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /app/build /usr/share/nginx/html
2
Answers
You need to point your set your upstream clients in your nginx config to be the names of the services in your docker-compose. Specifically here, you need to change under client2
You can just use linuxserver/swag images for subdomains, and etc.. your files would be:
docker-compose.yaml:
something.subdomain.conf file for your subdomain in /nginx/config/nginx/proxy-confs:
NGINX default.conf file:
Please read these for more info: