Fix Plex Remote Access with Dynamic VPN Port Forwarding and Gluetun
Learn how to automatically update Plex's public port when using a VPN provider with dynamic port forwarding and the Gluetun container.
If you self-host Plex and route your traffic through a VPN using Gluetun, you have probably run into this frustrating situation. Your VPN provider assigns a random forwarded port on each connection, Plex does not know about it, and remote access silently breaks. You end up manually updating the port every time your container restarts.
There is a cleaner way to handle this, and it only takes two environment variables.
Setting Up the Environment Variables
Add the following to your Gluetun container configuration:
environment:
- VPN_PORT_FORWARDING_UP_COMMAND=/bin/sh -c 'sleep 60s && wget --method=PUT -O - "http://127.0.0.1:32400/:/prefs?ManualPortMappingPort={{PORT}}&X-Plex-Token=xxxxxxxxxxxxxxxxxxxx"'
- VPN_PORT_FORWARDING_LISTENING_PORTS=32400
How It Works
When Gluetun receives the forwarded port from your VPN provider, it replaces {{PORT}} with the actual assigned value and executes the command. This sends a request directly to Plex's internal API to update the port automatically. The short delay ensures Plex is fully started before the update is applied.
Configuring Plex
On the Plex side, you must enable manual port configuration before this works. Go to Settings → Remote Access and check Manually specify public port, otherwise Plex will ignore the incoming update entirely.
Things to Keep in Mind
Make sure to replace the Plex token placeholder with your own token. Plex also needs to be reachable at 127.0.0.1 from the Gluetun container, which is typically the case when both share the same Docker network namespace.
With this setup, every VPN reconnection will automatically push the correct port to Plex, keeping remote access working without any manual intervention.