Home > Net >  Nginx on Docker Unable to reach site
Nginx on Docker Unable to reach site

Time:09-20

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 go to 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?

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

server {
    listen       80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

CodePudding user response:

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
  • Related