I'm trying to configure nginx to manage all request and redirect to proper microservice on node/express. All microservices works fine, connecting to Redis and Postgres.
I would like to manage without use ports on URL something like...
http://localhost/auth (dockerized microservice 1)
http://localhost/banks (dockerized microservice 2)
http://localhost/users (dockerized microservice 3)
I'm currently using Docker with multiples images, here's most relevant files..
Any idea? Thanks!
docker-compose.yml
version: '3'
services:
nginx:
build: ./nginx
links:
- auth:auth
- banks:banks
- users:users
ports:
- "80:80"
amplify:
build: ./docker-nginx-amplify
environment:
- API_KEY=
- AMPLIFY_IMAGENAME=development
auth:
build: ./api-auth
environment:
- PG_SERVER=172.19.0.3
links:
- redis
- postgres
- banks
ports:
- "8888"
banks:
build: ./api-banks
environment:
- PG_SERVER=172.19.0.3
links:
- redis
- postgres
ports:
- "8888"
users:
build: ./api-users
environment:
- PG_SERVER=172.19.0.3
links:
- redis
- postgres
ports:
- "8888"
redis:
image: redis
ports:
- "6379"
postgres:
image: postgres
hostname: postgres
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgresuser
POSTGRES_PASSWORD: postgresuser
POSTGRES_DB: privatedatabase
restart: unless-stopped
nginx.conf
worker_processes 4;
events { worker_connections 1024; }
http {
upstream node-app {
least_conn;
server auth:8888 weight=10 max_fails=3 fail_timeout=30s;
server banks:8888 weight=10 max_fails=3 fail_timeout=30s;
server users:8888 weight=10 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
location / {
proxy_pass http://node-app;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
}
Related
I am trying to communicate my frontend container with my nodejs backend container. In the docker cli when I ping to other container it shows no error but In the browser hitting localhost returns me a "504 Gateway Time-out"
docker-compose.yml
version: '3'
services:
nodejs-app:
build:
context: ./nodedocker_app
container_name: nodejsserver
hostname: nodejsserver
ports:
- "3000:3000"
networks:
- example-net
depends_on:
- mongo
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27017:27017"
networks:
- example-net
nginx:
build:
context: ./nginx
container_name: nginx
hostname: nginx
ports:
- "80:80"
depends_on:
- nodejs-app
networks:
- example-net
networks:
example-net:
external: true
default.conf (nginx)
server {
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://nodejsserver:3000;
}
}
i am trying to setup a node + react app for local develpoment environment using docker + nginx
nginx is running on 3002
for route /api strip off the /api and redirect to service api i.e. the node app
redirect other routes to service client i.e. react app
docker-compose file
version: "3"
services:
api:
build:
context: api
dockerfile: Dockerfile.dev
ports:
- 3000:3000
volumes:
- ./api:/app
client:
stdin_open: true
build:
context: client
dockerfile: Dockerfile.dev
ports:
- 3001:3000
volumes:
- ./client:/app
nginx:
restart: always
build:
context: nginx
dockerfile: Dockerfile.dev
ports:
- 3002:80
volumes:
- ./nginx/default.dev.conf:/etc/nginx/conf.d/default.conf
depends_on:
- client
- api
nginx default.dev.conf
upstream api {
server api:3000;
}
upstream client {
server client:3001;
}
server {
listen 80;
location / {
proxy_pass http://client;
}
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;
proxy_pass http://api/;
}
}
client Dockerfile.dev
FROM node:alpine
WORKDIR /app
CMD ["npm", "run", "start"]
api Dockerfile.dev
FROM node:alpine
WORKDIR /app
CMD ["npm", "run", "dev"]
nginx Dockerfile.dev
FROM nginx
on visiting http://localhost:3002/api - works
on visiting http://localhost:3002 - i get 502 Bad Gateway
http://localhost:3000 and http://localhost:3001 work
on docker exec -it bytecode_api_1 sh (i.e. going inside terminal of api container) ping client and ping nginx work
the issue is in the connection between reat-app and nginx i.e. neither is reqeust from react going to nginx and nor is a request to http://localhost:3002 being directed to react-server
EDIT: figured it out with the help of some good people on discord, i had made a silly mistake, it should be
upstream client {
server client:3000;
}
i.e. the container port
Im having troubles while trying to access my NodeJs app on a container.
Resume:
I customized my own image to run pm2 and loopback on my container.
I configured my app to listen to a mongodb on another container within a docker network. This works fine.
I created my docker-compose.yml file to looks like this:
version: '3.5'
services:
webapp:
expose:
- "3000"
ports:
- "3000:3000"
volumes:
- ${NODEJS_PATH}:${NODEJS_PATH_DESTIONATION}
image: ${CUSTOMIZED_IMAGE}
networks:
default:
ipv4_address: ${NODEJS_IP}
db:
image: mongo:4.2.6
container_name: mongodb
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
volumes:
- ${MONGO_SCRIPT_PATH}:${MONGO_SCRIPT_DESTINATION_PATH}
networks:
default:
ipv4_address: ${MONGO_INITDB_IP}
networks:
default:
external:
name: ${CUSTOMIZED_NET}
I ran my webapp with docker-compose run webapp for now for customize purposes.
Inside this container I execute the next cmd
pm2 start app.js --env environment_variables
It runs the NodeJS App on http://localhost:3000 inside the docker
I tried to check this from a browser http://{DOCKER_IP}:3000 but it's not working.
I would be eternally grateful with any answer to this trouble.
We used nginx to proxy_pass the service from the docker to the outside world.
We did it this way because we need other services on the docker_network to listen to this one.
Here is the config from the nginx
server {
listen 80;
server_name domain.local;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
I am googling and trying to use nginx for simple node.js application with docker compose. But when I look at localhost:8081 My request returned me as 502 bad gate way. How can I handle this error?
My file structure below:
Load-Balancer:
DockerFile:
FROM nginx:stable-alpine
LABEL xxx yyyyyy
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 8081
CMD ["nginx", "-g", "daemon off;"]
nginx.conf:
events { worker_connections 1024; }
http {
upstream localhost {
server backend1:3001;
server backend2:3001;
server backend3:3001;
}
server {
listen 8081;
server_name localhost;
location / {
proxy_pass http://localhost;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
}
}
docker-compose.yml
version: '3.2'
services:
backend1:
build: ./backend
tty: true
volumes:
- './backend'
backend2:
build: ./backend
tty: true
volumes:
- './backend'
backend3:
build: ./backend
tty: true
volumes:
- './backend'
loadbalancer:
build: ./load-balancer
tty: true
links:
- backend1
- backend2
- backend3
ports:
- '8081:8081'
volumes:
backend:
My repository: https://github.com/yusufkaratoprak/nginx_docker_loadbalancer
There is no command set for the backend container images to run.
The official nodejs images run node by default, which will start the cli when a tty exists. I assuming the tty was enabled in the compose definition to keep the containers from crashing.
A simple Dockerfile for an app would look like:
FROM node:boron
WORKDIR /app
COPY src/. /app/
RUN npm install
EXPOSE 3001
CMD [ "node", "/app/index.js" ]
A tty shouldn't be needed for most daemons, remove the tty settings from the docker-compose.yml. The links are also redundant in version 2+ compose files.
version: '3.2'
services:
backend1:
build: ./backend
volumes:
- './backend'
backend2:
build: ./backend
volumes:
- './backend'
backend3:
build: ./backend
volumes:
- './backend'
loadbalancer:
build: ./load-balancer
ports:
- '8081:8081'
volumes:
backend:
My application is working. console.log is recorded on logs, and able to display process.env.URL on cmd. However, on the browser, it returns error 502, bad gateway.
This is my docker-compose
version: "2"
volumes:
mongostorage:
services:
app:
build: ./app
ports:
- "3000"
links:
- mongo
- redis
command: node ./bin/www
nginx:
build: ./nginx
ports:
- "80:80"
links:
- app:app
mongo:
image: mongo:latest
environment:
- MONGO_DATA_DIR=/data/db
volumes:
- mongostorage:/data/db
ports:
- "27017:27017"
redis:
image: redis
volumes:
- ./data/redis/db:/data/db
ports:
- "6379:6379"
nginx.conf
events {
worker_connections 1024;
}
http{
upstream app.dev{
least_conn;
server app:3000 weight=10 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name app.dev;
location / {
proxy_pass http://app;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
}
This is app/Dockerfile
FROM node:6.3
WORKDIR /var/www/app
RUN mkdir -p /var/www/app
COPY package.json /var/www/app
RUN npm install
COPY . /var/www/app
This is nginx/Dockerfile
FROM nginx:latest
EXPOSE 80
COPY nginx.conf /etc/nginx/nginx.conf
In your nginx.conf file, In proxy_pass_http you should use the server name i.e
proxy_pass http://app.dev/ instead of proxy_pass http://app and it should work;