nginx reverse proxy is not working for asp.net core docker app

Viewed 544

I have asp.net web api application in docker container and it's working good with below commands,

 docker build -t hello-aspnetcore3 -f Api.Dockerfile .
 docker run -d -p 5000:5000 --name hello-aspnetcore3  hello-aspnetcore3

Browse to: http://localhost:5000/weatherforecast works perfect.

Now I am trying to use Nginx as reverse proxy and running in another container and here is nginx conf and container files,

nginx.conf

worker_processes 1;

events { worker_connections 1024; }

http {

sendfile on;

upstream web-api {
    server api:5000;
}

server {
    listen 80;
    server_name $hostname;
    location / {
        proxy_pass         http://web-api;
        proxy_redirect     off;
        proxy_http_version 1.1;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   X-Forwarded-Host $server_name;
    }
  }
}

Nginx.Dockerfile

FROM nginx:latest

COPY nginx.conf /etc/nginx/nginx.conf

and here is my docker compose file,

docker-compose.yml

version: "3.7"

services:

reverseproxy:
build:
  context: ./Nginx
  dockerfile: Nginx.Dockerfile
ports:
  - "80:80"
restart: always

api:
depends_on:
  - reverseproxy
build:
  context: ./HelloAspNetCore3.Api
  dockerfile: Api.Dockerfile
expose:
  - "5000"
restart: always

Building and running containers are fine docker-compose build and docker-compose up -d.

But when trying to browse http://localhost/weatherforecast, it's giving HTTP Error 404. The requested resource is not found. error. What's wrong here?

Note - when I browse using host ip address http://192.168.0.103/weatherforcast, then it's works fine.

Docker-Compose ps output is here....

enter image description here

0 Answers
Related