If you wish to put Nginx in front of Go HTTP server you can use example below. At the end you can access the API on either 6000 port (Nginx) or 8080 port (Go).


Structure


.
├── cmd
│   └── server.go
├── docker
│   └── dev
│   ├── docker-compose.yaml
│   ├── go
│   │   └── Dockerfile
│   └── nginx
│   ├── Dockerfile
│   └── app.conf
├── go.mod
└── go.sum

Files


cmd/server.go


package main

import (
"fmt"
"log"
"net/http"

"github.com/google/uuid"
)

func handler(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(uuid.New().String()))
}

func main() {
http.HandleFunc("/", handler)
fmt.Println("Server is running ...")
log.Fatal(http.ListenAndServe(":8080", nil))
}

docker/dev/docker-compose.yaml


version: "3.4"

services:

football_api_go:
build:
context: "../.."
dockerfile: "docker/dev/go/Dockerfile"
hostname: "football-api-go"

football_api_nginx:
build:
context: "../.."
dockerfile: "docker/dev/nginx/Dockerfile"
hostname: "football-api-nginx"
ports:
- "6000:80"
depends_on:
- "football_api_go"

docker/dev/go/Dockerfile


#
# STAGE 1: prepare
#
FROM golang:1.13.1-alpine3.10 as prepare

WORKDIR /source

COPY go.mod .
COPY go.sum .

RUN go mod download

#
# STAGE 2: build
#
FROM prepare AS build

COPY . .

RUN CGO_ENABLED=0 go build -ldflags "-s -w" -o bin/server -v cmd/server.go

#
# STAGE 3: run
#
FROM scratch as run

COPY --from=build /source/bin/server /server

EXPOSE 8080

ENTRYPOINT ["/server"]

docker/dev/nginx/Dockerfile


FROM nginx:1.15.8-alpine

COPY docker/dev/nginx/app.conf /etc/nginx/conf.d/default.conf

EXPOSE 80

docker/dev/nginx/app.conf


server {
listen 80 default_server;

location / {
proxy_set_header X-Request-Id $request_id;
proxy_set_header Host $host;

proxy_pass http://football_api_go:8080/;
}
}

go.mod


module football-api

go 1.13

require github.com/google/uuid v1.1.1 // indirect

go.sum


github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

Build


Docker


$ docker-compose -f docker/dev/docker-compose.yaml up --build

Building football_api_go
Successfully built 74e284b2da5c
Successfully tagged dev_football_api_go:latest

Building football_api_nginx
Successfully built 12eabebd6b5f
Successfully tagged dev_football_api_nginx:latest

Recreating dev_football_api_go_1 ... done
Recreating dev_football_api_nginx_1 ... done

Attaching to dev_football_api_go_1, dev_football_api_nginx_1
football_api_go_1 | Server is running ...

$ curl -i http://0.0.0.0:6000
HTTP/1.1 200 OK
Server: nginx/1.15.8
Date: Thu, 26 Dec 2019 15:13:12 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 36
Connection: keep-alive

96d16744-c128-48ce-b9ff-5ae2fc0e3f05

Host OS


$ go build -ldflags "-s -w" -o bin/server -v cmd/server.go
command-line-arguments

$ bin/server
Server is running ...

$ curl -i http://0.0.0.0:8080
HTTP/1.1 200 OK
Date: Thu, 26 Dec 2019 15:16:07 GMT
Content-Length: 36
Content-Type: text/plain; charset=utf-8

cecaabd0-d224-45cc-91c2-5c77f9627611