Reverse proxy nginx docker container with subdomains - docker

I have a docker-compose.yml file with multiple services connected to the same network. the services include nginx exposes port 80, a web app(app1) running on 0.0.0.0:8000, a second web app(app2) running on 0.0.0.0:7000, yet another web app(app3) running on 0.0.0.0:5000 and so on
I can access all the apps from the browser when a go to 0.0.0.0:8000 or 0.0.0.0:5000 etc
I want to access the first app with "http://localhost" from the browser i.e the first web app to be the root. then the others to be subdomains for example; for app2 to be access from this: "http://localhost/app2"
WHAT I HAVE TRIED
I have created a server block for each web app as shown bellow
>> cat app1.conf
upstream app1 {
server app1:8000;
}
server {
# this server listens on port 80
listen 80 ;
server_name app1;
# the location / means that when we visit the root url (localhost:80/), we use this configuration
location / {
proxy_pass http://app1;
}
}
now for one of the subdomains i have :
upstream app2 {
server app2:8000;
}
server {
# this server listens on port 80
listen 80 ;
server_name app2;
# the location / means that when we visit the root url (localhost:80/), we use this configuration
location /app2 {
proxy_pass http://app2;
}
}
Here is my docker-compose.yml file:
version: "3.8"
services:
app1:
image: app1 image
container_name: app1
networks:
- local-network
ports:
- "8000:80"
restart: unless-stopped
app2:
image: app2 image
container_name: app2
networks:
- local-network
ports:
- "7000:80"
restart: unless-stopped
app3:
image: app3 image
container_name: app3
networks:
- local-network
ports:
- "5000:80"
restart: unless-stopped
nginx:
image: nginx:latest
container_name: nginx
networks:
- local-network
command: nginx -g "daemon off;"
volumes:
- ./nginx/sites-available/app1.conf:/etc/nginx/conf.d/sites-available/app1.conf
- ./nginx/sites-available/app2.conf:/etc/nginx/conf.d/sites-available/app2.conf
- ./nginx/sites-available/app3.conf:/etc/nginx/conf.d/sites-available/app3.conf
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
ports:
- "80:80"
restart: unless-stopped
depends_on:
- app1
- app2
- app3
networks:
local-network
in my nginx.conf I including the sites-enabled and manually created symlink of the mounted sites-available configuration files in the nginx container.
On the browser "http://localhost/" works fine but "http://localhost/app1" or app2 or app3 redirects to http://localhost/
There are no errors in the docker logs
What might I be doing wrong?

Here is a quick example, both nginx conf file and docker-compose.yml below:
~/Projects/docker/echo-test $ tree
.
├── docker-compose.yml
└── nginx
└── app.conf
1 directory, 2 files
~/Projects/docker/echo-test $ cat nginx/app.conf
upstream app1 {
server echoer1:7777;
}
upstream app2 {
server echoer2:8888;
}
upstream app3 {
server echoer3:9999;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://app1;
}
location /app2 {
proxy_pass http://app2;
}
location /app3 {
proxy_pass http://app3;
}
}
~/Projects/docker/echo-test $ cat docker-compose.yml
version: "3.8"
services:
echoer1:
hostname: echoer1
image: mendhak/http-https-echo
environment:
- HTTP_PORT=7777
echoer2:
hostname: echoer2
image: mendhak/http-https-echo
environment:
- HTTP_PORT=8888
echoer3:
hostname: echoer3
image: mendhak/http-https-echo
environment:
- HTTP_PORT=9999
nginx:
image: nginx
command: nginx -g "daemon off;"
volumes:
- ./nginx/app.conf:/etc/nginx/conf.d/app.conf
ports:
- "80:80"

Related

How do I configure/ reconfigure an existing NGINX server to proxy to a docker container?

I have an existing NGINX server hosting 2 websites, one as standard and one on a node server. I want to run 3 docker containers as well on this.
All of the tutorials suggest running NGINX in a container, however this would conflict with my existing set up.
nodejs server, ports 3030:3030
mysql, ports 3360:3360
phpmyadmin, ports 8080:80
They run on localhost on my local machine fine, but I cant get NGINX on the remote server to host them.
I want to be able to access the node server at http://publicIP:3030
I have tried to follow this answer but NGINX is giving me 404 error when trying to access.
my nginx config is:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /paragon/ {
proxy_pass http://localhost:3030/;
# proxy_set_header X-SRV paragon;
}
location /phpmyadmin {
proxy_pass http://localhost:8080/;
# proxy_set_header X-SRV phpmyadmin;
}
location /mysql {
proxy_pass http://localhost:3360/;
# proxy_set_header X-SRV mysql;
}
I have tried it with the X-SRV headers uncommented as well.
My docker-compose.yml config is:
services:
web:
container_name: paragon_web
build: .
command: npm run
depends_on:
- db
volumes:
- ./:/app
- /node_modules
networks:
- paragon_net
ports:
- "3030:3030"
db:
container_name: paragon_db
image: mysql:8.0
command:
--default-authentication-plugin=mysql_native_password
--init-file ./src/data/db_init.sql
restart: unless-stopped
volumes:
- ./src/data/db_init.sql:/docker-entrypoint-initdb.d/
- mysql-data:/var/lib/mysql
ports:
- "3360:3306"
expose:
- "3306"
environment:
MYSQL_DATABASE: paragon
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: admin
MYSQL_PASSWORD: paragon99
SERVICE_TAG: dev
SERVICE_NAME: paragon_db
networks:
- paragon_net
# volumes:
phpmyadmin:
container_name: sql_admin
image: phpmyadmin:5.2.0-apache
restart: always
depends_on:
- db
ports:
- "8090:80"
networks:
- paragon_net
networks:
paragon_net:
driver: bridge
The location of the new site on the server are at /var/www/newsite

