رفتن به محتوا
بازگشت به وبلاگ
لینوکس

Nginx به عنوان Reverse Proxy: توازن بار و SSL Termination

پیکربندی Nginx برای Reverse Proxy، توازن بار چند Backend و SSL Termination با Let's Encrypt در محیط تولیدی.

تیر ۱۴۰۴
۱۱ دقیقه مطالعه

Nginx Reverse Proxy با SSL و Load Balancing

Nginx پرکاربردترین Reverse Proxy/Load Balancer است. این یک راه‌اندازی آماده برای تولید با SSL از Let's Encrypt را پوشش می‌دهد.

نصب

BASH
apt install nginx certbot python3-certbot-nginx

Reverse Proxy پایه

NGINX
server {
    listen 443 ssl http2;
    server_name app.example.com;

    ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;

    location / {
        proxy_pass http://127.0.0.1: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;
    }
}

Load Balancing بین چند Backend

NGINX
upstream backend {
    least_conn;
    server 10.0.0.10:3000 weight=3;
    server 10.0.0.11:3000 weight=2;
    server 10.0.0.12:3000 weight=1 backup;
}

محدودیت نرخ

NGINX
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;

location /api/ {
    limit_req zone=api burst=20 nodelay;
}

گواهی Let's Encrypt

BASH
certbot --nginx -d app.example.com
certbot renew --dry-run

هدرهای امنیتی

NGINX
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options SAMEORIGIN;