Bab 15: Integrasi Nginx dengan Backend – PHP, Node.js, Python, dan Load Balancing

Bab 15: Integrasi Nginx dengan Backend – PHP, Node.js, Python, dan Load Balancing

Nginx dapat berfungsi sebagai reverse proxy dan load balancer untuk berbagai backend, mulai dari PHP, Node.js, hingga Python WSGI. Integrasi ini memungkinkan aplikasi berjalan stabil, cepat, dan scalable.


1. PHP-FPM (FastCGI)

PHP-FPM memungkinkan Nginx menjalankan aplikasi PHP melalui FastCGI.

Contoh konfigurasi:

server {
    listen 80;
    server_name example.com;

    root /var/www/html/example;
    index index.php index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
  • fastcgi_pass → jalur socket PHP-FPM
  • try_files → fallback untuk file atau folder
  • fastcgi_param SCRIPT_FILENAME → path file PHP

Tips:

  • Gunakan socket jika server dan PHP-FPM berada di mesin yang sama
  • Gunakan IP:Port jika PHP-FPM di server terpisah

2. Node.js (Reverse Proxy)

Nginx dapat meneruskan request ke Node.js backend menggunakan proxy_pass.

Contoh konfigurasi:

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

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
  • Mendukung WebSocket (Upgrade dan Connection)
  • proxy_cache_bypass → bypass cache untuk request upgrade
  • Nginx bertindak sebagai reverse proxy dan load balancer

3. Python WSGI (uWSGI/Gunicorn)

Untuk aplikasi Python (Flask, Django) menggunakan WSGI server:

Contoh Gunicorn + Nginx:

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

    location / {
        proxy_pass http://127.0.0.1:8000;
        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;
    }
}
  • Gunicorn berjalan di port 8000
  • Nginx meneruskan request dan menambahkan proxy headers
  • Memudahkan load balancing dan SSL termination

4. Load Balancing Backend Apps

Nginx bisa mendistribusikan request ke beberapa backend menggunakan upstream.

Contoh Node.js Load Balancing:

upstream node_backend {
    server 127.0.0.1:3000;
    server 127.0.0.1:3001;
}

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

    location / {
        proxy_pass http://node_backend;
        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;
    }
}
  • Metode default: round-robin
  • Bisa diganti dengan least_conn atau ip_hash sesuai kebutuhan
  • Mendukung failover jika salah satu backend gagal

Contoh PHP-FPM Load Balancing:

upstream php_backend {
    server unix:/run/php/php8.1-fpm.sock;
    server 127.0.0.1:9000 backup;
}

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

    location ~ \.php$ {
        fastcgi_pass php_backend;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
  • Backend utama menggunakan socket
  • Backend cadangan (backup) untuk failover

5. Kesimpulan

Dengan integrasi backend Nginx, kamu dapat:

  1. Menjalankan aplikasi PHP dengan PHP-FPM
  2. Mengatur Node.js dan WebSocket melalui reverse proxy
  3. Menghubungkan Python WSGI (uWSGI/Gunicorn) dengan Nginx
  4. Mengatur load balancing untuk semua backend dengan upstream
  5. Menggabungkan SSL, caching, dan security headers untuk arsitektur aplikasi production-ready

Integrasi ini membuat Nginx menjadi gateway fleksibel untuk berbagai bahasa dan framework, sekaligus meningkatkan kecepatan, stabilitas, dan skalabilitas aplikasi.