Ngnix config: 502 gateway error

Greetings

I am trying spin a simple ngnix container to run as proxy for my grafana on windows machine. This is what I have but I seem to be stuck

per Run Grafana behind a reverse proxy | Grafana Labs

events {
  worker_connections  4096;  ## Default: 1024
}

http {
      map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
      }
      upstream grafana {
        server localhost:3951;
      }

      server {
            listen 80;
            root /usr/share/nginx/html;
            index index.html index.htm;

            location / {
              proxy_set_header Host $http_host;
              proxy_pass http://grafana;
            }

      }
}

default.ini

# The http port to use
http_port = 3951

# The public facing domain name used to access grafana from a browser
domain = localhost

# Redirect to correct domain if host header does not match domain
# Prevents DNS rebinding attacks
enforce_domain = true

root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana/
serve_from_sub_path = true

I am now getting 502 bad gateway

Thanks

You defined that Grafana is running on localhost:3951. But keep in mind that each container has own network namespace = own localhost, which is not related to localhost of other containers by default (unless you configure container to use host network).

But it looks like you are on windows, so probably your Docker host is only VM in your Windows Host OS. So that “host network” can be more complicated.

Generally, make sure that upstream:

      upstream grafana {
        server localhost:3951;
      }

is reachable from the nginx container. If Grafana is also container, then I would use docker network.

ok so just for POC I spun up both grafana and nginx as containers using following docker compose

# Use root/example as user/password credentials
version: "3.1"

services:
  nginx:
    image: nginx:alpine
    ports:
      - target: 80
        host_ip: 127.0.0.1
        published: "8080"
        protocol: tcp
        mode: host
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    networks:
      - nginx-net

  grafana:
    image: grafana/grafana:9.5.5
    environment:
      - GF_AUTH_ANONYMOUS_ENABLED=true
      - GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
      - GF_USERS_DEFAULT_THEME=light
      - GF_LOG_MODE=console
      - GF_LOG_LEVEL=critical
    ports:
      - target: 3000
        host_ip: 127.0.0.1
        published: "3955"
        protocol: tcp
        mode: host
    networks:
      - nginx-net
networks:
  nginx-net:

I still get the same error. Let me chew on this a bit. I am using podman rather than docker but should not be too different. thanks!

This should work with that docker compose:

      upstream grafana {
        server grafana:3000;
      }

There is no need to publish any Grafana port or use host network for Grafana container.

1 Like

thanks, I think I see what the issue might be.

connect() failed (111: Connection refused) while connecting to upstream

Ok now things are getting clearer