> For the complete documentation index, see [llms.txt](/llms.txt). This page's section is indexed at [/enterprise/llms.txt](/enterprise/llms.txt).

# NGINX Load Balancing

NGINX acts as a reverse proxy and load balancer for Browserless Docker containers, providing SSL termination, traffic distribution across multiple instances, and automatic failover through health monitoring.

- A Browserless Enterprise license
- NGINX installed on your infrastructure
- Multiple Browserless Docker containers running (see the [Docker Configuration Reference](/enterprise/docker/config))

## How NGINX Works as a Load Balancer

NGINX acts as a reverse proxy that sits between your clients and Browserless instances. When a request arrives, NGINX:

1. **Receives the incoming connection** - Accepts HTTP/HTTPS requests and WebSocket connections from clients
2. **Routes to a backend** - Selects one of the healthy Browserless instances based on the configured load balancing algorithm (e.g., least connections, round-robin)
3. **Forwards the request** - Proxies the request to the selected Browserless container, maintaining WebSocket connections for browser automation
4. **Handles failures** - If a backend instance fails or returns an error, automatically retries the request on another healthy instance
5. **Returns the response** - Sends the backend's response back to the client

This architecture allows you to scale horizontally by adding more Browserless containers without changing your client code, while NGINX ensures traffic is distributed efficiently and failures are handled gracefully.

## Why Use NGINX with Browserless?

- **Load Balancing**: Distribute traffic across multiple Browserless containers
- **SSL Termination**: Handle HTTPS certificates at the proxy level
- **Health Checks**: Automatically retry failed requests to healthy instances
- **WebSocket Support**: Properly handle WebSocket connections for Puppeteer and Playwright
- **High Availability**: Prevent single points of failure

## Docker Compose Setup

Here's a complete example using Docker Compose with NGINX load balancing two Browserless instances:

### docker-compose.yml

```yaml
version: '3.8'

services:
  nginx:
    image: nginx:alpine
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
    ports:
      - "80:80"
    depends_on:
      - browserless_one
      - browserless_two
    restart: unless-stopped

  browserless_one:

image: registry.browserless.io/browserless/browserless/enterprise:latest
    environment:
      - TOKEN=your-token-here
      - CONCURRENT=10
    restart: unless-stopped

  browserless_two:
    image: registry.browserless.io/browserless/browserless/enterprise:latest
    environment:
      - TOKEN=your-token-here
      - CONCURRENT=10
    restart: unless-stopped
```

### nginx.conf

```nginx
upstream browserless {
  least_conn;
  server browserless_one:3000;
  server browserless_two:3000;
}

server {
  listen 80;

  location / {
    proxy_pass http://browserless;
    proxy_next_upstream error timeout http_500 http_503 http_429 non_idempotent;

    # WebSocket support
    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;

    # Extended timeouts for long-running browser operations
    proxy_connect_timeout 900;
    proxy_send_timeout 900;
    proxy_read_timeout 900;
    send_timeout 900;
  }
}
```

## Configuration Explanation

### Upstream Block

The `upstream` block defines the backend Browserless instances:

- `least_conn`: Routes requests to the server with the fewest active connections
- `server browserless_one:3000`: References the Docker service name and internal port
- Multiple servers provide redundancy and load distribution

### Server and Location Blocks

The `server` and `location` blocks handle incoming requests:

- `listen 80`: Listens for HTTP traffic on port 80
- `proxy_pass http://browserless`: Routes requests to the upstream backend pool
- `proxy_next_upstream`: Automatically retries failed requests (errors, timeouts, 500/503/429 responses) on other servers
- `proxy_http_version 1.1`: Required for WebSocket support
- `Upgrade` and `Connection` headers: Essential for WebSocket connections used by Puppeteer and Playwright
- Extended timeouts (900 seconds): Accommodate long-running browser operations like scraping or PDF generation

## Starting the Setup

1. Create the `nginx.conf` file with the configuration above
2. Create the `docker-compose.yml` file
3. Start the services:

```bash
docker-compose up
```

Your browserless instances will be available at `http://localhost` with automatic load balancing.

## Advanced Configuration

### Health Checks

Enable health checks in Browserless to make load balancing more intelligent. When CPU or memory usage is high, Browserless will reject requests, causing NGINX to automatically route traffic to healthier instances:

```yaml
# docker-compose.yml
browserless_one:
  image: registry.browserless.io/browserless/browserless/enterprise:latest
  environment:
    - TOKEN=your-token-here
    - CONCURRENT=10
    - HEALTH=true

browserless_two:
  image: registry.browserless.io/browserless/browserless/enterprise:latest
  environment:
    - TOKEN=your-token-here
    - CONCURRENT=10
    - HEALTH=true
```

### SSL/HTTPS Configuration

For production deployments, add SSL support. Update your `nginx.conf` to listen on port 443 and include SSL certificates:

```nginx
upstream browserless {
  least_conn;
  server browserless_one:3000;
  server browserless_two:3000;
}

server {
  listen 443 ssl;
  server_name your-domain.com;

  ssl_certificate /path/to/your/certificate.pem;
  ssl_certificate_key /path/to/your/private-key.pem;
  ssl_protocols TLSv1.2 TLSv1.3;

  location / {
    proxy_pass http://browserless;
    proxy_next_upstream error timeout http_500 http_503 http_429 non_idempotent;

    # WebSocket support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    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;
    proxy_cache_bypass $http_upgrade;

    # Extended timeouts
    proxy_connect_timeout 900;
    proxy_send_timeout 900;
    proxy_read_timeout 900;
    send_timeout 900;
  }
}

# Optional: Redirect HTTP to HTTPS
server {
  listen 80;
  server_name your-domain.com;
  return 301 https://$server_name$request_uri;
}
```

