0Pricing
API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · درس

Nginx في البيئات الحاوية

انشروا Nginx وأديروه بكفاءة داخل حاويات Docker وعناقيد Kubernetes.

Nginx في البيئات الحاوية درس مجاني في API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Containerize Nginx?

Running Nginx in containers, like Docker, offers many benefits. It provides a consistent environment, making sure Nginx behaves the same way everywhere, from your laptop to production.

Key advantages include:

  • Portability: Move Nginx applications easily across different systems.
  • Isolation: Nginx runs in its own isolated environment, preventing conflicts.
  • Scalability: Quickly spin up new Nginx instances as traffic grows.
  • Resource Efficiency: Containers share the host OS kernel, using fewer resources than traditional VMs.

Your First Nginx Dockerfile

A Dockerfile is a script that contains instructions for building a Docker image. To containerize Nginx, we typically start with an official Nginx image and then add our custom configurations or static files.

Here's a basic Dockerfile to create an Nginx image:

FROM nginx:latest
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./html /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Running Nginx in Docker

Once you have a Dockerfile (and optionally, your nginx.conf and html files), you can build an image and run it.

First, build the image (name it my-nginx):

docker build -t my-nginx .

Then, run a container from your image, mapping port 8080 on your host to port 80 in the container:

docker run -d -p 8080:80 --name my-nginx-container my-nginx

Custom Nginx Config in Docker

You often need to use a custom nginx.conf. Instead of baking it into the image with COPY (as in the Dockerfile example), you can mount it as a volume. This allows you to change the config without rebuilding the image.

Here's how to mount a local ./custom-nginx.conf file into the container:

docker run -d -p 8080:80 \
  --name my-nginx-custom \
  -v ./custom-nginx.conf:/etc/nginx/nginx.conf \
  nginx:latest

Nginx with Docker Compose

For applications with multiple services (e.g., Nginx as a reverse proxy for a backend API), Docker Compose is invaluable. It lets you define and run multi-container Docker applications with a single YAML file.

This example sets up Nginx to proxy requests to a simple web service:

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
  app:
    image: httpd:alpine
    expose:
      - "8080"

Nginx and Kubernetes Basics

Kubernetes (K8s) is an open-source container orchestration platform. When deploying Nginx in a production environment, Kubernetes provides advanced features like:

  • Automated Deployment: Declarative configuration for desired state.
  • Scaling: Easily scale Nginx instances up or down.
  • Self-healing: Automatically restart or replace failed Nginx containers.
  • Load Balancing: Distribute traffic across multiple Nginx instances.

Key K8s concepts for Nginx include Pods, Deployments, and Services.

Deploying Nginx in Kubernetes

In Kubernetes, you define your applications using YAML files. A Deployment object manages a set of identical Pods, ensuring a specified number of Nginx instances are always running.

Here's a basic Kubernetes Deployment for Nginx:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

Exposing Nginx with K8s Services

A Kubernetes Service is an abstract way to expose an application running on a set of Pods. It provides a stable IP address and DNS name for your Nginx deployment, even if Pods are replaced.

Common Service types for Nginx include NodePort (exposes Nginx on a port across all cluster nodes) and LoadBalancer (integrates with cloud load balancers).

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: NodePort

Nginx Config with ConfigMaps

To manage Nginx configuration in Kubernetes, ConfigMaps are the preferred way. A ConfigMap stores non-confidential data in key-value pairs, which can then be mounted as files into your Nginx Pods.

This allows you to update Nginx configuration without rebuilding your Docker image or modifying the Deployment YAML directly.

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  nginx.conf: |
    events { worker_connections 1024; }
    http {
      server {
        listen 80;
        location / {
          return 200 'Hello from Nginx in K8s!';
        }
      }
    }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-configmap-deployment
spec:
  selector:
    matchLabels:
      app: nginx-configmap
  template:
    metadata:
      labels:
        app: nginx-configmap
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        volumeMounts:
        - name: nginx-config-volume
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
      volumes:
      - name: nginx-config-volume
        configMap:
          name: nginx-config

Check Your Container Knowledge

Which of the following are valid ways to manage custom Nginx configuration files when running Nginx in a Docker container or Kubernetes cluster?

Recap: Nginx in Containers

We've explored how to deploy and manage Nginx effectively within containerized environments, focusing on Docker and Kubernetes.

  • Docker: Provides isolation and portability, allowing Nginx to run consistently.
  • Dockerfile: Used to build custom Nginx images, including custom configurations.
  • Docker Compose: Orchestrates multi-container Nginx setups for development.
  • Kubernetes: Offers robust orchestration for production, with features like scaling and self-healing.
  • Deployments & Services: Core K8s objects to manage and expose Nginx Pods.
  • ConfigMaps: The standard K8s way to manage Nginx configuration dynamically.

Mastering these concepts is crucial for modern, scalable Nginx deployments!

الأسئلة الشائعة

هل درس «Nginx في البيئات الحاوية» مجاني؟

نعم — نص درس «Nginx في البيئات الحاوية» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)، انتقل إلى CoddyKit PRO. تتضمن دورة API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 4 دروس في المجموع.

ماذا ستتعلم في «Nginx في البيئات الحاوية»؟

انشروا Nginx وأديروه بكفاءة داخل حاويات Docker وعناقيد Kubernetes. تتمرن على API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)؟

لا تُشترط خبرة سابقة. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «Nginx في البيئات الحاوية»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) هذا؟

نعم. كل درس في API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. السجلات والمقاييس في Nginx
  2. ضبط أداء Nginx
  3. Nginx في البيئات الحاوية
  4. إعادة التحميل دون توقف واختبار الإعدادات
← العودة إلى API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)