Configure NGINX in docker container to network with other docker container app

I have one docker container with nginx (path docker/nginx):
services:
web:
image: nginx
volumes:
- ./templates:/etc/nginx/templates
ports:
- "80:80"
networks:
- nginx-net
networks:
nginx-net:
name: proxynet
external: true
And docker with app (docker/app):
services:
php:
build:
context: .
dockerfile: Dockerfile
container_name: app_php
networks:
- nginx-net
networks:
nginx-net:
external: true
name: proxynet
File nginx conf.template
server {
listen 80 default_server;
server_name subdomain.domain.com;
location /{
proxy_pass app_php:80/;
}
}
I want nginx to redirect to the app container and to other containers in the future.
But in web browser subdomain.domain.com show 504 error.
ping -c 4 app_php - ping from nginx container is ok
I spent a few hours and I don't know what the problem is, I suspect nginx conf
You need to expose app_php on another port eg. 8000 and have nginx act as proxy for that. Something like:
services:
php:
build:
context: .
dockerfile: Dockerfile
container_name: app_php
networks:
- nginx-net
ports:
- 8000:80
nginx.conf:
server {
listen 80 default_server;
server_name subdomain.domain.com;
location /{
proxy_pass app_php:8000;
}
}
Probably best to have all containers in a single docker-compose.yml file also.

Nginx and Docker with static html: Proxy pass does not forward to expected route

I want to serve static html as a service with Docker and nginx as a reverse proxy (there are also a python backend and mysql container, which I excluded here)
I have got the following docker-compose file:
version: "3.7"
frontend:
build: ./frontend
container_name: frontend
restart: always
ports:
- "5000:80"
nginx:
build: ./nginx
container_name: nginx
restart: always
ports:
- "80:80"
Dockerfile for Frontend:
FROM nginx:alpine
COPY . /usr/share/nginx/html
nginx.conf in my I do this:
server {
listen 80;
location /frontend {
proxy_pass http://frontend:5000/;
#proxy_pass http://frontend:5000; -> also tried this
}
}
Everything builds fine, but the proxy_pass does not work as expected.
Where I can reach my app:
http://localhost:5000/
Desired:
http://localhost/frontend
What did I do wrong?
The NGINX location should be the root (I imagine there is no /frontend web path)
location / {
proxy_pass http://frontend:5000/;
}

How to connect two docker containers together

I have a reactjs front end application and a simple python flask. And I am using a docker-compose.yml to spin up both the containers, and it is like this:
version: "3.2"
services:
frontend:
build: .
environment:
CHOKIDAR_USEPOLLING: "true"
ports:
- 80:80
links:
- "backend:backend"
depends_on:
- backend
backend:
build: ./api
# volumes:
# - ./api:/usr/src/app
environment:
# CHOKIDAR_USEPOLLING: "true"
FLASK_APP: /usr/src/app/server.py
FLASK_DEBUG: 1
ports:
- 8083:8083
I have used links so the frontend service can talk to backend service using axios as below:
axio.get("http://backend:8083/monitors").then(res => {
this.setState({
status: res.data
});
});
I used docker-compose up --build -d to build and start the two containers and they are started without any issue and running fine.
But now the frontend cannot talk to backend.
I am using an AWS ec2 instance. When the page loads, I tried to see the for any console errors and I get this error:
VM167:1 GET http://backend:8083/monitors net::ERR_NAME_NOT_RESOLVED
Can someone please help me?
The backend service is up and running.
You can use a nginx as reverse proxy for both
The compose file
version: "3.2"
services:
frontend:
build: .
environment:
CHOKIDAR_USEPOLLING: "true"
depends_on:
- backend
backend:
build: ./api
# volumes:
# - ./api:/usr/src/app
environment:
# CHOKIDAR_USEPOLLING: "true"
FLASK_APP: /usr/src/app/server.py
FLASK_DEBUG: 1
proxy:
image: nginx
volumes:
- ./nginx.conf:/etc/nginx/conf.d/example.conf
ports:
- 80:80
minimal nginx config (nginx.conf):
server {
server_name example.com;
server_tokens off;
location / {
proxy_pass http://frontend:80;
}
}
server {
server_name api.example.com;
server_tokens off;
location / {
proxy_pass http://backend:8083;
}
}
The request hits the nginx container and is routed according the domain to the right container.
To use example.com and api.example.com you need to edit your hosts file:
Linux: /etc/hosts
Windows: c:\windows\system32\drivers\etc\hosts
Mac: /private/etc/hosts
127.0.0.1 example.com api.example.com

Problem with nginx reverse proxy and docker. Only one proxy_path is working

I am completly lost.. So i have two proxy_paths in my nginx conf: '/' and '/api'. '/' redirects to my frontend and is working perfectly but the '/api' proxy path is not working at all. The proxy server is logging requests to '/api/' but not forwarding them to my actual api-server. I'm missing something. Is the '/' proxy_path some sort of catch all that overrides any other proxy paths? Any assistance would be invaluable! Thanks! Here are my configs:
nginx reverse proxy conf:
server {
listen 80;
server_name proxy;
location / {
proxy_pass http://frontend_prod:3000/;
}
location /api {
proxy_pass http://api_prod:3333/;
}
}
docker-compose:
version: '3.1'
services:
proxy:
build: ./proxy/
ports:
- '9000:80'
restart: always
depends_on:
- frontend_prod
- api_prod
frontend_prod:
build: ./frontend/nginx/
ports:
- '3000'
depends_on:
- api_prod
restart: always
db:
image: mariadb
restart: always
environment:
MYSQL_ROOT_PASSWORD: hunter2
api_prod:
build: ./backend/api/
command: npm run production
ports:
- '3333'
depends_on:
- db

Resources