Take it with a grain of salt

Load Balancing Tomcat Menggunakan Nginx

Posted on

Apa itu load balancing? Mengutip dari situs nginx: Load balancing refers to efficiently distributing incoming network traffic across a group of backend servers, also known as a server farm or server pool. Selengkapnya klik disini

Berikut ini adalah contoh load balancing aplikasi tomcat: Skenario:

Berikut ini adalah konfigurasi nginxnya:

# Desc: Load balancing aplikasi servlet tomcat, menggunakan nginx
# Author: Sumarsono
# Create date: 2019-01-08
# Tested: Yes

proxy_cache_path /tmp/NGINX_cache/ keys_zone=backcache:10m;

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;

upstream tomcat {
    # Use IP Hash for session persistence

    # List of Tomcat application servers
    #server+app tomcat 1
    #server+app tomcat 2

server {
    listen 80;

    # Load balance requests for / across Tomcat application servers
    location / {
        proxy_pass http://tomcat;
        proxy_cache backcache;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    # WebSocket configuration
    location /wstunnel/ {
        proxy_pass https://tomcat;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;