Nginx برای فینچ

Nginx به عنوان یک reverse proxy جلوی برنامه فینچ قرار می‌گیرد. این کار عملکرد SSL، فایل‌های استاتیک و load balancing را ممکن می‌سازد.

مفهوم Reverse Proxy

Client → Nginx (پورت 80/443) → Finch (پورت 8080)

Nginx درخواست‌ها را می‌پذیرد، SSL را پردازش می‌کند و آن‌ها را به Finch که در پشت اجرا می‌شود ارسال می‌کند.

پیکربندی Nginx

upstream finch_app {
    server finch:8080;
}

server {
    listen 80;
    server_name example.com www.example.com;

    # فایل‌های استاتیک مستقیماً سرو می‌شوند
    location /public/ {
        alias /app/public/;
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    # همه چیز دیگر به فینچ می‌رود
    location / {
        proxy_pass http://finch_app;
        proxy_set_header Host \$host;
        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;

        # پشتیبانی از WebSocket
        proxy_http_version 1.1;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

پشتیبانی از HTTPS

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location / {
        proxy_pass http://finch_app;
        proxy_set_header X-Forwarded-Proto https;
        # ... بقیه header ها
    }
}

# هدایت HTTP به HTTPS
server {
    listen 80;
    server_name example.com;
    return 301 https://\$host\$request_uri;
}

دستورالعمل try_files

برای برنامه‌های Single Page Application از try_files استفاده کنید:

location / {
    try_files \$uri \$uri/ @finch;
}

location @finch {
    proxy_pass http://finch_app;
}