Skip to main content

Command Palette

Search for a command to run...

Taming Traffic in Kubernetes: Setting Up NGINX as a Reverse Proxy

Updated
2 min read
M

Cloud and DevOps professional with a passion for automation, containers, and cloud-native practices, committed to sharing lessons from the trenches while always seeking new challenges. Combining hands-on expertise with an open mind, I write to demystify the complexities of DevOps and grow alongside the tech community.

Ever hosted a house party where guests end up lost and you’re stuck pointing them to the right room? That’s exactly how it feels juggling services inside a Kubernetes cluster - requests arrive, and chaos follows. But then I discovered NGINX: the digital party host in your K8s setup, greeting every request and guiding it straight to the right service, no mix-ups, no hallway traffic jams.

In this walkthrough, I’ll show you how to set up NGINX as a reverse proxy inside Kubernetes - so your microservices party runs smooth, requests never get lost, and you get to enjoy the music.

Why NGINX as a Reverse Proxy in Kubernetes?

  • Centralized Traffic Management: NGINX routes incoming requests to specific Kubernetes services based on URL, domain, or path.

  • SSL/TLS Termination: Offload encryption at the proxy, making your backend services simpler and more secure.

  • Load Balancing: NGINX distributes requests to replicas of backend pods, improving availability and scalability.

  • Security: Keep your internal services private and expose only NGINX, adding an extra shield against the outside world.

Hands-on: Setting Up NGINX Reverse Proxy in Kubernetes

1. Custom NGINX Config

Create a nginx.conf defining how requests are routed:

events { }

http {
    server {
        listen 8080;
        location /api/ {
            proxy_pass http://backend-svc:5000/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location / {
            proxy_pass http://frontend-svc:3000/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}
  • Change backend-svc and frontend-svc to your actual Kubernetes service names.

2. Containerize NGINX with Your Config

Dockerfile example:

FROM nginx:latest
COPY nginx.conf /etc/nginx/nginx.conf

Build and push this image to your registry.

3. Kubernetes Deployment and Service

nginx-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-reverse-proxy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-reverse-proxy
  template:
    metadata:
      labels:
        app: nginx-reverse-proxy
    spec:
      containers:
      - name: nginx
        image: your-nginx-image
        ports:
        - containerPort: 8080

nginx-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: nginx-reverse-proxy-svc
spec:
  selector:
    app: nginx-reverse-proxy
  ports:
    - port: 8080
      targetPort: 8080
  type: LoadBalancer

Apply both files via kubectl apply -f.

4. Test the Setup

Port-forward for local testing:

kubectl port-forward svc/nginx-reverse-proxy-svc 8080:8080

Now, accessing /api/ gets you to the backend; root / leads you to the frontend.

Bonus: When to Use NGINX vs. NGINX Ingress

  • For most production K8s clusters, you’ll use the NGINX Ingress Controller, which is a special version built to automatically read Kubernetes Ingress resources and manage routes for you.

  • The above approach is great for learning, custom needs, or situations where you want manual, fine-grained proxy control.