Sign inSign up

colakoltan/nginx-reverse-proxy

By colakoltan

•Updated almost 3 years ago

Preconfigured nginx:stable-alpine image for enabling reverse proxy via /api path with rate limiting

Image
1

489

colakoltan/nginx-reverse-proxy repository overview

⁠Introduction

This image contains stable-alpine tag of nginx official image with custom default config with templating.

⁠How to use with your frontend application?

⁠Bundle frontend application and copy frontend build output to nginx folder (Example Dockerfile)
# Author: [email protected] (January 2024)

#Docker image and stage naming
FROM node:lts-alpine AS build-and-test-stage

# Set working directory 
WORKDIR /app
# Copy package and package-lock.json to working directory
COPY package*.json /app/
# Install Dependencies (create node_modules folder)
RUN npm install
# Copy source code to working directory
COPY ./ /app/
# Run eslint tests (eslint ./src)  -- Optional
# RUN npm run lint 
# Build optimized build (HTML-JS only output)
RUN npm run build

# Nginx as a web server image and stage naming
FROM  colakoltan/nginx-reverse-proxy:stable-alpine AS deploy-stage
# Copy optimized html build output to nginx html folder
#Update 'dist' path with your build output if needed, for example with webpack builds it will be 'public'
COPY --from=build-and-test-stage /app/dist/ /usr/share/nginx/html/ 
⁠Deploy to Azure
  • Build image and push it to Azure Container Registry
  • Create new App Service with this image and give environment variables in Configuration section
⁠Deploy to AWS
  • Build image and push it AWS Elastic Container Registry
  • Create new App Runner with this image and give environment variables
⁠Deploy to GCloud
  • Build image and push it to Artifact Registryor Container Registry
  • Create new Cloud Run application in Container(s)/Environment Variables and give environment variables
⁠Run it Locally
  • Create dockerfile in your project folder from example

  • Start your docker engine and run docker build -t my-local-frontend .

  • After that if you want to run it

    docker run -e NGINX_WEB_SERVER_NAME=nginx_web_server_name -e NGINX_REVERSE_PROXY_HOSTNAME=reverse-proxy.com -e NGINX_REVERSE_PROXY_API_LINK=http://backend-service:8000 -p 8080:80 my-local-frontend

⁠Default Configs

⁠HTTP Configuration
...
    # API Limit 5RequestPerSecond
    limit_req_zone $binary_remote_addr zone=api_limit_5_request_per_second:10m rate=5r/s;
...
⁠Server configuration
        server {
        root /usr/share/nginx/html;
        index index.html index.htm;
        listen 80;
        listen [::]:80;
        server_name ${NGINX_WEB_SERVER_NAME};

        location = /api {
            # Redirect all /api requests to /api/
            return 302 /api/;
        }

        location /api/ {
            # Enforces rate limiting based on the defined zone (User IP Address)
            # The burst parameter allows for a burst of up to 10 requests
            # before the rate limiting takes effect.
            # The nodelay parameter ensures that exceeding the limit results in an immediate rejection.
            limit_req zone=api_limit_5_request_per_second burst=10 nodelay; #Apply limit
            limit_req_log_level warn; # Log rejected requests
            limit_req_status 429; # HTTP Too many requests

            # Reverse proxy configuration
            proxy_ssl_server_name on;
            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-Port $server_port;
            proxy_set_header Authorization $http_authorization;
            proxy_set_header Cookie $http_cookie;
            proxy_pass_request_headers on;

            # Reverse Proxy To
            proxy_set_header Host ${NGINX_REVERSE_PROXY_HOSTNAME};
            proxy_pass ${NGINX_REVERSE_PROXY_API_LINK};
        }

        location / {
            # First attempt to serve request as file, then
            try_files $uri $uri/ /index.html;
        }
    }

⁠Configurable Runtime Environment Variables

Environment VariableRequiredDefault ValueExample ValueDocker Run Command
NGINX_WEB_SERVER_NAME✓NULLyour-frontend-application.com-e NGINX_WEB_SERVER_NAME=your-frontend-application.com
NGINX_REVERSE_PROXY_HOSTNAME✓NULLyour-backend-application.com-e NGINX_REVERSE_PROXY_HOSTNAME=your-backend-application.com
NGINX_REVERSE_PROXY_API_LINK✓NULLhttps://your-backend-application.com/api/-e NGINX_REVERSE_PROXY_HOSTNAME=https://your-backend-application.com/api/

⁠Contact

For any questions please don't hesitate to communicate with

Tag summary

Content type

Image

Digest

sha256:4a3e99704…

Size

16.1 MB

Last updated

almost 3 years ago

docker pull colakoltan/nginx-reverse-proxy