Preconfigured nginx:stable-alpine image for enabling reverse proxy via /api path with rate limiting
489
This image contains stable-alpine tag of nginx official image with custom default config with templating.
# 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/
Azure Container RegistryApp Service with this image and give environment variables in Configuration sectionAWS Elastic Container RegistryApp Runner with this image and give environment variablesArtifact Registryor Container RegistryCloud Run application in Container(s)/Environment Variables and give environment variablesCreate 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
...
# API Limit 5RequestPerSecond
limit_req_zone $binary_remote_addr zone=api_limit_5_request_per_second:10m rate=5r/s;
...
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;
}
}
| Environment Variable | Required | Default Value | Example Value | Docker Run Command |
|---|---|---|---|---|
NGINX_WEB_SERVER_NAME | ✓ | NULL | your-frontend-application.com | -e NGINX_WEB_SERVER_NAME=your-frontend-application.com |
NGINX_REVERSE_PROXY_HOSTNAME | ✓ | NULL | your-backend-application.com | -e NGINX_REVERSE_PROXY_HOSTNAME=your-backend-application.com |
NGINX_REVERSE_PROXY_API_LINK | ✓ | NULL | https://your-backend-application.com/api/ | -e NGINX_REVERSE_PROXY_HOSTNAME=https://your-backend-application.com/api/ |
For any questions please don't hesitate to communicate with
Content type
Image
Digest
sha256:4a3e99704…
Size
16.1 MB
Last updated
almost 3 years ago
docker pull colakoltan/nginx-reverse-proxy