Also update your `docker-compose.yml` to expose port 443:

```yaml
nginx:
  image: nginx:alpine
  volumes:
    - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
    - /path/to/certificates:/etc/nginx/certs:ro
  ports:
    - "80:80"
    - "443:443"
  depends_on:
    - browserless_one
    - browserless_two
  restart: unless-stopped
```

### Browserless Proxy Awareness

When using NGINX in front of Browserless, configure Browserless to be aware of the proxy for proper link generation:

```yaml
# docker-compose.yml
browserless_one:
  image: registry.browserless.io/browserless/browserless/enterprise:latest
  environment:
    - TOKEN=your-token-here
    - CONCURRENT=10
    - EXTERNAL=https://your-domain.com
```

The `EXTERNAL` variable should be set to the fully-qualified URL (including protocol and port if non-standard) where your Browserless instances are accessible externally. See the [Docker Configuration](/enterprise/docker/config#external-address-configuration) documentation for more details.

### Session Affinity for Reconnect and LiveURL

Browserless sessions are stateful. A browser running on Instance A cannot be reconnected from Instance B. Both `Browserless.reconnect` and `Browserless.liveURL` return URLs that contain the originating instance's address. Without the `EXTERNAL` env var set, these URLs contain `localhost:3000`, which is unreachable by end users or external clients. If `EXTERNAL` is set to the shared load balancer address, reconnect requests may route to the wrong instance.

Solutions:
- Set `EXTERNAL` to each instance's direct address (e.g., `EXTERNAL=http://browserless-1:3000` for instance 1, `EXTERNAL=http://browserless-2:3000` for instance 2). Reconnect and LiveURL links then bypass the load balancer and reach the correct instance.
- Use the returned URL as-is. It already points to the correct instance.

For initial connections, use round-robin or least-connections routing. For reconnections and LiveURL links, route the client directly to the URL returned by Browserless.

### /pressure Endpoint for Health-Aware Routing

The `/pressure` GET endpoint returns real-time capacity data for the instance:

```bash
curl http://localhost:3000/pressure?token=YOUR_TOKEN
```

Response:
```json
{
  "pressure": {
    "cpu": 45,
    "date": 1711468800000,
    "isAvailable": true,
    "maxConcurrent": 10,
    "maxQueued": 10,
    "memory": 62,
    "message": "",
    "queued": 0,
    "reason": "",
    "recentlyRejected": 0,
    "running": 3
  }
}
```

| Field | Type | Description |
|-------|------|-------------|
| `cpu` | number or null | CPU usage percentage |
| `memory` | number or null | Memory usage percentage |
| `isAvailable` | boolean | Whether the instance accepts new sessions |
| `maxConcurrent` | number | Maximum concurrent browser sessions |
| `maxQueued` | number | Maximum queued requests |
| `running` | number | Currently running sessions |
| `queued` | number | Currently queued requests |
| `reason` | string | Why the instance is unavailable: `full`, `cpu`, `memory`, or empty |
| `recentlyRejected` | number | Recent rejected connection count |

Use `isAvailable` in your load balancer health check to route traffic away from overloaded instances.

### Scaling to More Instances

To add more Browserless instances, add more services to your `docker-compose.yml` and update the nginx upstream block:

```yaml
# docker-compose.yml
services:
  nginx:
    # ... nginx config
    depends_on:
      - browserless_one
      - browserless_two
      - browserless_three
      - browserless_four

  browserless_one:
    # ... config

  browserless_two:
    # ... config

  browserless_three:
    # ... config

  browserless_four:
    # ... config
```

```nginx
# nginx.conf
upstream browserless {
  least_conn;
  server browserless_one:3000;
  server browserless_two:3000;
  server browserless_three:3000;
  server browserless_four:3000;
}
```

## Testing Your Setup

You can test your load-balanced setup by connecting with Puppeteer:

```javascript
const puppeteer = require('puppeteer');

const browser = await puppeteer.connect({
  browserWSEndpoint: 'ws://localhost:80?token=your-token-here'
});

const page = await browser.newPage();
await page.goto('https://example.com');
const title = await page.title();
console.log(title);

await browser.close();
```

The requests will be automatically distributed across your Browserless instances by NGINX.

## FAQ & Troubleshooting

<details>
<summary>How do I get an Enterprise license?</summary>

Contact [sales@browserless.io](mailto:sales@browserless.io) or visit the [pricing page](https://browserless.io/pricing) for Enterprise plan details.

</details>

<details>
<summary>Can I run Browserless on my own infrastructure?</summary>

Yes. Enterprise customers can self-host using the Docker image. See [Docker deployment](/enterprise/docker/enterprise-image) for setup instructions.

</details>

## Next steps

  <a href="/enterprise/docker/enterprise-image" className="next-step-card-link">
    
      <h3 className="next-step-card-title">Docker deployment</h3>
      self-host Browserless with Docker
    
  </a>
  <a href="/enterprise/launch-options" className="next-step-card-link">
    
      <h3 className="next-step-card-title">Launch parameters</h3>
      configure your Enterprise deployment
    
  </a>