I have the following setup and cannot for the life of me figure out why I can't connect to the api.
nginx.conf
worker_processes auto;
worker_rlimit_nofile 200000;
events {
use epoll;
accept_mutex on;
multi_accept on;
worker_connections 1024;
}
http {
error_log /etc/nginx/error_log.log warn;
client_max_body_size 20m;
server {
listen 80;
listen [::]:80;
location / {
proxy_pass http://api:8080/;
}
location /health {
return 200;
access_log off;
}
}
}
docker-compose.yml
version: "3.7"
services:
nginx:
container_name: nginx
image: "nginx:latest"
ports:
- "8000:80"
networks:
- internal_net
volumes:
- ./container/nginx.conf:/etc/nginx/nginx.conf
api:
container_name: api
build:
context: .
dockerfile: container/Dockerfile
expose:
- "8080"
networks:
- internal_net
depends_on:
- postgres
command: ["./wait-for-it.sh", "postgres:5432", "--timeout=60", "--", "./52-server-go"]
postgres:
container_name: postgres
image: "postgres:9.5-alpine"
expose:
- "5432"
networks:
- internal_net
networks:
internal_net:
driver: bridge
volumes:
container:
My api is set to run on port 8080, when I go into the container and run a curl request against that address it works. According to the compose file, it should be exposing that address to the local compose network that is shared by all services including nginx.
According to the nginx config it should be passing every request (except /health
check, which works) to the api
service. Instead what is returned is a 502 from nginx.
Where am I mixed up? What did I do wrong?
The issue was actually in my go app. Using golang gorilla/mux
, had to change the address:
original (broken)
// Start server
address := "127.0.0.1:8080"
srv := &http.Server{
Handler: r,
Addr: address,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
fix
// Start server
address := "0.0.0.0:8080"
srv := &http.Server{
Handler: r,
Addr: address,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
I don't know why, but that fixed it.