dockernginxdocker-composesonarqubenginx-reverse-proxy

Nginx Reverse proxy to Sonarqube not working. Getting 500 error


Using docker-compose file spinned up 3 docker containers i.e nginx,sonarqube & postgres

docker-compose file below :

version: "3.3"

services:

sonarqube:
container_name: sonarqube_9
 image: sonarqube:9-community
 restart: always
 ports: 
    - "9000" 
 
 networks:
   - sonarnet
 depends_on:
    - db
 environment:
     SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
     SONAR_JDBC_USERNAME: sonar
     SONAR_JDBC_PASSWORD: sonar
 volumes:
   - /data/docker-volumes/sonarqube/conf:/opt/sonarqube/conf
   - /data/docker-volumes/sonarqube/data:/opt/sonarqube/data
   - /data/docker-volumes/sonarqube/extensions:/opt/sonarqube/extensions
   - /data/docker-volumes/sonarqube/bundled-plugins:/opt/sonarqube/lib/bundled-plugins


db:
  image: postgres:14
  restart: always
  ports:
    - 5432:5432
  networks:
    - sonarnet
  environment:
    POSTGRES_USER: sonar
    POSTGRES_PASSWORD: sonar
  volumes:
    - /data/docker-volumes/postgresql:/var/lib/postgresql
    - /data/docker-volumes/postgresql/data:/var/lib/postgresql/data

reverse_proxy:
  container_name: reverse_proxy
depends_on:
  - sonarqube
image: nginx:latest
networks:
  - sonarnet
ports:
  - 80:80
  - 443:443
restart: always
volumes:
    - /data/docker-volumes/nginx/conf:/etc/nginx/conf.d/
    
networks:

 sonarnet:
   name: sonarnet
  driver: bridge

.......................................

Inside nginx container inside default.config file for reverse proxy

server {

listen       80;
server_name  localhost:9000;

location / {

  proxy_pass http://127.0.0.1:9000;

}

}

When on browser trying to access http://localhost:80 we are getting error 500 on screen

in logs we are getting

User_Client: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36" "Request: GET / HTTP/1.1 Status: 502 Request_URI: / Host: localhost


Solution

  • localhost within nginx container refers to nginx itself. You need to use container name as proxy_pass, e.g. proxy_pass http://sonarqube_9:9000;.