# Using keep-alive connections to improve performance

> Turn on HTTP keep-alive connections so browsers load your pages faster, and check that keep-alive is working.

Source: https://helpcenter.ruachost.com/developers/web-servers/using-keep-alive-connections-to-improve-performance/

**Keep‑Alive connections** allow a single TCP connection to handle multiple HTTP requests and responses. On **ruachost.com**, enabling keep‑alive improves website performance by reducing the overhead of repeatedly opening and closing connections.

## Why Use Keep‑Alive?

- **Faster page loads**: Multiple resources (CSS, JS, images) can be delivered over one connection.
- **Reduced server load**: Fewer TCP handshakes mean lower CPU and memory usage.
- **Better user experience**: Visitors see faster, smoother browsing.
- **Improved performance scores**: Tools like GTMetrix check for keep‑alive and penalize sites without it.

## How to Enable Keep‑Alive

### Method 1: Using `.htaccess`

Add the following lines to your site’s `.htaccess` file in the root directory:

```

    Header set Connection keep-alive

```

Save the file — keep‑alive is now enabled.

### Method 2: Apache Configuration (Unmanaged VPS/Dedicated Servers)

Edit the Apache configuration file (`httpd.conf`):

```
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
```

- `KeepAlive On` → enables persistent connections.
- `MaxKeepAliveRequests` → maximum number of requests per connection.
- `KeepAliveTimeout` → time (in seconds) the server waits for the next request.

Restart Apache:

```
sudo systemctl restart apache2
```

## How to Verify Keep‑Alive Is Enabled

1. Open your site in Chrome or Firefox.
2. Use **Developer Tools → Network tab**.
3. Inspect the response headers.
4. Look for:

   ```
   Connection: Keep-Alive
   ```

Alternatively, use `curl`:

```
curl -I https://yourdomain.com
```

> **Important Notes:**
- Keep‑Alive is enabled by default on most Ruachost shared hosting accounts.
- Disabling keep‑alive may reduce performance but can be useful for high‑traffic sites with limited resources.
- Always test after enabling to confirm headers are correctly set.
