How to configure nginx as load balancer for CJP

Article ID:360002435972
2 minute readKnowledge base

Issue

  • How to configure nginx as load balancer for CJP

  • Is possible to use nginx as load balancer for CJP HA?

Environment

Resolution

The recommended configuration uses HAProxy and is described here in HA Configuration, we do not have a recommended configuration for nginx because only the commercial version of nginx allow checking an URL as a health check beat, see this link for more info http-health-check so if you have the commercial version of nginx you can use this configuration file

upstream backend  {
        ip_hash;
        server cje1.example.com:8181;
        server cje2.example.com:8282;
}

match server_primary {
        status 200-399;
        body !~ "primary";
}


server {
  listen 80;

  access_log /var/log/nginx/access.log;

  location / {
    proxy_pass https://backend;
    health_check uri=/ha/health-check match=server_primary interval=30s fails=3 passes=2;
    proxy_set_header Host $host;
    proxy_redirect off;

    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;

    client_max_body_size 250m;
    client_body_buffer_size 128k;

    proxy_connect_timeout 90;
    proxy_send_timeout 300;
    proxy_read_timeout 300;

    proxy_buffering off;

    proxy_temp_file_write_size 64k;
  }
}

if not you have to use something like this could work but the primary should be always the same if both servers are up

upstream backend  {
        ip_hash;
        server cje1.example.com:8181 max_fails=3 fail_timeout=30s weight=100;
        server cje2.example.com:8282 max_fails=3 fail_timeout=30s backup;
}

server {
  listen 80;

  access_log /var/log/nginx/access.log;

  location / {
    proxy_pass https://backend;
    proxy_set_header Host $host;
    proxy_redirect off;

    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;

    client_max_body_size 250m;
    client_body_buffer_size 128k;

    proxy_connect_timeout 90;
    proxy_send_timeout 300;
    proxy_read_timeout 300;

    proxy_buffering off;

    proxy_temp_file_write_size 64k;
  }
}

or if your version of nginx does not support backup directive this one trick the round robin to redirect always to the primary if it is available

upstream backend  {
        ip_hash;
        server cje1.example.com:8181 max_fails=3 fail_timeout=30s weight=100;
        server cje2.example.com:8282 max_fails=3 fail_timeout=30s;
}

server {
  listen 80;

  access_log /var/log/nginx/access.log;

  location / {
    proxy_pass https://backend;
    proxy_set_header Host $host;
    proxy_redirect off;

    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;

    client_max_body_size 250m;
    client_body_buffer_size 128k;

    proxy_connect_timeout 90;
    proxy_send_timeout 300;
    proxy_read_timeout 300;

    proxy_buffering off;

    proxy_temp_file_write_size 64k;
  }
}

Tested product/plugin versions