failed connect (nginx with docker) - docker

I'm getting this failed to connect issues when it comes to communicating with other docker containers e.g Java server end.
This is the issue I'm getting
nginx_1 | 2019/12/14 17:39:47 [error] 6#6: *2 connect() failed (111:
Connection refused) while connecting to upstream, client: 172.19.0.1,
server: , request: "GET /api/cards HTTP/1.1", upstream:
"http://172.19.0.4:80/api/cards", host: "localhost", referrer:
"http://localhost/"
Here is my Docker-Compose configuration (docker-compose.development.yml)
version: '3'
services:
auth:
build:
context: ./auth
dockerfile: dev.Dockerfile
volumes:
- ./auth:/var/app
- ~/.gradle/:/root/.gradle/
client:
build:
context: ./client
dockerfile: dev.Dockerfile
volumes:
- ./client:/var/app/
- /var/app/node_modules
server:
build:
context: ./server
dockerfile: dev.Dockerfile
volumes:
- ./server:/var/app/
- ~/.gradle/:/root/.gradle/
nginx:
image: nginx
volumes:
- ./server.dev.conf:/etc/nginx/conf.d/default.conf
ports:
- "80:80"
depends_on:
- auth
- client
- server
Here is my nginx docker configuration (server.dev.conf)
server {
listen 80;
location /login/ {
proxy_pass http://auth:80/login/;
}
location /api/ {
proxy_pass http://server:80/api/;
}
location /resource/ {
proxy_pass http://server:80/resource/;
}
location / {
proxy_pass http://client:80;
rewrite ^/home/.*$ / last;
}
}
Remind you this is for our development hosted locally. The production version uses SSL

Related

connection refused while connecting to upstream Nginx Docker

I'm trying to build a FastAPI app in docker, I find the site comes to a '502-Bad Gate Way' error and nginx log shows:
nginx_1 | 2020/12/21 18:23:13 [error] 29#29: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.27.0.1, server: , request: "GET /user HTTP/1.1", upstream: "http://172.27.0.3:8000/user", host: "localhost:8080"
Here is my docker-compose.yaml file:
version: '3.7'
services:
fastapi:
build: ./api
command: uvicorn main:app --reload --host 0.0.0.0 --port 8000
volumes:
- ./fastapi/:/fastapi/
ports:
- 8000:8000
depends_on:
- db
db:
image: mongo:latest
container_name: mongo4
ports:
- "27017:27017"
volumes:
- "./data/mongo/data:/data/db"
nginx:
image: nginx:latest
ports:
- "8080:8080"
volumes:
- ./nginx_config.conf:/etc/nginx/conf.d/default.conf
depends_on:
- fastapi
Nginx config file:
server {
listen 8080;
location /user {
proxy_pass http://fastapi:8000/user;
}
}
Thanks for help.

Nginx local reverse proxy server bad gateway

I want to connect to my nextcloud server via https, so I set up an Nginx server to handle it. Nextcloud and Nginx both run in docker. Https works, but when I try to connect I get "502 Bad Gateway". I can also access nextcloud over 192.168.0.28:7070, so its running.
cloud.conf
upstream cloud {
server localhost:7070;
}
server {
listen 443 ssl;
server_name 192.168.0.28;
access_log /etc/nginx/logs/cloud.access.log;
error_log /etc/nginx/logs/cloud.error.log;
include common.conf;
include /etc/nginx/ssl.conf;
location / {
proxy_pass http://cloud;
include common_location.conf;
}
}
error.log
2020/11/27 11:41:26 [error] 210#210: *114 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.0.178, server: 192.168.0.28, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:7070/", host: "192.168.0.28"
2020/11/27 11:41:26 [error] 210#210: *114 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.0.178, server: 192.168.0.28, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:7070/", host: "192.168.0.28"
cloud.yml
version: '2'
volumes:
cloud:
db:
services:
db:
container_name: cloud_database
hostname: cloud_database
image: mariadb
command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
restart: "unless-stopped"
volumes:
- db:/var/lib/mysql
- /etc/localtime:/etc/localtime:ro
environment:
- MYSQL_ROOT_PASSWORD= password
- MYSQL_PASSWORD= password
- MYSQL_DATABASE=nextcloud
- MYSQL_USER=nextcloud
app:
container_name: cloud
hostname: cloud
image: nextcloud
links:
- db
volumes:
- /var/docker/cloud:/var/www/html
- /etc/localtime:/etc/localtime:ro
restart: "unless-stopped"
ports:
- 7070:80

