Understanding Trust Proxy in Jooby: A Comprehensive Guide

Understanding Trust Proxy in Jooby

Introduction

The Trust Proxy feature in Jooby is essential for applications that operate behind proxies, such as load balancers or reverse proxies. This feature helps the application understand the original client's information, such as their IP address, when requests are forwarded through these intermediaries.

Key Concepts

  • Proxy: An intermediary server that forwards requests from clients to other servers. Common examples include load balancers and content delivery networks (CDNs).
  • Client IP Address: The original IP address of the user making a request. This information is often lost when a request passes through a proxy.
  • Trust Proxy: A configuration setting that tells Jooby to trust the information provided by the proxy and to use it to determine the client’s real IP address.

When to Use Trust Proxy

You should enable Trust Proxy if:

  • Your application is deployed behind one or more proxies.
  • You need to accurately log client IPs for analytics, security, or customization.

How to Enable Trust Proxy in Jooby

To enable Trust Proxy in a Jooby application, you can set it in the application configuration. Here’s a simple example:

import io.jooby.Jooby;

public class App extends Jooby {
    {
        // Enable trust proxy
        trustProxy();
    }
}

You can also configure it to trust specific proxies by providing their IP addresses.

Example

Imagine you have a web application running behind a reverse proxy. When a user accesses your application, the proxy forwards the request. Without Trust Proxy enabled, your application would only see the proxy's IP address. By enabling Trust Proxy, Jooby will look for the X-Forwarded-For header, which contains the original client's IP, allowing you to log or use that information.

Conclusion

Using the Trust Proxy feature in Jooby is critical for applications that need accurate client information when operating behind proxies. By configuring Trust Proxy correctly, developers can ensure that their applications handle requests properly and maintain accurate records of client activity.