Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ jobs:
targets: image-local
env:
DEFAULT_TAG: ${{ env.BUILD_TAG }}
-
name: Check reverse proxy handling
run: |
docker run --rm --network none \
--mount type=bind,source="$PWD/test/proxy.sh",target=/proxy-test.sh,readonly \
--entrypoint sh "$BUILD_TAG" /proxy-test.sh
-
name: Start
run: |
Expand Down
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ ___
* [Notes](#notes)
* [LNMS command](#lnms-command)
* [Validate](#validate)
* [Reverse proxy](#reverse-proxy)
* [Dispatcher service container](#dispatcher-service-container)
* [Syslog-ng container](#syslog-ng-container)
* [Snmptrapd container](#snmptrapd-container)
Expand Down Expand Up @@ -122,10 +123,15 @@ linux/s390x
* `FPM_PM_MAX_SPARE_SERVERS`: FPM max spare servers (default: `6`)
* `OPCACHE_MEM_SIZE`: PHP OpCache memory consumption (default `128`)
* `LISTEN_IPV6`: Enable IPv6 for Nginx (default `true`)
* `REAL_IP_FROM`: Trusted addresses that are known to send correct replacement addresses (default `0.0.0.0/32`)
* `REAL_IP_FROM`: Override the trusted proxy address or CIDR for Nginx access
logs. Unset or `0.0.0.0/32` uses LibreNMS's `APP_TRUSTED_PROXIES`
configuration.
* `REAL_IP_HEADER`: Request header field whose value will be used to replace the client address (default `X-Forwarded-For`)
* `LOG_IP_VAR`: Use another variable to retrieve the remote IP address for access [log_format](http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format) on Nginx. (default `remote_addr`)
* `APP_TRUSTED_PROXIES`: IPs or CIDR ranges trusted to set forwarding headers (X-Forwarded-For, X-Forwarded-Proto, etc.). These hosts can spoof any request header, including authentication headers.
* `APP_TRUSTED_PROXIES`: Proxy IP addresses or CIDR ranges, separated by commas
without spaces, trusted to supply forwarded client addresses, hostnames,
ports, and schemes (default `127.0.0.1`). Also used for Nginx access logs.
See [Reverse proxy](#reverse-proxy).
* `SESSION_DRIVER`: [Driver to use for session storage](https://github.com/librenms/librenms/blob/master/config/session.php) (default `file`)
* `CACHE_DRIVER`: [Driver to use for cache and locks](https://github.com/librenms/librenms/blob/master/config/cache.php) (default `database`)

Expand Down Expand Up @@ -333,6 +339,36 @@ SNMP | NET-SNMP 5.8
[WARN] Updates are managed through the official Docker image
```

### Reverse proxy

Set `APP_TRUSTED_PROXIES` in the LibreNMS container environment to your trusted
reverse proxy IP addresses or network ranges. The proxy must forward the
original host and scheme using `X-Forwarded-Host` and
`X-Forwarded-Proto`, and the client address using `X-Forwarded-For`.

For example, if the proxy connects from `192.0.2.10`:

```yaml
environment:
APP_TRUSTED_PROXIES: "192.0.2.10"
```

Restrict this setting to your actual proxies. Docker network subnets vary
between deployments, so do not assume a particular subnet or use `*` to work
around an address mismatch. Recreate the container after changing its
environment. Startup caches these settings for PHP-FPM, so `CLEAR_ENV=yes`
can remain enabled and no manual edits to the generated `.env` are needed.

Nginx automatically uses the same trusted proxies to record client addresses
in its access logs. `REAL_IP_FROM` remains available to override Nginx's trust
configuration independently. If you previously configured only `REAL_IP_FROM`,
replace it with `APP_TRUSTED_PROXIES` to apply the setting to both LibreNMS and
Nginx.

When `APP_TRUSTED_PROXIES` is `*` or `**`, LibreNMS still handles forwarded
headers, but Nginx logs the connection address. Set `REAL_IP_FROM` to an
explicit proxy IP address or CIDR to enable client address logging in that case.

### Dispatcher service container

If you want to enable the new [Dispatcher service](https://docs.librenms.org/Extensions/Dispatcher-Service/),
Expand Down
24 changes: 0 additions & 24 deletions examples/traefik/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ services:
volumes:
- "./acme.json:/acme.json"
- "/var/run/docker.sock:/var/run/docker.sock"
networks:
- librenms_net
restart: always

db:
Expand All @@ -54,26 +52,20 @@ services:
- "MYSQL_DATABASE=${MYSQL_DATABASE}"
- "MYSQL_USER=${MYSQL_USER}"
- "MYSQL_PASSWORD=${MYSQL_PASSWORD}"
networks:
- librenms_net
restart: always

redis:
image: redis:7.2-alpine
container_name: librenms_redis
environment:
- "TZ=${TZ}"
networks:
- librenms_net
restart: always

msmtpd:
image: crazymax/msmtpd:latest
container_name: librenms_msmtpd
env_file:
- "./msmtpd.env"
networks:
- librenms_net
restart: always

librenms:
Expand Down Expand Up @@ -105,9 +97,6 @@ services:
- "DB_USER=${MYSQL_USER}"
- "DB_PASSWORD=${MYSQL_PASSWORD}"
- "DB_TIMEOUT=60"
- "APP_TRUSTED_PROXIES=172.28.0.0/16"
networks:
- librenms_net
restart: always

dispatcher:
Expand All @@ -132,8 +121,6 @@ services:
- "DB_TIMEOUT=60"
- "DISPATCHER_NODE_ID=dispatcher1"
- "SIDECAR_DISPATCHER=1"
networks:
- librenms_net
restart: always

syslogng:
Expand Down Expand Up @@ -164,8 +151,6 @@ services:
- "DB_PASSWORD=${MYSQL_PASSWORD}"
- "DB_TIMEOUT=60"
- "SIDECAR_SYSLOGNG=1"
networks:
- librenms_net
restart: always

snmptrapd:
Expand Down Expand Up @@ -196,13 +181,4 @@ services:
- "DB_PASSWORD=${MYSQL_PASSWORD}"
- "DB_TIMEOUT=60"
- "SIDECAR_SNMPTRAPD=1"
networks:
- librenms_net
restart: always

networks:
librenms_net:
driver: bridge
ipam:
config:
- subnet: 172.28.0.0/16
4 changes: 3 additions & 1 deletion examples/traefik/librenms.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ UPLOAD_MAX_SIZE=16M
OPCACHE_MEM_SIZE=128
REAL_IP_FROM=0.0.0.0/32
REAL_IP_HEADER=X-Forwarded-For
LOG_IP_VAR=http_x_forwarded_for
LOG_IP_VAR=remote_addr

# Set APP_TRUSTED_PROXIES for your reverse proxy. See README.md#reverse-proxy.

CACHE_DRIVER=redis
SESSION_DRIVER=redis
Expand Down
16 changes: 0 additions & 16 deletions rootfs/etc/s6-overlay/scripts/config
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ FPM_PM_START_SERVERS=${FPM_PM_START_SERVERS:-2}
FPM_PM_MIN_SPARE_SERVERS=${FPM_PM_MIN_SPARE_SERVERS:-1}
FPM_PM_MAX_SPARE_SERVERS=${FPM_PM_MAX_SPARE_SERVERS:-6}
OPCACHE_MEM_SIZE=${OPCACHE_MEM_SIZE:-128}
LISTEN_IPV6=${LISTEN_IPV6:-true}
REAL_IP_FROM=${REAL_IP_FROM:-"0.0.0.0/32"}
REAL_IP_HEADER=${REAL_IP_HEADER:-"X-Forwarded-For"}
LOG_IP_VAR=${LOG_IP_VAR:-remote_addr}
MAX_INPUT_VARS=${MAX_INPUT_VARS:-1000}

MEMCACHED_PORT=${MEMCACHED_PORT:-11211}
Expand Down Expand Up @@ -75,18 +71,6 @@ echo "Setting OpCache configuration..."
sed -e "s/@OPCACHE_MEM_SIZE@/$OPCACHE_MEM_SIZE/g" \
/tpls/etc/php84/conf.d/opcache.ini >/etc/php84/conf.d/opcache.ini

# Nginx
echo "Setting Nginx configuration..."
sed -e "s#@UPLOAD_MAX_SIZE@#$UPLOAD_MAX_SIZE#g" \
-e "s#@REAL_IP_FROM@#$REAL_IP_FROM#g" \
-e "s#@REAL_IP_HEADER@#$REAL_IP_HEADER#g" \
-e "s#@LOG_IP_VAR@#$LOG_IP_VAR#g" \
/tpls/etc/nginx/nginx.conf >/etc/nginx/nginx.conf

if [ "$LISTEN_IPV6" != "true" ]; then
sed -e '/listen \[::\]:/d' -i /etc/nginx/nginx.conf
fi

# SNMP
echo "Updating SNMP community..."
file_env 'LIBRENMS_SNMP_COMMUNITY' 'librenmsdocker'
Expand Down
2 changes: 2 additions & 0 deletions rootfs/etc/s6-overlay/scripts/config-main
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,5 @@ artisan db:seed --force --no-ansi --no-interaction
echo "Clear cache"
artisan cache:clear --no-interaction
artisan config:cache --no-interaction

/etc/s6-overlay/scripts/config-nginx
27 changes: 27 additions & 0 deletions rootfs/etc/s6-overlay/scripts/config-nginx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
set -e

cd "${LIBRENMS_PATH}"

UPLOAD_MAX_SIZE=${UPLOAD_MAX_SIZE:-16M}
LISTEN_IPV6=${LISTEN_IPV6:-true}
REAL_IP_HEADER=${REAL_IP_HEADER:-"X-Forwarded-For"}
LOG_IP_VAR=${LOG_IP_VAR:-remote_addr}

echo "Setting Nginx configuration..."
if [ -n "${REAL_IP_FROM}" ] && [ "${REAL_IP_FROM}" != "0.0.0.0/32" ]; then
realIpFrom="set_real_ip_from ${REAL_IP_FROM};"
else
# Use the same effective proxy list as LibreNMS, including .env settings.
realIpFrom=$(php /etc/s6-overlay/scripts/nginx-trusted-proxies.php)
fi

sed -e "s#@UPLOAD_MAX_SIZE@#$UPLOAD_MAX_SIZE#g" \
-e "s#@REAL_IP_FROM@#$realIpFrom#g" \
-e "s#@REAL_IP_HEADER@#$REAL_IP_HEADER#g" \
-e "s#@LOG_IP_VAR@#$LOG_IP_VAR#g" \
/tpls/etc/nginx/nginx.conf >/etc/nginx/nginx.conf

if [ "$LISTEN_IPV6" != "true" ]; then
sed -e '/listen \[::\]:/d' -i /etc/nginx/nginx.conf
fi
26 changes: 26 additions & 0 deletions rootfs/etc/s6-overlay/scripts/nginx-trusted-proxies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

require 'vendor/autoload.php';
$app = require 'bootstrap/app.php';
(new Illuminate\Foundation\Bootstrap\LoadConfiguration)->bootstrap($app);
$proxies = $app['config']->get('trustedproxy.proxies');

// Leave wildcard handling to LibreNMS and log the connection address in Nginx.
if ($proxies === '*' || $proxies === '**') {
return;
}

echo 'real_ip_recursive on; ';
Symfony\Component\HttpFoundation\Request::setTrustedProxies((array) $proxies, 0);
$proxies = Symfony\Component\HttpFoundation\Request::getTrustedProxies();
foreach ($proxies as $proxy) {
if ($proxy !== '') {
[$address, $prefix] = array_pad(explode('/', $proxy, 2), 2, null);
if (($prefix !== null && !preg_match('/^(0|[1-9][0-9]*)$/D', $prefix))
|| !Symfony\Component\HttpFoundation\IpUtils::checkIp($address, $proxy)) {
fwrite(STDERR, "Ignoring invalid trusted proxy address: $proxy\n");
continue;
}
echo "set_real_ip_from $proxy; ";
}
}
5 changes: 4 additions & 1 deletion rootfs/tpls/etc/nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ http {
tcp_nodelay on;

## Handling of IPs in proxied and load balancing situations
set_real_ip_from @REAL_IP_FROM@;
@REAL_IP_FROM@
real_ip_header @REAL_IP_HEADER@;

# Log
Expand Down Expand Up @@ -85,6 +85,9 @@ http {
set $path_info $fastcgi_path_info;
try_files $fastcgi_script_name =404;
include fastcgi_params;
# Preserve the peer for LibreNMS trusted proxy validation.
fastcgi_param REMOTE_ADDR $realip_remote_addr;
fastcgi_param REMOTE_PORT $realip_remote_port;
fastcgi_param SERVER_SOFTWARE "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $path_info;
Expand Down
Loading
Loading