[SOLVED] Nginx on Docker Unable to reach site

Issue

This Content is from Stack Overflow. Question asked by JianYA

I have this docker compose file that I added an nginx section to:

version: '3.4'

services:
  rp:
    image: nginx:latest
    ports:
      - 8120:80
    restart: always
    volumes:
      - ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
     - sdr_filestore.api

  sdr_filestore.api:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://+
    image: ${DOCKER_REGISTRY-}sdrfilestoreapi
    build:
      context: .
      dockerfile: SDR_Filestore.Api/Dockerfile
    networks: 
    - dev
    ports:
    - 59521:80

This is the nginx.conf file:

events {

}
http {

    server {
        listen 80;
        listen [::]:80;

        server_name localhost;
        server_tokens off;
    }

}

When I run docker-compose, it compiles and runs fine. However, when I run http://localhost:8120, I get this error:

This site can’t be reached localhost refused to connect.

I then tried another method which is to just run the command straight from the Nginx website:

docker run –name mynginx1 -p 8120:80 -d nginx

This also runs fine and when I go to http://localhost:8120, I get the nginx page. What did I configure wrong in my first docker compose file?



Solution

Don’t overwrite nginx.conf with your volume. Instead do the following:

volumes:
- ./nginx/conf/default.conf:/etc/nginx/conf.d/default.conf:ro

where default.conf is:

server {
        listen 80;
        listen [::]:80;

        server_name localhost;
        server_tokens off;
    }

If you want to see why, check the contents of nginx.conf and default.conf inside the original image:

docker run --rm nginx cat /etc/nginx/nginx.conf

docker run --rm nginx cat /etc/nginx/conf.d/default.conf


This Question was asked in StackOverflow by JianYA and Answered by Mihai It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?