Docker uWSGI - NGINX: uWSGI ok but NGINX is a 502 - docker

I configured my django-uwsgi-nginx using docker compose with the following files.
From browser "http://127.0.0.1:8000/" works fine and gives me the django default page
From browser "http://127.0.0.1:80" throws a 502 Bad Gateway
dravoka-docker.conf
upstream web {
server 0.0.0.0:8000;
}
server {
listen 80;
server_name web;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias "/dravoka-static/";
}
location / {
include uwsgi_params;
proxy_pass http://web;
}
}
nginx/Dockerfile
FROM nginx:latest
RUN echo "---------------------- I AM NGINX --------------------------"
RUN rm /etc/nginx/conf.d/default.conf
ADD sites-enabled/ /etc/nginx/conf.d
RUN nginx -t
web is just from "django-admin startproject web"
docker-compose.yaml
version: '3'
services:
nginx:
restart: always
build: ./nginx/
depends_on:
- web
ports:
- "80:80"
web:
build: .
image: dravoka-image
ports:
- "8000:8000"
volumes:
- .:/dravoka
command: uwsgi /dravoka/web/dravoka.ini
Dockerfile
# Ubuntu base image
FROM ubuntu:latest
# Some installs........
EXPOSE 80

When you say from the docker instance , you are running curl from with in the container ?? or you are running the curl command from your local ?
if you are running it from your local , update your docker-compose's web service to following
...
web:
build: .
image: dravoka-image
expose:
- "8000:8000"
volumes:
- .:/dravoka
command: uwsgi /dravoka/web/dravoka.ini
and try again.

Related

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/;
}

Reverse proxy nginx docker container with subdomains

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"

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

Nginx can't find upstream host in multi-container docker compose setup

I'm running a multi-docker container locally with docker-compose, the containers are React front-end 'client', a Nodejs app 'api', and a Nginx proxy in sits in front of two. I have been using the docker-compose setup as follow for a while
version: '3'
services:
client:
build:
dockerfile: Dockerfile.dev
context: ./client
volumes:
- /usr/app/node_modules
- ./client:/usr/app
api:
build:
dockerfile: Dockerfile.dev
context: ./server
volumes:
- /usr/app/node_modules
- ./server:/usr/app
nginx:
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
ports:
- '8080:80'
and my Nginx setup is as follows
upstream client {
server client:3000;
}
upstream api {
server api:5000;
}
server {
listen 80;
server_name _;
location / {
if ($http_x_forwarded_proto != 'https') {
return 301 https://$host$request_uri;
}
proxy_pass http://client;
}
location /api {
if ($http_x_forwarded_proto != 'https') {
return 301 https://$host$request_uri;
}
rewrite /api/(.*) /$1 break;
proxy_pass http://api;
}
}
Recently when I tried to start up the containers, i got following error:
nginx_1 | 2019/08/08 18:11:12 [emerg] 1#1: host not found in upstream "client:3000" in /etc/nginx/conf.d/default.conf:2
nginx_1 | nginx: [emerg] host not found in upstream "client:3000" in /etc/nginx/conf.d/default.conf:2
Any idea why nginx not able to find upstream?
I have tried to add links to nginx setup blocks as follows:
nginx:
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
links:
- client:client
- api:api
ports:
- '8080:80'
I also tried 'depends_on' instead of links. After adding links, nginx no longer complains and exit with code 0. But when i visit the localhost:8080,
it gives a 301 redirect to https://localhost.
Any help or direction are greatly appreciated!!!
You should check names of your services. Docker compose will start your service api in pod named [YOUR_PROJECT_NAME]_api_1. Start only api and client and check output of docker ps. You should gey list of names of pods.
In newer docker_compose syntax versions you can use link_external to map [YOUR_PROJECT_NAME]_api_1 to api.

Resources