NGINX.COM
Web Server Load Balancing with NGINX Plus

A version of this blog post originally appeared on the ScientiaMobile blog.

Editor – In April 2017, NGINX, Inc. launched the NGINX Plus Certified Module program for third‑party partners who build, deliver, and fully support modules for NGINX Plus platform, opening up additional features and application delivery management use cases for NGINX Plus customers. The functionality of an NGINX Certified Module is fully supported by the module vendor.

WURFL InFuze is a great example of extending application delivery management to dynamically support responsive web design for multiple device types. Below is a guest blog post from Ken Jones of ScientiaMobile about its new WURFL NGINX Plus Certified Module.

NGINX provides flexible, software‑based, high‑availability load balancing and an application delivery platform. And now with the WURFL InFuze Certified Module for NGINX Plus, you can quickly inject WURFL’s device intelligence into NGINX Plus’ application delivery logic.

NGINX Load Balancing Based on Smartphone, Tablet, or Other Device Form Factor

WURFL provides a number of options for load balancing criteria within NGINX Open Source or NGINX Plus. We can implement load balancing by operating system, browser type, or the price of the smartphone. In our following example (which will work for both NGINX Open Source and NGINX Plus), we load balance based on the device’s form factor.

To begin testing this scenario, you will need to edit the default NGINX configuration file, typically located at /etc/nginx/nginx.conf.

We separate traffic into three streams: smartphone, tablets, and other devices (such as desktop, smart TV, etc.). NGINX Plus can redirect those streams to specific servers.

# This is an example of how to configure NGINX to be used with WURFL Device Detection module.
#
# This configuration performs a redirection to different backends based on WURFL detection
# In this scenario we have three backend pools:
#
#  - One for SMARTPHONE devices: SmartphonePool (192.168.140.2x)
#  - One for TABLET devices: TabletPool (192.168.140.3x)
#  - One for any other type of client (desktop, smartTV, etc.): GeneralPurposePool (192.168.140.1x)
#
# Uncomment this line if you are using NGINX Plus or you compiled WURFL module with 
# 'configure --add-dynamic-module' (WURFL API version 1.8.1.0 or above / NGINX OSS 1.9.11 or above).
#load_module modules/ngx_http_wurfl_module.so;

worker_processes 1;

error_log logs/error.log info;
pid logs/nginx.pid;

events {
    worker_connections  512;
}

http {

    # the backends pool for requests from smartphones
    upstream SmartphonePool {
        server 192.168.140.20;
        server 192.168.140.21;
        server 192.168.140.22;
    }

    # the backends pool for requests from tablets
    upstream TabletPool {
        server 192.168.140.30;
        server 192.168.140.31;
        server 192.168.140.32;
    }

    # the backends pool for requests from other client types
    upstream GeneralPurposePool {
        server 192.168.140.10;
        server 192.168.140.11;
        server 192.168.140.12;
    }

    # WURFL root definition, one per config. User MUST specify this path for WURFL engine 
    # to start correctly.
    wurfl_root      /usr/share/wurfl/wurfl.zip;

    # Increase the variable hash size
    variables_hash_max_size 1024;
    variables_hash_bucket_size 1024;

    wurfl_cache_double_lru 10000,3000;

    # the WURFL virtual capability that determines if a device is a mobile phone
    wurfl_request_capability is_smartphone;
    wurfl_request_capability is_tablet;

    # Map to different upstream backends based on concatenation of is_smartphone and is_tablet 
    # (pipe separated). $backend_pool_name contains the upstream name based on is_phone value.

    map $wurfl_cap_is_smartphone|$wurfl_cap_is_tablet $backend_pool_name {
        true|false   "SmartphonePool";
        false|true   "TabletPool";
    # any other combination will redirect to GeneralPurposePool
        default      "GeneralPurposePool";
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            # hredirect request to the target upstream
            proxy_pass http://$backend_pool_name;
        }
    }
}

Note that the IP addresses and upstream block names are examples only. Change them to match your test environment.

To start a free 30-day trial of the WURFL InFuze NGINX Plus Certified Module, visit ScientiaMobile.

To try NGINX Plus, start your free 30-day trial today or contact us to discuss your use cases.

Hero image
Free O'Reilly eBook: The Complete NGINX Cookbook

Updated for 2024 – Your guide to everything NGINX



About The Author

Ken Jones

VP of Marketing

About F5 NGINX

F5, Inc. is the company behind NGINX, the popular open source project. We offer a suite of technologies for developing and delivering modern applications. Together with F5, our combined solution bridges the gap between NetOps and DevOps, with multi-cloud application services that span from code to customer.

Learn more at nginx.com or join the conversation by following @nginx on Twitter.