├── server ├── go.mod ├── Dockerfile └── main.go ├── docker-compose.yml ├── README.md └── nginx.conf /server/go.mod: -------------------------------------------------------------------------------- 1 | module main 2 | 3 | go 1.16 4 | -------------------------------------------------------------------------------- /server/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.16-alpine 2 | 3 | WORKDIR /app 4 | COPY go.mod /app 5 | RUN go mod download 6 | 7 | COPY *.go ./ 8 | 9 | RUN go build -o /main 10 | 11 | CMD [ "/main" ] -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | services: 2 | nginx: 3 | image: nginx:alpine 4 | volumes: 5 | - ./nginx.conf:/etc/nginx/nginx.conf 6 | ports: 7 | - "8081:8080" 8 | server: 9 | build: 10 | context: server 11 | container_name: golang_app 12 | ports: 13 | - "8082:8082" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NGINX Cache basics 2 | 3 | Simple app to study about the basics for cache requests using NGINX. 4 | Common topics that is applied in this repo: 5 | - Reverse Proxy 6 | - Cache configurations 7 | - Bypass requests 8 | 9 | ### Run project 10 | This projects has a simple Golang app and a simple NGINX config. 11 | ``` 12 | docker-compose up 13 | ``` 14 | 15 | ### Test project 16 | to make requests for NGINX app 17 | 18 | ``` 19 | curl -X GET 'http://localhost:8081' 20 | ``` -------------------------------------------------------------------------------- /nginx.conf: -------------------------------------------------------------------------------- 1 | events{ 2 | worker_connections 1024; 3 | } 4 | 5 | http { 6 | proxy_cache_path /etc/nginx/cache keys_zone=mycache:10m; 7 | 8 | server { 9 | listen 8080; 10 | 11 | location / { 12 | proxy_pass http://server:8082; 13 | proxy_cache mycache; 14 | add_header X-Cache-Status $upstream_cache_status; 15 | proxy_cache_valid 200 10s; 16 | proxy_cache_key $host$uri$is_args$args; 17 | proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504; 18 | proxy_cache_bypass $arg_nocache; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /server/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "log" 6 | "net/http" 7 | "time" 8 | ) 9 | 10 | type Response struct { 11 | Message string `json:message` 12 | Datetime string `json:datetime` 13 | } 14 | 15 | func message(w http.ResponseWriter, req *http.Request) { 16 | loc, _ := time.LoadLocation("America/Sao_Paulo") 17 | currentTime := time.Now().In(loc) 18 | timeFormatted := currentTime.Format("2006-01-02 15:04:05") 19 | w.WriteHeader(http.StatusOK) 20 | w.Header().Set("Content-Type", "application/json") 21 | resp := &Response{ 22 | Message: "Hello world!", 23 | Datetime: timeFormatted, 24 | } 25 | j, _ := json.Marshal(resp) 26 | w.Write(j) 27 | } 28 | 29 | func main() { 30 | http.HandleFunc("/", message) 31 | http.ListenAndServe(":8082", nil) 32 | log.Print("Server is listening on port :8082") 33 | } 34 | --------------------------------------------------------------------------------