Docker compose nginx permissions problem using fedora and podman

I am trying to create a boilerplate project with docker, php & nginx.
After I run build & up commands, I'm trying to access localhos:8080 (my exposed port on nginx), but I get this error in logs:
[crit] 24#24: *1 open() "/var/www/app/public/" failed (13: Permission
denied), client: 127.0.0.1, server: api.boilerplate.local, request:
"GET / HTTP/1.1", host: "localhost:8080" 2020/11/21 [error] 24#24: *1
connect() failed (111: Connection refused) while connecting to
upstream, client: 127.0.0.1, server: api.boilerplate.local, request:
"GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host:
"localhost:8080"
This is my docker-compose.yml:
version: "3.8"
services:
db:
image: mysql
command: ["--default-authentication-plugin=mysql_native_password"]
restart: unless-stopped
ports:
- 3306:3306
env_file:
- ${PATH_CORE}/.env
volumes:
- mysql:/var/lib/mysql
networks:
- backend
api:
build: ${PATH_CORE}/docker/api
restart: unless-stopped
depends_on:
- db
env_file:
- ${PATH_CORE}/.env
volumes:
- ${PATH_CORE}:/var/www/app
networks:
- backend
- frontend
nginx:
build: ${PATH_CORE}/docker/nginx
restart: unless-stopped
depends_on:
- api
ports:
- 8080:80
volumes:
- ${PATH_CORE}/public:/var/www/app/public
networks:
- backend
volumes:
mysql:
networks:
frontend:
backend:
This is my nginx Dockerfile:
FROM nginx:alpine
COPY nginx.conf /etc/nginx/
COPY default.conf /etc/nginx/conf.d/default.conf
RUN apk add shadow && set -x && usermod -u 1000 nginx && groupmod -g 1000 nginx
WORKDIR /etc/nginx/
EXPOSE 80 443
And default.conf file:
server {
listen 80;
server_name api.boilerplate.local;
root /var/www/app/public;
location / {
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /index.php/$1 last;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass api:9001;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
internal;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
}
What do I do wrong?
PS: I'm using Fedora and podman
Your nginx container fails to bind to the port 80 as this is a privileged port and requires a super user rights. Simply change the port number to 8080 and update the compose file to match the new port mapping.
And note, that you need to adjust selinux settings when passing volumes on selinux enabled systems.

DNS in docker-compose, map to non-80 port?

While experimenting with docker-compose, I'm running into some issues with NGINX and the dns. The error is the regular "connection refused while connecting to upstream" in NGINX. I think the problem arises due to the port numbers.
Examples online like this one run the NGINX on port 80, which doesn't cause the issue.
The docker-compose.yml:
version: '3'
services:
http-server:
networks:
- mynetwork
image: nginx_image
ports:
- 8080:8080
depends_on:
- frontend
- rest_api
frontend:
networks:
- mynetwork
image: frontend_image
ports:
- 8001:8001
rest_api:
networks:
- mynetwork
image: rest_api_image
ports:
- 8000:8000
networks:
mynetwork:
driver: bridge
nginx.conf for nginx_image has this block:
server {
listen 8080;
location /static/js/ {
proxy_pass http://frontend;
}
location /static/css/ {
proxy_pass http://frontend;
}
location /static/ {
proxy_pass http://rest_api;
}
location / {
proxy_pass http://frontend;
}
location /rest_api/ {
proxy_pass http://rest_api;
}
}
Now, both the frontend and api are called over port 80, while it should be 8001 and 8000.
What am I missing? I would expect docker-compose to make the port mapping automatically.
Thanks in advance!
Kind regards,
DA
EDIT1: here's the error (including the hostname suggestion)
EDIT2: updated the question
http-server_1 | 2018/06/04 14:47:50 [error] 14#14: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /rest_api/admin/ HTTP/1.1", upstream: "http://172.18.0.2:80/rest_api/admin/", host: "localhost:8080"
you have to specify hostnames:
frontend:
image: frontend_image
hostname: frontend
ports:
- 8001:8001
rest_api:
hostname: rest_api

proxy'ing between docker containers

I have a docker setup with the following
rails api backend
mysql db
redis db
node/react frontend (webpack)
nginx serving the frontend
(the rails backend is currently being served through the builtin puma server - I think I will move it to the same nginx server runing the node app)
My problem is that the frontend will request stuff on the backend, but this does not work.
I have set up a proxy on nginx as follows:
#nginx.conf
server {
listen 8080;
# Always serve index.html for any request
location / {
# Set path
root /wwwroot/;
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://127.0.0.1:3000;
}
}
But when I when I initiate an api call I get the following in the nginx log:
nginx-server | 2017/05/13 20:56:08 [error] 5#5: *19 connect() failed (111: Connection refused) while connecting to upstream, client: 172.21.0.1, server: , request: "POST /api/authenticate HTTP/1.1", upstream: "http://127.0.0.1:3000/api/authenticate", host: "localhost:8080", referrer: "http://localhost:8080/"
nginx-server | 172.21.0.1 - - [13/May/2017:20:56:08 +0000] "POST /api/authenticate HTTP/1.1" 502 575 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36" "-"
And I do not see any thing hitting the puma server.
I am not sure where I should be looking. Is this a problem with my docker-compose file or is it a nginx issue (or both).
I have included my docker-compose.yml below:
version: '2'
services:
nginx:
build:
context: .
dockerfile: docker.nginx
image: pt-nginx
container_name: nginx-server
ports:
- "8080:8080"
volumes:
- wwwroot:/wwwroot
webpack:
build:
context: ./frontend
dockerfile: docker.webpack
image: pt-webpack
container_name: react-frontend
ports:
- "35729:35729"
volumes:
- ./frontend:/app
- /app/node_modules
- wwwroot:/wwwroot
db:
build:
context: ./backend
dockerfile: docker.mysql
image: pt-mysql
container_name: mysql-server
env_file: ./backend/.env
ports:
- "3306:3306"
volumes:
- ./sql/data/:/var/lib/mysql
redis:
build:
context: ./backend
dockerfile: docker.redis
image: pt-redis
container_name: redis-server
app:
build:
context: ./backend
dockerfile: docker.rails
image: pt-rails
container_name: rails-server
command: >
sh -c '
rake resque:start_workers;
bundle exec rails s -p 3000 -b 0.0.0.0;
'
env_file: ./backend/.env
volumes:
- ./backend:/usr/src/app
- /Users/mh/Pictures/ROR_PT/phototank:/Users/martinhinge/Pictures/ROR_PT/phototank
ports:
- "3000:3000"
expose:
- "3000"
depends_on:
- db
- redis
volumes:
wwwroot:
driver: local
The problem is that your nginx service is requesting upstream of localhost (127.0.0.1). But the application is in another container (with another IP). You can reference the rails application by DNS at appsince they are both in a default network. The upstream in nginx configuration would look something like proxy_pass http://app:3000; instead.
Read more about the networking at https://docs.docker.com/compose/networking/, specifically:
Within the web container, your connection string to db would look like postgres://db:5432, and from the host machine, the connection string would look like postgres://{DOCKER_IP}:8001.

Resources