For development, I'm trying to run two react apps, locally, on the same port (so both can share localStorage), where app1 runs on localhost:8000 and app2 runs on localhost:8000/app2. However, with the setup below I have the issue that all requests to localhost:8000/app2/... are routed to app1.
Is there a way around this?
nginx.conf
Update: moved /app2 block in front of / (see comment).
server {
listen 8000;
server_name localhost;
location /app2 {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://app2:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://app1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
docker-compose.yml
version: "3"
services:
nginx:
image: nginx:latest
ports:
- "8000:8000"
volumes:
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf
app1:
container_name: "app1"
image: node:12
ports:
- "3000:3000"
build:
context: "./app1"
volumes:
- "./app1:/src/app"
app2:
container_name: "app2"
image: node:12
ports:
- "3001:3001"
build:
context: "./app2"
volumes:
- "./app2:/src/app"
Dockerfile app1
And the app2 Dockerfile has EXPOSE 3001 and npm start --port 3001
FROM node:12
WORKDIR /src/app
COPY package*.json ./
RUN npm install
EXPOSE 3000
CMD ["npm", "start", "--port", "3000"]
I'll update this old question with an answer: The problem was not related to nginx or docker-compose. Instead, I was running two create-react-app applications in development mode and I forgot to set { "homepage": "/app2" } in package.json or set PUBLIC_URL=/app2 as environment variable to serve the app from a subdirectory.
Related
I have created docker files for 1 react app, 1 express api backend server and 1 nginx server which I want as reverse proxy for react and express apps. I am getting 502 Bad Gateway and I am unable to figure out why nginx is not able to forward the requests.
docker-compose.yml
version: "3"
services:
reverse-proxy:
image: mern-reverse-proxy
build: ./reverse-proxy
container_name: mern-app-reverse-proxy
ports:
- "80:80"
client:
image: mern-react
build: ./client
volumes:
- ./client/:/usr/src/app
- /usr/src/app/node_modules
container_name: mern-app-client
environment:
CHOKIDAR_USEPOLLING: "true"
depends_on:
- reverse-proxy
server:
image: mern-express
build: ./server
volumes:
- ./server/:/usr/src/app
- /usr/src/app/node_modules
container_name: mern-app-server
environment:
CHOKIDAR_USEPOLLING: "true"
depends_on:
- reverse-proxy
react-dockerfile
FROM node:16-alpine3.11
WORKDIR /usr/src/app
COPY ./package.json ./
COPY ./yarn.lock ./
RUN yarn install
COPY . .
EXPOSE 80
CMD ["yarn", "start"]
express-dockerfile
FROM node:16-alpine3.11
WORKDIR /usr/src/app
COPY ./package.json ./
COPY ./yarn.lock ./
RUN yarn install
COPY . .
EXPOSE 80
CMD ["yarn", "start"]
nginx-dockerfile
FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
COPY ./nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
nginx.conf
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
server {
listen 80;
location /api {
proxy_pass http://server;
proxy_redirect off;
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-Host $server_name;
}
location / {
proxy_pass http://client;
proxy_redirect off;
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-Host $server_name;
}
}
}
Is it because of the ports or proxy_pass in nginx.conf? I have exposed port 80 in all 3 containers because of reverse proxy but haven't mapped the ports of client and server services as they lie in the same network? I would be very grateful if somebody can help. Thanks for your time :)
I have a Node app on port 3000 and I am trying to figure out how to redirect it to https so that it can be accessed at https://localhost:3000/ instead of http://localhost:3000. The setup I currently have redirects http to https, but gives me a '502 Bad Gateway Error' and does not show my app. Any ideas what could be wrong with my setup?
nginx.conf
server {
listen 443 ssl;
ssl_certificate /etc/ssl/server-crt.pem;
ssl_certificate_key /etc/ssl/server-key.pem;
error_page 497 301 =307 https://$host:3000$request_uri;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REQUEST_URI $request_uri;
location / {
set $originaddr http://localhost:3000$request_uri;
proxy_pass $originaddr;
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;
}
}
Dockerfile:
FROM node:latest
WORKDIR /usr/src/app
COPY . ./
RUN npm install
RUN node -v
RUN ls -ltr
EXPOSE 3000
CMD [ "npm", "start" ]
FROM nginx:latest
COPY ./ /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
COPY ssl/server-*pem /etc/ssl/
CMD ls
EXPOSE 443
CMD ["nginx", "-g", "daemon off;"]
docker-compose.yml
services:
node-app:
build:
context: ./my-node-app
dockerfile: Dockerfile
container_name: my-node-app
ports:
- "3000:443"
volumes:
- "./my-node-app/src:/usr/src/app/src"
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'm trying to build multiple services and reverse proxy them with nginx.
So service1 is:
http://api/service1 (nginx) => docker (http://service1:4001/) =>
express (http://localhost:4000)
service2 is :
http://api/service2 (nginx) => docker (http://service2:4002/) =>
express (http://localhost:4000)
It's my first time experimenting with nginx from scratch and I'm stuck, I can't reach any of my service from http://localhost:80/service1 or http://api/service1. And do you think it's a good start of an architecture for micro-services for dev and production?
I also have doubt about my network inside for my docker compose, is it accurate to put that network or let the default docker network ?
(All of the containers working fine);
docker-compose.yml :
version: '3'
services:
mongo:
container_name: mongo
image: mongo:latest
ports:
- '27017:27017'
volumes:
- './mongo/db:/data/db'
nginx:
build: ./nginx
container_name: nginx
links:
- service1
- service2
ports:
- '80:80'
- '443:443'
depends_on:
- mongo
networks:
- api
service1:
build: ./services/service1
container_name: service1
links:
- 'mongo:mongo'
volumes:
- './services/service1:/src/'
ports:
- '4001:4000'
command: yarn dev
networks:
- api
service2:
build: ./services/service2
container_name: service2
links:
- 'mongo:mongo'
volumes:
- './services/service2:/src/'
ports:
- '4002:4000'
command: yarn dev
networks:
- api
networks:
api:
nginx.conf :
worker_processes 1 ;
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name api;
charset utf-8;
location /service1 {
proxy_pass http://service1:4001;
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;
}
location /service2 {
proxy_pass http://service2:4002;
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;
}
}
}
service DockerFile:
FROM node:latest
RUN mkdir /src
WORKDIR /src
COPY package.json /src/package.json
RUN npm install
COPY . /src/
EXPOSE 4000
nginx DockerFile:
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
I'm trying to reach either http://localhost:80/service1/ which would normally get me to http://service1:4001
but I'm getting this error:
[error] 7#7: *2 connect() failed (111: Connection
refused) while connecting to upstream, client: 172.23.0.1, server:
172.23.0.1 - - [15/Apr/2020:22:01:44 +0000] "GET / HTTP/1.1" 502 157 "-" "PostmanRuntime/7.24.1"
bts-api, request: "GET / HTTP/1.1", upstream:
"http://172.23.0.2:4001/", host: "localhost:80"
I'm also trying to reach http://api/service1/ (defined in nginx.conf as server_name) but I don't have any response or ping.
Please add the proxy_redirect parameter and container access port in your nginx.conf files as below.
server {
listen 80;
server_name api;
charset utf-8;
location /service1 {
proxy_pass http://service1:4000;
proxy_redirect http://service1:4000: http://www.api/service1;
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;
}
location /service2 {
proxy_pass http://service2:4000;
proxy_redirect http://service2:4000: http://www.api/service2;
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;
}
Finally it was due to my container name being $(project)-$(container-name) so in nginx i replaced accounts => $(project)-accounts
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;