0
votes

I'm having trouble trying to get the following to work in Docker

What I want is that when the user requests http://localhost/api then NGINX reverse proxies to my .Net Core API running in another container.

Container Host: Windows

Container 1: NGINX

dockerfile

FROM nginx

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

nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {

    server {
        location /api1 {
            proxy_pass http://api;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

Container 2: .Net Core API

Dead simple - API exposed on port 80 in the container

Then there is the docker-compose.yml

docker-compose.yml

version: '3'

services:
  api1:
    image: api1
    build:
      context: ./Api1
      dockerfile: Dockerfile
    ports:
      - "5010:80"

  nginx:
    image: vc-nginx
    build:
      context: ./infra/nginx
      dockerfile: Dockerfile
    ports:
      - "5000:80"

Reading the Docker documentation it states:

Links allow you to define extra aliases by which a service is reachable from another service. They are not required to enable services to communicate - by default, any service can reach any other service at that service’s name.

So as my API service is called api1, I've simply referenced this in the nginx.conf file as part of the reverse proxy configuration:

proxy_pass http://api1;

Something is wrong as when I enter http:\\localhost\api I get a 404 error.

Is there a way to fix this?

1

1 Answers

2
votes

The problem is the nginx location configuration.

The 404 error is right, because your configuration is proxying request from http://localhost/api/some-resource to a missing resource, because your mapping is for /api1 path and you're asking for /api.

So you should only change the location to /api and it will work.

Keep in mind that requests to http://localhost/api will be proxied to http://api1/api (the path is kept). If your backend is configured to expose api with a prefixing path this is ok, otherwise you will receive another 404 (this time from your service). To avoid this you should rewrite the path before proxying the request with a rule like this:

# transform /api/some-resource/1 to /some-resource/1
rewrite    /api/(.*) /$1 break;