<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>VPN on Tech Philosopher</title><link>https://blog.bktus.com/en/tags/vpn/</link><description>Recent content in VPN on Tech Philosopher</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Sat, 23 May 2026 00:35:00 +0200</lastBuildDate><atom:link href="https://blog.bktus.com/en/tags/vpn/index.xml" rel="self" type="application/rss+xml"/><item><title>Building a Split-Tunneling Personal VPN Gateway: A Practical Setup with WireGuard, sing-box, and Gluetun</title><link>https://blog.bktus.com/en/archives/uqrk3i/</link><pubDate>Sat, 23 May 2026 00:35:00 +0200</pubDate><guid>https://blog.bktus.com/en/archives/uqrk3i/</guid><description>&lt;p>In a &lt;a href="https://blog.bktus.com/en/archives/qqyy2t/">previous post&lt;/a>, I introduced a fairly straightforward chained VPN setup: all devices first connect to a WireGuard server I host myself, the server forwards the traffic to Gluetun, and in the end the traffic reaches the Internet through the exit of an upstream VPN provider.&lt;/p>
&lt;p>The core value of this setup is pretty clear:&lt;/p>
&lt;ul>
&lt;li>All devices only need to set up one WireGuard connection.&lt;/li>
&lt;li>The upstream VPN only has to be maintained on the server.&lt;/li>
&lt;li>Phones, computers, tablets and home devices can all join the same virtual LAN.&lt;/li>
&lt;li>The exit IP comes from a commercial VPN, a company VPN or another upstream VPN, instead of exposing the VPS IP directly.&lt;/li>
&lt;li>The whole system can be deployed with Docker, so it keeps the host environment as clean as possible.&lt;/li>
&lt;li>Compared with connecting to a VPN directly, all the VPN provider can see in this setup is the exit of your VPN gateway (your VPS), not the real IP of each device.&lt;/li>
&lt;li>Also, your local ISP and others can’t tell whether you’re using a mainstream VPN. All they know is that you’re connecting to a VPN gateway (your VPS), but where the traffic finally goes is unknown to them.&lt;/li>
&lt;/ul>
&lt;p>But as I used it in more situations, the original structure started to show some limits. The most typical problem was that traffic control wasn’t fine-grained enough. By default, all traffic had to go into the upstream VPN tunnel, and for some services this could trigger risk-control mechanisms or cause access problems. It also wasn’t easy to do traffic engineering and use different exit strategies for different services. I could already do split routing with iptables and routing tables, but once there were a lot of rules, it got hard to maintain.&lt;/p></description><content:encoded><![CDATA[<p>In a <a href="https://blog.bktus.com/en/archives/qqyy2t/">previous post</a>, I introduced a fairly straightforward chained VPN setup: all devices first connect to a WireGuard server I host myself, the server forwards the traffic to Gluetun, and in the end the traffic reaches the Internet through the exit of an upstream VPN provider.</p>
<p>The core value of this setup is pretty clear:</p>
<ul>
<li>All devices only need to set up one WireGuard connection.</li>
<li>The upstream VPN only has to be maintained on the server.</li>
<li>Phones, computers, tablets and home devices can all join the same virtual LAN.</li>
<li>The exit IP comes from a commercial VPN, a company VPN or another upstream VPN, instead of exposing the VPS IP directly.</li>
<li>The whole system can be deployed with Docker, so it keeps the host environment as clean as possible.</li>
<li>Compared with connecting to a VPN directly, all the VPN provider can see in this setup is the exit of your VPN gateway (your VPS), not the real IP of each device.</li>
<li>Also, your local ISP and others can’t tell whether you’re using a mainstream VPN. All they know is that you’re connecting to a VPN gateway (your VPS), but where the traffic finally goes is unknown to them.</li>
</ul>
<p>But as I used it in more situations, the original structure started to show some limits. The most typical problem was that traffic control wasn’t fine-grained enough. By default, all traffic had to go into the upstream VPN tunnel, and for some services this could trigger risk-control mechanisms or cause access problems. It also wasn’t easy to do traffic engineering and use different exit strategies for different services. I could already do split routing with iptables and routing tables, but once there were a lot of rules, it got hard to maintain.</p>
<p>So I reorganized the original setup into a more complete personal VPN gateway architecture. The new version isn’t just a simple <code>WG-Easy + Gluetun</code> anymore. It has become:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>WG-Easy + sing-box + Gluetun + AdGuard Home + unified config generation from config.yaml
</span></span></code></pre></div><p>For me, this setup is no longer just “VPN over VPN”. It’s a personal network exit that I can maintain, extend and observe over the long term. It works for everyday browsing, and also for remote work, accessing my home network, development and testing, and managing many devices in one place. If you only need the simplest global VPN forwarding, the old approach is still good enough. But if you want a better balance between privacy, control, split routing rules and DNS management, the new architecture is a better fit.</p>
<p>In this post, I’ll go through the design ideas behind the new architecture, its traffic paths, the use cases it fits, and the main improvements over the old setup. If you’re interested in the new version, or just want to deploy it, you can go straight to <a href="https://github.com/saturneric/wg-easy-gluetun">my project repository on GitHub</a>.</p>
<h2 id="why-i-needed-a-new-architecture">Why I Needed a New Architecture</h2>
<p>The earliest chained VPN setup actually solved a lot of problems already. For example, at home, in the office or on any network outside, I could use the same WireGuard config to connect to the VPS, and then get into the upstream VPN through Gluetun on the VPS. This way, all devices share the same upstream VPN exit, and I don’t have to install, log in to and configure different VPN clients on every device over and over again.</p>
<p>In my daily use, this setup has been very stable, and the performance is good enough for what I need. There were basically no big problems. But after using it for a long time, I still ran into a few pain points.</p>
<h3 id="not-all-traffic-should-go-through-the-upstream-vpn">Not All Traffic Should Go Through the Upstream VPN</h3>
<p>In practice, I found that some services may trigger risk-control mechanisms because of the VPN exit, such as account logins, payments and AI services. For example, some streaming platforms I use a lot have strict restrictions on VPNs, and sometimes this traffic is better off going direct. Also, I sometimes need to access course servers inside the campus network, and sometimes that involves remote desktop, which needs fairly low latency. In these cases, going through a VPN gives a really poor experience, even when the exit is in the same country.</p>
<p>So I wanted finer-grained split routing. For example, I could use rule sets to let traffic for some services I trust bypass the upstream VPN and go to the Internet directly from the server. This keeps the privacy protection of the upstream VPN and also avoids some unnecessary access problems. Before, I relied more on iptables, routing tables and container networking. It worked, but once there were a lot of rules, it was hard to maintain.</p>
<p>After some research and testing, I found that sing-box’s TUN mode is a really good fit for this kind of rule-based split routing. It supports many rule types and flexible routing strategies, and it can decide the next hop for traffic based on the domain, the IP and other factors. By putting sing-box between WG-Easy and Gluetun, I can route traffic in a smarter way instead of using a simple global VPN. sing-box can also reference remote rule sets, such as geosite-openai and geosite-paypal, so I don’t need to maintain these rules myself. Another thing is that, compared with running an OpenWrt virtual machine, sing-box is lighter to deploy and maintain, and it’s more Docker-friendly.</p>
<p>This way, I can do traffic engineering much more flexibly. For example, default traffic should go through the VPN for privacy. Some services in a particular region I want to reach from a different VPN exit. Some services I want to access directly, and some I want to send through a specific VPN exit. With sing-box’s rule-based routing, these policies are much easier to set up.</p>
<h3 id="configuration-needs-to-be-managed-in-one-place">Configuration Needs to Be Managed in One Place</h3>
<p>In the original repository, the configuration was scattered across the conf directory, and changing things like subnets was already a bit inconvenient. After I added sing-box, maintaining Docker Compose, the WG-Easy hook, the sing-box JSON, the AdGuard Home config and the iptables rules all together became pretty painful. So in the new version, I use <code>config.yaml</code> as the single entry point for configuration, and generate the runtime files from templates.</p>
<p>After each config change, I just run the <code>up.sh</code> script and it brings all the related config files up to date. That way I no longer run into “changing this one and forgetting that one”.</p>
<h3 id="better-network-monitoring">Better Network Monitoring</h3>
<p>The original setup mainly relied on logs to see what was happening on the network. Sometimes I wanted to see which devices were connected to WireGuard right now, what services they were accessing, whether there was any unusual traffic, and so on. In the old setup, I had to read the logs inside the containers myself or install extra monitoring tools to get this information. In the new version, the API that sing-box provides works with tools like MetaCubeXD, so I can see traffic distribution, rule hits, upstream access and other information in real time. This makes the network much easier to observe.</p>
<p>So the goal of the new architecture isn’t just to “add one more VPN layer”. What I want is to build a controllable network exit that’s closer to something for home or personal use.</p>
<h2 id="components-of-the-new-setup">Components of the New Setup</h2>
<p>The new setup is mainly made up of four core containers.</p>
<h3 id="wg-easy-the-wireguard-access-layer">WG-Easy: The WireGuard Access Layer</h3>
<p>WG-Easy still handles the most basic and most important part: it provides the WireGuard server and a web management interface. All phones, computers, tablets and servers connect to WG-Easy as WireGuard clients. Users can create peers in the WG-Easy web UI, download config files, or scan a QR code to import the config on a phone.</p>
<p>In the new architecture, WG-Easy no longer just hands traffic straight to Gluetun. Instead, it uses a hook and policy routing to send client traffic into a more complex gateway path.</p>
<h3 id="adguard-home-unified-dns-and-filtering-layer">AdGuard Home: Unified DNS and Filtering Layer</h3>
<p>AdGuard Home takes care of DNS resolution, ad filtering, blocking tracking domains and logging DNS queries.</p>
<p>In the new architecture, regular DNS requests from WireGuard clients are restricted to AdGuard Home. In other words, clients can’t just send DNS queries to <code>8.8.8.8</code>, <code>1.1.1.1</code> or other external DNS servers.</p>
<p>The benefits of this are:</p>
<ul>
<li>Upstream DNS can be set in one place.</li>
<li>Filter lists can be managed together.</li>
<li>Client DNS queries can be viewed.</li>
<li>The risk of DNS leaks is lower.</li>
<li>There’s no need to set up filtering software on each device separately.</li>
</ul>
<p>Of course, this doesn’t mean it can fully stop every form of DNS bypass. For example, DoH, DoT or the built-in resolvers in some apps still need extra policies. But for regular DNS requests on UDP/TCP port 53, the gateway can bring them all through one place.</p>
<h3 id="sing-box-the-rule-based-routing-layer">sing-box: The Rule-Based Routing Layer</h3>
<p>sing-box is the most important change in the new architecture.</p>
<p>It receives the traffic forwarded from WG-Easy through a TUN inbound, and then decides the next hop based on rules:</p>
<ul>
<li>Traffic that matches <code>bypass_rule_sets</code> goes out directly.</li>
<li>Private IP traffic goes out directly, so it isn’t sent into the upstream VPN by mistake.</li>
<li>Other default traffic gets a routing mark and is handed to Linux policy routing, which then sends it into Gluetun.</li>
<li>Rule sets can use remote <code>.srs</code> files, such as geosite-openai and geosite-paypal.</li>
</ul>
<p>If you look at the sing-box config file, you’ll notice something a bit strange: the <code>gluetun</code> outbound in sing-box is also of type <code>direct</code>, but it carries a <code>routing_mark</code>. Actually, this mark gets picked up by Linux policy routing, which sends the traffic to Gluetun. So it isn’t “direct” in the usual sense. It’s really “sent to Gluetun through the system’s routing”.</p>
<p>Basically, sing-box works more like a smart traffic splitter here, and Gluetun is the final upstream VPN exit. Besides going through Gluetun, sing-box itself can also be set up in a similar way with other types of outbound, such as WireGuard, VLESS and Trojan, to work with a wider range of upstream services. If you don’t need the various conveniences Gluetun provides, you could even replace it right here with another VPN client or a direct Internet exit.</p>
<p>One thing to note is that sing-box can’t get the domain information here, so we need to turn on sniffing to identify the domain. Only then can it do split routing based on domain rule sets. For protocols or encrypted traffic that sniffing doesn’t support, it may only be able to split traffic based on IP rules.</p>
<h3 id="gluetun-the-upstream-vpn-exit-layer">Gluetun: The Upstream VPN Exit Layer</h3>
<p>Gluetun is responsible for connecting to a commercial VPN, a company VPN or another supported upstream VPN service.</p>
<p>Its strength is that it supports a lot of VPN providers, and it packs the OpenVPN / WireGuard clients, authentication parameters, firewall rules, health checks and other logic into one container.</p>
<p>In the new architecture, most default traffic ends up in Gluetun, and Gluetun then reaches the Internet through the upstream VPN tunnel. This way, clients see their own WireGuard network, while the Internet sees the exit IP of the upstream VPN.</p>
<h2 id="overall-network-topology">Overall Network Topology</h2>
<p>You can think of the new architecture as the following path:</p>
<pre class="mermaid">
  flowchart TD
  subgraph devices[&#34;Your Devices&#34;]
    phone[&#34;Phone&#34;]
    pc[&#34;PC&#34;]
    laptop[&#34;Laptop&#34;]
  end

  subgraph server[&#34;Your Server&#34;]
    wgeasy[&#34;WG-Easy (WireGuard Server)&#34;]
    adguard[&#34;AdGuard Home (DNS Filtering)&#34;]
    singbox[&#34;sing-box (TUN Router / Rule Engine)&#34;]
    gluetun[&#34;Gluetun (VPN Provider Client)&#34;]
  end

  phone -- WireGuard --&gt; wgeasy
  pc -- WireGuard --&gt; wgeasy
  laptop -- WireGuard --&gt; wgeasy

  wgeasy -- DNS only --&gt; adguard
  wgeasy -- regular traffic --&gt; singbox
  singbox -- bypass_rule_sets --&gt; direct[&#34;Direct Internet&#34;]
  singbox -- default route --&gt; gluetun
  gluetun -- VPN tunnel --&gt; provider[&#34;VPN Provider&#34;]
  provider --&gt; internet[&#34;Internet&#34;]
  direct --&gt; internet
</pre>
<p>From the traffic point of view, it can be split into three types:</p>
<ol>
<li><strong>DNS traffic</strong>: DNS requests from WireGuard clients are always forwarded to AdGuard Home.</li>
<li><strong>Traffic that matches bypass rules</strong>: for example, OpenAI, PayPal or other rule sets you put into <code>bypass_rule_sets</code> can go direct.</li>
<li><strong>Default traffic</strong>: regular Internet traffic that doesn’t match any bypass rule goes into Gluetun, and then reaches the Internet through the upstream VPN exit.</li>
</ol>
<p>Also, you can configure <code>direct_subnets</code> to let specific subnets bypass sing-box. This is useful for clear-cut internal network access, such as reaching your home LAN, a specific Docker network or some dedicated subnets.</p>
<h2 id="configuration-management-from-hand-written-configs-to-generated-configs">Configuration Management: From Hand-Written Configs to Generated Configs</h2>
<p>Another important change in the new setup is how configuration is managed.</p>
<p>Before, you might have had to change all of these at the same time:</p>
<ul>
<li><code>docker-compose.yml</code></li>
<li>WireGuard hook</li>
<li>iptables rules</li>
<li>Gluetun environment variables</li>
<li>DNS config</li>
<li>sing-box JSON</li>
<li>AdGuard Home config</li>
</ul>
<p>This is flexible, but it’s expensive to maintain. Especially after even a small change to the network structure, it’s easy to end up “changing it in one place and forgetting the other”. The new setup puts the main configuration in <a href="https://github.com/saturneric/wg-easy-gluetun/blob/master/config.yaml"><code>config.yaml</code></a>, and then a generator script renders it into the actual config files under runtime/.</p>
<p>So regular users only need to edit <code>config.yaml</code>. Advanced users who want to adjust more settings can edit the Jinja2 templates under <code>templates/</code>. This keeps the configuration in one place and still leaves enough flexibility.</p>
<p>One thing to note: I suggest not editing the files under the <code>runtime/</code> directory directly. They’re generated, and they get overwritten every time it runs again.</p>
<h2 id="how-the-sing-box-split-routing-rules-work">How the sing-box Split Routing Rules Work</h2>
<p>In the new setup, the sing-box rules roughly look like this:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&#34;route&#34;</span>: {
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;rules&#34;</span>: [
</span></span><span style="display:flex;"><span>      { <span style="color:#f92672">&#34;inbound&#34;</span>: <span style="color:#e6db74">&#34;tun-in&#34;</span>, <span style="color:#f92672">&#34;action&#34;</span>: <span style="color:#e6db74">&#34;sniff&#34;</span> },
</span></span><span style="display:flex;"><span>      { <span style="color:#f92672">&#34;ip_is_private&#34;</span>: <span style="color:#66d9ef">true</span>, <span style="color:#f92672">&#34;outbound&#34;</span>: <span style="color:#e6db74">&#34;direct&#34;</span> },
</span></span><span style="display:flex;"><span>      {
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;rule_set&#34;</span>: [<span style="color:#e6db74">&#34;geosite-openai&#34;</span>, <span style="color:#e6db74">&#34;geosite-paypal&#34;</span>],
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;outbound&#34;</span>: <span style="color:#e6db74">&#34;direct&#34;</span>
</span></span><span style="display:flex;"><span>      },
</span></span><span style="display:flex;"><span>      { <span style="color:#f92672">&#34;outbound&#34;</span>: <span style="color:#e6db74">&#34;gluetun&#34;</span> }
</span></span><span style="display:flex;"><span>    ]
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>This means:</p>
<ol>
<li>First, sniff the traffic to identify the domain and other information.</li>
<li>Private IP addresses go direct.</li>
<li>Traffic that matches the rule sets goes direct.</li>
<li>All other traffic goes to the <code>gluetun</code> outbound.</li>
</ol>
<p><code>bypass_rule_sets</code> means “bypass the upstream VPN”, not “bypass your server”.</p>
<p>For example, if you configure:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#f92672">singbox</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">bypass_rule_sets</span>:
</span></span><span style="display:flex;"><span>    - <span style="color:#f92672">tag</span>: <span style="color:#e6db74">&#34;geosite-openai&#34;</span>
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">url</span>: <span style="color:#e6db74">&#34;https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-openai.srs&#34;</span>
</span></span><span style="display:flex;"><span>    - <span style="color:#f92672">tag</span>: <span style="color:#e6db74">&#34;geosite-paypal&#34;</span>
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">url</span>: <span style="color:#e6db74">&#34;https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-paypal.srs&#34;</span>
</span></span></code></pre></div><p>then the traffic for these services still goes through your WireGuard server and sing-box. It just doesn’t go into Gluetun’s upstream VPN tunnel anymore, and the server reaches the Internet directly instead.</p>
<p>This really helps with services that are sensitive to VPN exits. For example, some services ask for extra verification when they see a commercial VPN IP, or even refuse access outright. Bypassing the upstream VPN with rules can cut down on this kind of problem.</p>
<h2 id="what-is-extra_rule_sets">What Is extra_rule_sets</h2>
<p>The new config also has an <code>extra_rule_sets</code> option:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#f92672">singbox</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">extra_rule_sets</span>:
</span></span><span style="display:flex;"><span>    - <span style="color:#f92672">tag</span>: <span style="color:#e6db74">&#34;geosite-google&#34;</span>
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">url</span>: <span style="color:#e6db74">&#34;https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-google.srs&#34;</span>
</span></span><span style="display:flex;"><span>    - <span style="color:#f92672">tag</span>: <span style="color:#e6db74">&#34;geosite-apple&#34;</span>
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">url</span>: <span style="color:#e6db74">&#34;https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-apple.srs&#34;</span>
</span></span></code></pre></div><p>What it does is download and cache these rule sets, but they don’t take part in the current route matching. That is, <code>extra_rule_sets</code> doesn’t change the traffic path. A rule only really takes effect when it’s put into <code>bypass_rule_sets</code> or explicitly referenced in the sing-box template.</p>
<p>I kept this option to make future extensions easier. For example, I might want to get rule sets for Google, Apple, Amazon and others ready in advance, without turning them on right away. When I need them, I only have to move the rule into <code>bypass_rule_sets</code> or add a new route rule.</p>
<p>If you want the simplest possible config, you can also set <code>extra_rule_sets</code> to an empty array:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#f92672">singbox</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">extra_rule_sets</span>: []
</span></span></code></pre></div><p>Note that in YAML, it’s better to write an explicit empty array rather than just an empty key:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#75715e"># Recommended</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">extra_rule_sets</span>: []
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Not recommended, it can easily be parsed as null</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">extra_rule_sets</span>:
</span></span></code></pre></div><h2 id="dns-why-adguard-home-is-included-by-default">DNS: Why AdGuard Home Is Included by Default</h2>
<p>In a VPN gateway, DNS is something that’s easy to overlook. If you only handle regular traffic and ignore DNS, you might end up with this:</p>
<ul>
<li>Browser traffic goes through your VPN gateway.</li>
<li>But DNS queries go to the local network, your ISP’s DNS or some public DNS.</li>
<li>In the end, this causes DNS leaks, or makes the split routing decisions inaccurate.</li>
</ul>
<p>In the new architecture, regular DNS requests from WireGuard clients are restricted to AdGuard Home. This way, you can manage DNS in one place on the gateway.</p>
<p>AdGuard Home brings a few benefits:</p>
<ul>
<li>Filtering of ads and tracking domains</li>
<li>Custom upstream DNS</li>
<li>Query logs</li>
<li>Local domain rewrites</li>
<li>Different policies for different clients</li>
</ul>
<p>But keep in mind that if you want to block every DNS bypass completely, you also need to think about DoH, DoT, QUIC and so on. The DNS restriction in this post mainly targets traditional queries on UDP/TCP port 53.</p>
<h2 id="use-cases">Use Cases</h2>
<p>The new architecture covers more scenarios than the old one. All things considered, it mainly fits the following situations:</p>
<h3 id="1-sharing-one-upstream-vpn-across-many-devices">1. Sharing One Upstream VPN Across Many Devices</h3>
<p>This is the most basic way to use it.</p>
<p>You only configure the upstream VPN once on the server, and all devices connect through WireGuard. Phones, computers, tablets and spare devices don’t each need their own upstream VPN client.</p>
<p>This is especially useful when you have a lot of devices, a mix of platforms, or an upstream VPN client that isn’t very good.</p>
<h3 id="2-remote-access-to-a-home-or-office-network">2. Remote Access to a Home or Office Network</h3>
<p>Once all devices join the same WireGuard network, they form one virtual LAN.</p>
<p>You can use it to access:</p>
<ul>
<li>the NAS at home</li>
<li>internal services at the office</li>
<li>a private Git service</li>
<li>a home media server</li>
<li>SSH management access</li>
</ul>
<p>Together with <code>direct_subnets</code>, you can also let some internal network access skip the more complex sing-box routing logic.</p>
<h3 id="3-different-exit-strategies-for-different-services">3. Different Exit Strategies for Different Services</h3>
<p>Some services work well with a commercial VPN, and some don’t.</p>
<p>For example:</p>
<ul>
<li>Default traffic goes through the Gluetun upstream VPN.</li>
<li>Sensitive services like OpenAI and PayPal go direct.</li>
<li>Torrent and P2P traffic goes through Gluetun or another dedicated VPN exit.</li>
<li>Private IPs and internal addresses don’t go into the upstream VPN.</li>
<li>Specific rule sets are downloaded in advance but not turned on yet.</li>
</ul>
<p>This structure is more flexible than the old “global proxy” approach, and easier to maintain than hand-written iptables rules.</p>
<h3 id="5-low-intrusion-deployment">5. Low-Intrusion Deployment</h3>
<p>The whole setup is based on Docker Compose. The host only needs Docker and Docker Compose installed, and most of the networking logic lives in the containers and the generated config. This makes it pretty friendly to VPSes, home servers, software routers and Raspberry Pis.</p>
<h2 id="deployment-overview">Deployment Overview</h2>
<p>The actual deployment roughly goes like this:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>git clone https://github.com/saturneric/wg-easy-gluetun.git
</span></span><span style="display:flex;"><span>cd wg-easy-gluetun
</span></span></code></pre></div><p>Then edit:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>vim config.yaml
</span></span></code></pre></div><p>At minimum, you need to configure:</p>
<ul>
<li>the WireGuard / OpenVPN parameters for the upstream VPN</li>
<li>the WG-Easy admin password</li>
<li>the server’s public address or domain name</li>
<li>the upstream DNS for AdGuard Home</li>
<li>the sing-box bypass rules</li>
<li>the necessary service ports and fixed container IPs</li>
</ul>
<p>To start it, use:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>./up.sh -d
</span></span></code></pre></div><p>Don’t just run a plain <code>docker compose up</code>, because the runtime compose file and the related config have to be created by the generator first. <code>up.sh</code> runs the config generation first and then starts the full stack.</p>
<h2 id="security-notes">Security Notes</h2>
<p>This kind of gateway setup is convenient, but you also need to pay attention to its security boundaries.</p>
<h3 id="1-dont-use-the-default-passwords">1. Don’t Use the Default Passwords</h3>
<p>The WG-Easy password, the Clash API secret and the AdGuard Home credentials in <code>config.yaml</code> should all be changed. Especially if a management interface is exposed to the public internet, default passwords are very dangerous.</p>
<h3 id="2-be-careful-about-exposing-management-ports">2. Be Careful About Exposing Management Ports</h3>
<p>Usually, only the WireGuard UDP port should be public. It’s best not to expose the WG-Easy, MetaCubeXD or AdGuard Home management interfaces directly to the public internet.</p>
<h3 id="3-check-your-upstream-vpns-terms-of-service">3. Check Your Upstream VPN’s Terms of Service</h3>
<p>Whether sharing across devices, forwarding through a gateway and chained VPNs are allowed depends on your upstream VPN’s terms of service. You should check this yourself before deploying.</p>
<h3 id="4-watch-out-for-dns-bypass">4. Watch Out for DNS Bypass</h3>
<p>This setup restricts regular DNS queries, but it can’t automatically block all DoH / DoT at the application level. If you need a stricter DNS policy, you’ll need extra controls based on domains, IPs, SNI or the application layer.</p>
<h2 id="summary-main-changes-from-the-old-setup">Summary: Main Changes from the Old Setup</h2>
<p>Compared with the old <code>WG-Easy + Gluetun</code>, the new architecture mainly changes these things:</p>
<table>
  <thead>
      <tr>
          <th>Aspect</th>
          <th>Old</th>
          <th>New</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Access layer</td>
          <td>WG-Easy</td>
          <td>WG-Easy</td>
      </tr>
      <tr>
          <td>Upstream VPN</td>
          <td>Gluetun</td>
          <td>Gluetun</td>
      </tr>
      <tr>
          <td>Split routing</td>
          <td>Mainly routing and iptables</td>
          <td>Rule-set split routing with sing-box</td>
      </tr>
      <tr>
          <td>DNS</td>
          <td>Needs extra configuration</td>
          <td>AdGuard Home built in by default</td>
      </tr>
      <tr>
          <td>Configuration</td>
          <td>Multiple files maintained by hand</td>
          <td><code>config.yaml</code> + template generation</td>
      </tr>
      <tr>
          <td>Rule management</td>
          <td>Coarse-grained</td>
          <td>geosite / rule-set level</td>
      </tr>
      <tr>
          <td>Observability</td>
          <td>Mainly logs</td>
          <td>sing-box can be viewed with MetaCubeXD</td>
      </tr>
      <tr>
          <td>Extensibility</td>
          <td>Fairly limited</td>
          <td>Better suited to adding more rules and services later</td>
      </tr>
  </tbody>
</table>
<p>So, to sum up, the old version is simpler and suits users who just want to send all their traffic into the upstream VPN. The new version is better for long-term use, especially if you need split routing, DNS filtering, centralized management and support for many different scenarios.</p>
]]></content:encoded></item><item><title>Long-Term Personal VPS Self-Hosting: Cleanliness and Security</title><link>https://blog.bktus.com/en/archives/vgjftw/</link><pubDate>Sat, 03 Jan 2026 00:36:04 +0100</pubDate><guid>https://blog.bktus.com/en/archives/vgjftw/</guid><description>&lt;p>I’ve been seriously tinkering with personal self-hosting for about five or six years now. Strictly speaking, the time I started managing my own VPS goes back even further. The original motivation was actually pretty simple: there were some services I used every day, like a password manager, music playback, note syncing, a personal website, code repositories and so on. So why not just deploy them myself? That way the data stays in my own hands, and I’m not pushed around by all kinds of platform subscriptions and price increases.&lt;/p></description><content:encoded><![CDATA[<p>I’ve been seriously tinkering with personal self-hosting for about five or six years now. Strictly speaking, the time I started managing my own VPS goes back even further. The original motivation was actually pretty simple: there were some services I used every day, like a password manager, music playback, note syncing, a personal website, code repositories and so on. So why not just deploy them myself? That way the data stays in my own hands, and I’m not pushed around by all kinds of platform subscriptions and price increases.</p>
<p>Looking back on these years, to be honest, self-hosting isn’t hassle-free. I have to maintain the system, back up data, keep an eye on security, and once in a while put out fires. But what I get back is also very clear:</p>
<ul>
<li>My data is fully under my own control</li>
<li>I’m no longer pushed around by subscription prices</li>
<li>I understand systems, networking and security a lot better</li>
</ul>
<p>Now I run a dozen or so services on one server, and the cost is just the price of that one server. After canceling quite a few third-party subscriptions over the past two years, the overall cost may actually be lower when I do the math. For me, this “complex but under control” state feels more reassuring than depending on a pile of external services. Of course, this isn’t for everyone. I feel it suits people who have some technical background, enjoy tinkering, and are willing to put in time for stability and control. When all three come together in one person, this kind of setup fits.</p>
<p>Even now, I don’t think I’ve become any kind of “expert,” but my feelings about two things have grown stronger and stronger: <strong>security</strong> and <strong>how clean the server environment is</strong>. These are also the two main threads of this post. These two questions pretty much decide whether the services on your server can keep running stably over the long term. They also decide how much work it is when you need to migrate, for example when you switch to a cheaper or better VPS provider. (I’ve switched several times myself, and only settled down once I really couldn’t face moving again.)</p>
<h2 id="the-rough-early-days">The Rough Early Days</h2>
<p>In the beginning, my approach was very basic. I installed services directly on the host, and then installed Nginx or Apache2 (httpd). For example, I used Nginx as a reverse proxy. The good thing about web servers like Nginx is that the config files can be split up, one per site, and services are told apart by domain name instead of port number, so everything looks neat and tidy. Back then I thought this setup was pretty elegant.</p>
<p>But as the number of services slowly grew, problems started to show up one by one. Some services needed specific versions of dependencies, and it was hard for me to install different versions of various libraries or databases in the same environment. Some left all kinds of temporary files in the system. Config files were scattered under <code>/etc</code>, data was in <code>/srv</code>, and logs were in <code>/var</code>. At first you could still remember “this belongs to that service,” but after a while it all got mixed up.</p>
<p>During this time, when I wanted to remove a service I no longer used, I wasn’t even sure I could remove it cleanly. This was especially true for services that come with a “one-click install script.” Installing them feels great at the time, but when you want to get rid of them, you find you have no idea what changes the script made back then, or how to undo them. It’s hard to know whether there are leftover configs or background processes, and you don’t even know whether deleting the wrong thing will break other services. This uncertainty is a risk in itself. For someone with a bit of a cleanliness obsession like me, it was deeply uncomfortable every time I thought about it.</p>
<h2 id="the-containerization-phase">The Containerization Phase</h2>
<p>Later I moved almost all of my services into Docker. And these days I lean more and more toward containerizing every service or tool I use before deploying it. For services I really want to use but that don’t offer a standard containerized deployment, I just package them into containers myself and make my own containerized solution.</p>
<p>What really changed the experience wasn’t Docker itself, but <strong>Docker Compose</strong>. Containers basically shouldn’t be treated as “things to maintain long-term.” They can be deleted and rebuilt at any time, and you need to be aware of that. Early on, I thought Docker Compose files were a hassle. Why not just create a container with a command and let it run for a long time? Later, when I needed to update it, or when the service depended on several other containers underneath, the problems slowly showed up. If I had written the deployment down in a config file from the start, I could just run that file every time and reproduce the original deployment perfectly. I wouldn’t need to set up a bridge network, a hostname or a restart policy by hand. This saves a lot of time and energy, and the chance of making mistakes during configuration also goes way down.</p>
<p>Looking back now, I can say there are only three things that really need long-term maintenance:</p>
<ul>
<li>Service configuration (including version, network, storage and so on)</li>
<li>Data (mainly volumes and other mapped directories)</li>
<li>Deployment configuration (the various Docker Compose YAML files)</li>
</ul>
<p>Whether I reinstall the system or switch to another machine, as long as these three are still there, the services can be cloned perfectly to the new place. And the host itself finally became “clean.” Its directories are no longer full of things you installed years ago and have since forgotten the purpose of. So at this stage, I created a separate Git repository to collect and manage the Docker Compose files for all the services I use. After a year with it, I feel this approach holds up in all kinds of situations.</p>
<h2 id="one-service-one-database">One Service, One Database</h2>
<p>This one is worth mentioning. For a long time, all of my services shared one database instance. Logically it looks very reasonable: it saves resources, the structure is simple, and management is centralized. But after maintaining it for a long time, you’ll find this is actually a <strong>very unfriendly design</strong>.</p>
<p>Every new service means another set of users, permissions and passwords. If you want to migrate a service, you have to carefully pull its database out. Once the database version is upgraded, every service has to share the risk. For people who aren’t familiar with a certain database, managing permissions and users is a headache. You have to use the command line or some database management tool to carefully keep each service’s users apart and keep their passwords safe.</p>
<p>Later I just did the opposite: <strong>each service gets its own database instance.</strong> That means each Docker Compose file has its own database service. After watching this for a long time, I found that for personal self-hosting, it’s not as “wasteful” as you might think. A database container usually only takes 100 or 200 MB of memory. On a VPS with 4 cores and 8 GB of RAM, or even 2 cores and 4 GB, or on a home server, that’s acceptable.</p>
<p>But what I get in return is very real. The services are fully isolated from each other, which improves security a lot, and I no longer have endless friction with permissions and compatibility. I can just pack up all the data and config in one go and take it with me, and each database only holds the data of its own service.</p>
<h2 id="the-principle-of-minimal-exposure">The Principle of Minimal Exposure</h2>
<p>Early on, I exposed almost all of my services directly to the public internet, and protected them with a reverse proxy plus login:</p>
<ul>
<li>One Nginx Proxy Manager</li>
<li>A public domain for every service</li>
<li>Username/password protection at the application layer</li>
</ul>
<p>Back then I also thought: “Every service has a username and password, so it should be fine, right?” Only later did I slowly realize that this was just fooling myself. As long as <strong>one service</strong> has a brute-force weakness, an authentication bypass or an RCE (remote code execution) vulnerability, an attacker may get access to the container or even the host. What first happened to me was with a GitLab instance I ran without a container. GitLab once had a serious remote code execution vulnerability, and I didn’t find out about it in time. Then one day I found that a trojan had been planted on my server, and I only knew because my cloud provider told me. If I had kept photos, videos or backups on that server back then, the result would have been a disaster.</p>
<p>Also, the security maturity of different services varies a lot, and their tech stacks vary widely. Some projects are still young, and their security thinking is far from mature. And I can’t watch every security advisory every day. Once one of these services has a vulnerability, or the frameworks or dependencies they use do, like the recent security vulnerabilities around the React stack, that service or those services can blow up in a short time. If containerization and isolation aren’t done well at that point, the attacker may get more than that one service. Quite possibly they get the whole machine. And then come the hidden trojan scripts or crypto-mining scripts.</p>
<p>This is also a core principle I came to later: if a service doesn’t have to be open to the public internet, don’t open it. Blogs and static pages don’t lose much if they get attacked, but things like notes, password managers and admin panels are a disaster if something goes wrong. So you need to take a good look at which services you’re exposing right now. From my own experience, the ones I feel safest about are static websites, or pages that are only generated from parameters with no database behind them. But there are a lot of services I want to use, so how do I expose some and not others? My current practice is to expose only the services that really need it to the public internet, like a static blog and a very small number of public services. Git repositories are kept private as much as possible, or only expose a read-only static view (through gitweb or cgit). Dynamic services, admin panels, notes, password managers and so on are never exposed directly to the public internet.</p>
<h3 id="deploying-a-private-vpn">Deploying a Private VPN</h3>
<p>Later I found that the key to solving this problem is a VPN. I deployed a private VPN on the server (WireGuard, for example) and put all private services into <strong>a network that can only be reached from the internal network</strong>. Only after I connect to the VPN and get a virtual internal IP can I reach these services, starting from that internal IP. The overall idea is:</p>
<ul>
<li>The VPN provides a virtual private subnet (like <code>10.8.x.x</code>)</li>
<li>Docker internal services use another private subnet (like <code>172.x.x.x</code>)</li>
<li>The two subnets can reach each other through routing</li>
<li>The public internet can’t reach these services directly</li>
</ul>
<p>What this step means is that I no longer need to design a separate “defense plan” for each service, because they’re not on the attack surface at all. Add a reverse proxy and DNS that only work inside the internal network, and the experience is actually better than on the public internet: clear domain names, stable services, and real peace of mind.</p>
<h3 id="internal-reverse-proxy-and-dns-resolution">Internal Reverse Proxy and DNS Resolution</h3>
<p>To improve the experience further, I deployed an internal Nginx container inside the internal network (under 172.24.0.0/16), completely isolated from the Nginx instance that serves the public (for example, 172.20.0.0/16). This internal Nginx instance is bound to a fixed private IP (for example, 172.24.0.2), and it only serves traffic that comes in through the VPN network.</p>
<pre class="mermaid">
  flowchart LR
  %% Clients
  PUB[&#34;Public Internet User&#34;] --&gt;|&#34;HTTPS&#34;| PNGX[&#34;Public Nginx (*.example.com)&#34;]
  PNGX --&gt; PBLOG[&#34;Public Service (e.g., blog/static)&#34;]

  %% VPN access
  DEV[&#34;Your Device\n(WireGuard Client)&#34;] --&gt;|&#34;VPN Tunnel&#34;| WG[&#34;WireGuard Server (10.8.0.0/24)&#34;]

  %% Internal name + reverse proxy (VPN-only)
  WG --&gt;|&#34;DNS&#34;| DNS[&#34;Internal DNS (172.24.0.4)&#34;]
  WG --&gt;|&#34;HTTPS&#34;| INGX[&#34;Internal Nginx (172.24.0.2)&#34;]

  %% Private services
  subgraph DOCKER[&#34;Docker Private Networks&#34;]
    INGX --&gt; VAULT[&#34;Vaultwarden&#34;]
    INGX --&gt; NOTES[&#34;Notes / Joplin&#34;]
    INGX --&gt; GIT[&#34;Git Service&#34;]
  end
</pre>
<p>Then I added a mandatory internal DNS service. All traffic coming in through the VPN can only use this DNS for name resolution. There are many mature options for this in practice, like AdGuard Home, which can also take care of ad blocking and DNS hijacking at the same time. Besides resolving the normal network traffic that passes through, this DNS service also specifically handles all DNS resolution under *.vpn.example.com, for example:</p>
<ul>
<li><code>git.vpn.example.com</code></li>
<li><code>notes.vpn.example.com</code></li>
<li><code>vault.vpn.example.com</code></li>
</ul>
<p>All of these resolve to the IP the internal Nginx listens on, 172.24.0.2. So when the VPN is connected, I can just type the domain name into the browser and get straight to the internal service. All the traffic stays inside the internal network. It’s best if this domain is registered under your own name. Then, in your external DNS settings, set *.vpn.example.com to NXDOMAIN or your VPS’s IP address. That way, if you accidentally try to reach an internal service without going through the VPN, the traffic won’t get forwarded somewhere else by mistake.</p>
<h3 id="isolating-database-containers">Isolating Database Containers</h3>
<p>A database shouldn’t be on the same Docker network as unrelated services. So I did one more thing: each service and its database get their own isolated bridge. On this bridge, only these two containers can talk to each other, and the bridge isn’t connected to any external network. In other words, even if a service gets compromised, the only thing it can reach is its own database, not every database on the server. Like this:</p>
<pre tabindex="0"><code>services:
  db:
    image: postgres:16-alpine
    ...
    networks:
      - iso
    restart: unless-stopped

  joplin:
    image: git.stdv.de/saturneric/joplin:latest
    ...
    depends_on:
      - db
    networks:
      - iso
      - vpn
    restart: unless-stopped
networks:
  iso:
    internal: true
  vpn:
    external: true
</code></pre><p>You can see that db only talks one-to-one with joplin on the iso network, and this network blocks direct access from inside it to the outside network (internal: true). So if another service gets compromised, it generally can’t reach my joplin database over the network. But this isn’t “absolutely secure.” It always assumes the attacker hasn’t got access to the host. Still, in the real world, <strong>reducing the chance of lateral movement</strong> is a really important layer of protection in itself.</p>
<p>This actually also helps avoid hostname / database name conflicts. When a lot of databases on the same network use the same hostname, some services may end up connecting to the wrong database now and then.</p>
<h2 id="firewall-strategy">Firewall Strategy</h2>
<p>If you’re using a VPS, my suggestion is to use the firewall from your cloud provider first, and keep host firewall rules to a minimum. For the external firewall, limit the inbound ports you allow as strictly as you can, and reach your services through domain names and the reverse proxy. There are a few reasons. Two layers of firewall get confusing very easily, and it’s easy to forget which one you configured and which one you didn’t. I also found that Docker port mapping sometimes bypasses the local firewall, which can expose internal service ports by accident, and a lot of the time I didn’t even notice. And the cloud firewall can block large-scale scanning traffic early, which moves that compute cost onto the cloud provider.</p>
<p>Usually the cloud firewall only allows these inbound ports:</p>
<ul>
<li>80 / 443</li>
<li>The VPN port</li>
<li>SSH</li>
<li>Other ports you think really need to be open</li>
</ul>
<p>Even if other ports are listening on the host, they’re invisible from the outside as long as the cloud firewall doesn’t allow them. You can only reach them through the VPN, which saves a lot of worry.</p>
<h2 id="ssh-security">SSH Security</h2>
<p>There are lots of other great articles on this topic, so I’ll just cover some very basic but really important points. These are also things I still do all the time:</p>
<ul>
<li>Disable SSH login for root. If you need root, use a normal user plus sudo.</li>
<li>Don’t make the normal user’s SSH username too easy to guess, like admin.</li>
<li>Give the normal user a long random password (I suggest generating it with a password manager), and use <strong>key-based login</strong> by default day to day.</li>
<li>Optional: only allow key-based login for the normal user and disable passwords. (Be careful about the case where the key isn’t backed up and then you lose the device.)</li>
</ul>
<p>A lot of people suggest changing the SSH port. On whether to change it in a personal environment, here’s my own view:</p>
<ul>
<li>In a personal environment, the security benefit is limited</li>
<li>Some configuration later on gets more complicated</li>
<li>In the end it depends on your own habits</li>
</ul>
<h2 id="afterword">Afterword</h2>
<p>Actually, I’ve now moved to a dedicated server instead of a VPS, which is like renting a physical server in a data center that you can actually see. That’s because I now need to host dozens of containers (I went through them all, and I need every one of them). I think this shows that self-hosting really has become technical infrastructure I can’t do without day to day. It also shows that I think spending more money on it every month is really worth it.</p>
<p>Also, I should point out that in some places, the setup in this post, where one private VPN carries both your internal service traffic and your normal browsing traffic, is hard to pull off. For example, the server’s uplink bandwidth may be very expensive there, or the traffic may be metered. In that case, my suggestion is to do split routing ahead of time on the software router at home: separate out the self-hosting traffic first, and let normal browsing traffic go out through your home connection.</p>
<p>One more thing. Even if your server has enough uplink bandwidth to support a private VPN, a lot of the time you probably don’t want to browse the web with your VPS’s IP, because that directly exposes your identity. A better option is to send the traffic on to a VPN server from another VPN provider, and then go out through the exit IP it provides. I have a mature solution for this that I’ve been using myself for a long time. You can check out my blog post: <a href="https://blog.bktus.com/en/archives/qqyy2t/">Building a Personal Chained VPN Network: Efficient Privacy Protection, Unlimited Device Management, and Remote Access</a>.</p>
]]></content:encoded></item><item><title>Building a Personal Chained VPN Network: Efficient Privacy Protection, Unlimited Device Management, and Remote Access</title><link>https://blog.bktus.com/en/archives/qqyy2t/</link><pubDate>Mon, 02 Jun 2025 19:20:00 +0200</pubDate><guid>https://blog.bktus.com/en/archives/qqyy2t/</guid><description>&lt;p>In some situations, you might want to set up a chained VPN. That means your devices on the internal network first connect to a self-hosted WireGuard server (a personal VPS, for example), and that server then forwards all traffic to another VPN (a company, school or commercial VPN service) before it goes out to the Internet. This kind of “VPN over VPN” setup has some clear advantages:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Strong privacy protection: Traffic from your devices first goes through your own encrypted WireGuard tunnel to your personal VPS. The VPS then forwards it a second time through the commercial or company VPN, and it finally reaches the Internet from that VPN’s exit IP. This way, the VPS never uses its own IP address to access the target network directly, which greatly reduces the risk of your personal IP being tracked or leaked.&lt;/p></description><content:encoded><![CDATA[<p>In some situations, you might want to set up a chained VPN. That means your devices on the internal network first connect to a self-hosted WireGuard server (a personal VPS, for example), and that server then forwards all traffic to another VPN (a company, school or commercial VPN service) before it goes out to the Internet. This kind of “VPN over VPN” setup has some clear advantages:</p>
<ul>
<li>
<p>Strong privacy protection: Traffic from your devices first goes through your own encrypted WireGuard tunnel to your personal VPS. The VPS then forwards it a second time through the commercial or company VPN, and it finally reaches the Internet from that VPN’s exit IP. This way, the VPS never uses its own IP address to access the target network directly, which greatly reduces the risk of your personal IP being tracked or leaked.</p>
</li>
<li>
<p>Centralized and easy management: All client devices connect to one WireGuard service, so you don’t have to configure every device one by one. More importantly, it also neatly gets around the device limit per account that some commercial VPN providers set. You pay once, and all your devices share it.</p>
</li>
<li>
<p>Flexible and easy to switch: The commercial or company VPN configuration only needs to be changed once, on your self-hosted WireGuard server, and downstream clients (phones, computers and so on) don’t need any changes. So replacing or reconfiguring the upstream VPN is much faster and easier, and day-to-day maintenance gets a lot more efficient.</p>
</li>
<li>
<p>Your own virtual LAN, so devices can talk to each other: Once all your devices are connected to your self-hosted WireGuard VPN server, each one automatically gets an IP address in the same VPN virtual LAN (for example, 10.8.0.0/16). Devices in this network can talk to each other directly, without any extra setup.</p>
</li>
</ul>
<p>This convenience means that wherever you are, you can easily do things like:</p>
<ul>
<li>Remote SSH login: access your servers at home or in the office at any time.</li>
<li>Private NAS media access: reach the media files on your home NAS while you’re out.</li>
<li>Remote work: securely access internal company network resources from anywhere, at any time.</li>
</ul>
<h2 id="the-solution">The Solution</h2>
<p>After some exploring and hands-on work, I gradually worked out my own chained VPN setup. In general, it’s deployed entirely with Docker, and the core components are:</p>
<ol>
<li><strong>Gluetun</strong>: a Docker image with built-in OpenVPN/WireGuard clients that can connect to all the major VPN providers. In this example, we configure it to act as a VPN gateway inside a Docker network.</li>
<li><strong>WG-Easy</strong>: a simplified WireGuard Docker image with a built-in web interface, which makes it easy to create and manage WireGuard configuration files. Downstream devices connect to this service with a WireGuard client, and all their traffic is forwarded to the Gluetun container, which then sends it on to the upstream VPN.</li>
</ol>
<p>The advantage of this setup is that it’s fully containerized, so it doesn’t pollute the environment of your server or VPS. It’s also fairly stable. In my own daily use, I haven’t noticed any obvious latency, stuttering or dropped connections, and the performance is good (tested on my home connection). Also, the setup now supports IPv6. Even if a device is on an IPv4-only network, it can still reach IPv6 resources through the chain.</p>
<p>In terms of network topology, the whole system only needs one Docker bridge network:</p>
<ul>
<li><strong>vpn</strong>: the “VPN network” between Gluetun and WG-Easy. Gluetun acts as the gateway in it, and all traffic from WG-Easy enters this network and is forwarded by Gluetun to the upstream VPN.</li>
</ul>
<pre class="mermaid">
  flowchart TD
 subgraph s1[&#34;Your Devices&#34;]
        n1[&#34;Phone&#34;]
        E[&#34;PC&#34;]
        D[&#34;Laptop&#34;]
  end
 subgraph s2[&#34;Your Server (VPS)&#34;]
        n2[&#34;WG-Easy (WireGuard Server)&#34;]
        F[&#34;Gluetun (VPN Client)&#34;]
  end
    n1 -- WireGuard Tunnel --&gt; n2
    n2 --&gt; F
    E -- &#34;WireGuard Tunnel&#34; --&gt; n2
    D -- &#34;WireGuard Tunnel&#34; --&gt; n2
    F -- VPN Tunnel --&gt; B[&#34;VPN Provider&#34;]
    B --&gt; n3[&#34;Internet&#34;]
    n1@{ shape: rect}
    B@{ shape: rect}
    n3@{ shape: rect}
</pre>
<p>Here, WG-Easy listens on UDP 51820. When a client (your own phone or computer) connects, WG-Easy (inside its container) sends the traffic over the Docker network <code>vpn</code> to the Gluetun container, and Gluetun connects to the upstream VPN over WireGuard/OpenVPN. All downstream traffic finally leaves through Gluetun’s <code>tun0</code> interface, and that’s how you get the chained VPN.</p>
<hr>
<p>If you want to skip the detailed setup steps below and just use the complete, integrated setup, you can check out my repository on GitHub:</p>
<p>👉 <strong><a href="https://github.com/saturneric/wg-easy-gluetun">https://github.com/saturneric/wg-easy-gluetun</a></strong></p>
<h2 id="environment-and-prerequisites">Environment and Prerequisites</h2>
<ul>
<li>A Linux server or VPS that supports Docker and Docker Compose (with kernel support for WireGuard and iptables/NFT). I recommend kernel version 5.10 or later.</li>
<li>Docker and Docker Compose v2+ installed.</li>
<li>An account with a commercial VPN provider that supports OpenVPN/WireGuard.</li>
<li>Make sure the host allows the CAP_NET_ADMIN and SYS_MODULE capabilities and can load the WireGuard kernel module (you can check with <code>lsmod | grep wireguard</code>).</li>
</ul>
<h2 id="create-a-custom-network"><strong>Create a Custom Network</strong></h2>
<p>Before the actual deployment, first create the bridge network on the host:</p>
<pre tabindex="0"><code>docker network create \
  --driver bridge \
  --subnet 172.31.0.0/16 \
  --subnet fd01:beee:beee::/48 \
  vpn
</code></pre><p>Note: If an existing network conflicts with these subnets, adjust them to fit your own situation. This example assumes the Docker bridge supports IPv6 (for the IPv6 forwarding in the example). If you don’t use IPv6, you can leave out the related configuration or set disable_ipv6=1.</p>
<h2 id="docker-compose-configuration-explained">Docker Compose Configuration Explained</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#f92672">version</span>: <span style="color:#e6db74">&#34;3&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">services</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">gluetun</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">image</span>: <span style="color:#ae81ff">qmcgaw/gluetun</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">cap_add</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">NET_ADMIN</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">SYS_MODULE</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">devices</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">/dev/net/tun:/dev/net/tun</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">environment</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">VPN_SERVICE_PROVIDER=${VPN_SERVICE_PROVIDER}</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">VPN_TYPE=${VPN_TYPE}</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">WIREGUARD_PRIVATE_KEY=${WIREGUARD_PRIVATE_KEY}</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">WIREGUARD_PRESHARED_KEY=${WIREGUARD_PRESHARED_KEY}</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">WIREGUARD_ADDRESSES=${WIREGUARD_ADDRESSES}</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">SERVER_REGIONS=${SERVER_REGIONS}</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">configs</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#f92672">source</span>: <span style="color:#ae81ff">post-rules.txt</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">target</span>: <span style="color:#ae81ff">/iptables/post-rules.txt</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">volumes</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">/lib/modules:/lib/modules:ro</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">/data/gluetun-ws:/gluetun</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">sysctls</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv4.ip_forward=1</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv4.conf.all.src_valid_mark=1</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv6.conf.all.disable_ipv6=0</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv6.conf.all.forwarding=1</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv6.conf.default.forwarding=1</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">networks</span>:
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">vpn</span>:
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">ipv4_address</span>: <span style="color:#ae81ff">172.31.0.4</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">ipv6_address</span>: <span style="color:#e6db74">&#34;fd01:beee:beee::4&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">restart</span>: <span style="color:#ae81ff">unless-stopped</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">wg-easy</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">image</span>: <span style="color:#ae81ff">ghcr.io/wg-easy/wg-easy:15</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">ports</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#e6db74">&#34;51820:51820/udp&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">networks</span>:
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">vpn</span>:
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">ipv4_address</span>: <span style="color:#ae81ff">172.31.0.8</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">ipv6_address</span>: <span style="color:#e6db74">&#34;fd01:beee:beee::8&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">cap_add</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">NET_ADMIN</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">SYS_MODULE</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">sysctls</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv4.ip_forward=1</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv4.conf.all.src_valid_mark=1</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv6.conf.all.disable_ipv6=0</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv6.conf.all.forwarding=1</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv6.conf.default.forwarding=1</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">volumes</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">/lib/modules:/lib/modules:ro</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">/data/wg-easy:/etc/wireguard</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">restart</span>: <span style="color:#ae81ff">unless-stopped</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">networks</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">vpn</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">external</span>: <span style="color:#66d9ef">true</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">configs</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">post-rules.txt</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">content</span>: |<span style="color:#e6db74">
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      iptables -A FORWARD -i eth0 -o tun0 -j ACCEPT
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      iptables -A FORWARD -i tun0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      ip6tables -I INPUT -p icmpv6 -j ACCEPT
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      ip6tables -I OUTPUT -p icmpv6 -j ACCEPT
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      ip6tables -A FORWARD -i eth0 -o tun0 -j ACCEPT
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      ip6tables -A FORWARD -i tun0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      ip6tables -t nat -A POSTROUTING -o tun0 -j MASQUERADE</span>
</span></span></code></pre></div><p>Next, let’s go through it piece by piece:</p>
<ul>
<li><code>image: qmcgaw/gluetun</code>: “Lightweight swiss-army-knife-like VPN client to multiple VPN service providers”. It can provide VPN service in a container environment and give a VPN connection to other containers that need one. This is the officially maintained Gluetun image, and it supports many VPN protocols and providers.</li>
<li><code>cap_add: [NET_ADMIN, SYS_MODULE]</code>: The container needs the NET_ADMIN and SYS_MODULE capabilities, plus settings like IP forwarding (the same for WG-Easy and Gluetun). These give the container permission to manage the network and load kernel modules, so it can create the TUN device and configure routes, and the WireGuard tunnel and routing work properly.</li>
<li><code>devices: [&quot;/dev/net/tun:/dev/net/tun&quot;]</code>: mounts the host’s TUN device into the container, so Gluetun can create a virtual network interface.</li>
<li><code>image: ghcr.io/wg-easy/wg-easy:15</code>: the official WG-Easy image (you can change the version as needed).</li>
<li><code>&quot;51820:51820/udp&quot;</code>: maps UDP port 51820 on the host to the container, so downstream devices can connect to WireGuard through the host or directly through the Docker network. If you need to expose WG-Easy’s web admin interface, you also need to add <code>&quot;51821:51821/tcp&quot;</code>.</li>
</ul>
<p>For the specific parameters in the environment variables, see the <a href="https://github.com/qdm12/gluetun-wiki/tree/main">official Gluetun documentation</a>.</p>
<p>Note that the two settings below fix each container’s IP address on the Docker bridge. They’re referenced a lot later, when configuring WG-Easy’s routing table and firewall rules. If you change them, remember to update the corresponding values in the later configuration too.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#f92672">networks</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">vpn</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">ipv4_address</span>: <span style="color:#ae81ff">172.31.0.4</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">ipv6_address</span>: <span style="color:#e6db74">&#34;fd01:beee:beee::4&#34;</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#f92672">networks</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">vpn</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">ipv4_address</span>: <span style="color:#ae81ff">172.31.0.8</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">ipv6_address</span>: <span style="color:#e6db74">&#34;fd01:beee:beee::8&#34;</span>
</span></span></code></pre></div><p>Here’s what the firewall rules added by Gluetun’s extra startup script <code>post-rules.txt</code> do:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span><span style="color:#75715e"># 1. IPv4 forwarding rules (allow forwarding in both directions, eth0 &lt;-&gt; tun0)</span>
</span></span><span style="display:flex;"><span>iptables -A FORWARD -i eth0 -o tun0 -j ACCEPT
</span></span><span style="display:flex;"><span>iptables -A FORWARD -i tun0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 2. IPv4 NAT masquerade (SNAT for every packet going out through tun0)</span>
</span></span><span style="display:flex;"><span>iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 3. Allow IPv6 ICMP (so Neighbor Discovery Protocol and similar protocols work)</span>
</span></span><span style="display:flex;"><span>ip6tables -I INPUT -p icmpv6 -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -I OUTPUT -p icmpv6 -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 4. IPv6 forwarding rules (allow forwarding in both directions, eth0 &lt;-&gt; tun0)</span>
</span></span><span style="display:flex;"><span>ip6tables -A FORWARD -i eth0 -o tun0 -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -A FORWARD -i tun0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 5. IPv6 NAT masquerade (SNAT for every IPv6 packet going out through tun0)</span>
</span></span><span style="display:flex;"><span>ip6tables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
</span></span></code></pre></div><p>With these rules, downstream traffic (coming from WG-Easy) can be forwarded to the <code>tun0</code> interface inside the Gluetun container and go into the upstream VPN tunnel. Packets coming back from upstream can also get back into the Docker network, reach WG-Easy and then return to the client. That’s how it works as a VPN gateway.</p>
<h2 id="wg-easy-post-up-script-explained">WG-Easy “Post Up” Script Explained</h2>
<p>Besides Gluetun’s network rules, you also need to set up “Post Up” and “Post Down” scripts in WG-Easy. They add routes and ip rules dynamically after the WireGuard tunnel comes up, so all downstream client traffic goes through Gluetun. This example is for WG-Easy v15, because WG-Easy only supports IPv6 from v15 on. If you want to see the configuration for v14, you can check <a href="https://github.com/qdm12/gluetun/discussions/1192#discussioncomment-12689316">the post I published on GitHub</a>.</p>
<p><img alt="Post Up and Post Down hooks in the WG-Easy admin panel" loading="lazy" src="https://img.bktus.com/One7gB5U.webp"></p>
<p>In the current version, v15.0.0, <strong>you need to turn my example into a single line (with the commands separated by semicolons) and fill it into the input box shown in the screenshot</strong>. Or you can write the script into a file, add it to a directory in WG-Easy and reference it from there. Then click <code>Save</code> and wait for WG-Easy to show a message that the configuration was saved. If needed, restart the WG-Easy container after configuring it.</p>
<h3 id="configuration-example">Configuration Example</h3>
<p>Here’s the full example with explanations (note that this needs to be configured on WG-Easy’s web admin page):</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span><span style="color:#75715e"># WG-Easy Post Up script (example)</span>
</span></span><span style="display:flex;"><span>VPN<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>ifconfig | grep -B1 172.31.0.8 | grep -o <span style="color:#e6db74">&#34;^\w*&#34;</span><span style="color:#66d9ef">)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># First, drop all forwarded traffic by default</span>
</span></span><span style="display:flex;"><span>iptables -P FORWARD DROP
</span></span><span style="display:flex;"><span>ip6tables -P FORWARD DROP
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Open the WireGuard UDP port ({{port}} is replaced with the WireGuard port, usually 51820)</span>
</span></span><span style="display:flex;"><span>iptables -A INPUT -p udp -m udp --dport <span style="color:#f92672">{{</span>port<span style="color:#f92672">}}</span> -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -A INPUT -p udp -m udp --dport <span style="color:#f92672">{{</span>port<span style="color:#f92672">}}</span> -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Allow forwarding between the local side and the WireGuard tunnel</span>
</span></span><span style="display:flex;"><span>iptables -A FORWARD -i wg0 -j ACCEPT
</span></span><span style="display:flex;"><span>iptables -A FORWARD -o wg0 -m state --state RELATED,ESTABLISHED -j ACCEPT
</span></span><span style="display:flex;"><span>iptables -A FORWARD -s <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> -d <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> -i wg0 -o wg0 -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>ip6tables -A FORWARD -i wg0 -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -A FORWARD -o wg0 -m state --state RELATED,ESTABLISHED -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -A FORWARD -s <span style="color:#f92672">{{</span>ipv6Cidr<span style="color:#f92672">}}</span> -d <span style="color:#f92672">{{</span>ipv6Cidr<span style="color:#f92672">}}</span> -i wg0 -o wg0 -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Configure the IPv4 routing table (table 200 is used only for downstream traffic)</span>
</span></span><span style="display:flex;"><span>iptables -t nat -A POSTROUTING -s <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> -o $VPN -j MASQUERADE
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Add an ip rule so traffic from {{ipv4Cidr}} uses table 200</span>
</span></span><span style="display:flex;"><span>ip rule add from <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip route add default via 172.31.0.4 dev $VPN table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip route add 172.31.0.0/16 via 172.31.0.1 dev $VPN table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip route add <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> dev wg0 table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Configure the IPv6 routing table (same idea, also table 200)</span>
</span></span><span style="display:flex;"><span>ip -6 rule add from <span style="color:#f92672">{{</span>ipv6Cidr<span style="color:#f92672">}}</span> table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip -6 route add default via fd01:beee:beee::4 dev $VPN table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip -6 route add fd01:beee:beee::/48 via fd01:beee:beee::1 dev $VPN table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip -6 route add <span style="color:#f92672">{{</span>ipv6Cidr<span style="color:#f92672">}}</span> dev wg0 table <span style="color:#ae81ff">200</span>
</span></span></code></pre></div><p>The three variables below are WG-Easy template variables. WG-Easy fills them in automatically, but you need to make sure the values are valid:</p>
<ul>
<li><code>{{port}}</code>: the port WireGuard listens on, usually the same as the port mapped in Docker (51820).</li>
<li><code>{{ipv4Cidr}}</code>: the downstream client subnet, for example <code>10.8.0.0/24</code>. WG-Easy assigns addresses from it to peers.</li>
<li><code>{{ipv6Cidr}}</code>: the downstream client IPv6 subnet, for example <code>fdcc:ad94:bacf:61a4::/64</code>.</li>
</ul>
<p>Here’s the script for PostDown (it simply replaces <code>-A</code> with <code>-D</code> and <code>add</code> with <code>del</code>):</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>VPN<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>ifconfig | grep -B1 172.31.0.8 | grep -o <span style="color:#e6db74">&#34;^\w*&#34;</span><span style="color:#66d9ef">)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>iptables -P FORWARD DROP
</span></span><span style="display:flex;"><span>ip6tables -P FORWARD DROP
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>iptables -D INPUT -p udp -m udp --dport <span style="color:#f92672">{{</span>port<span style="color:#f92672">}}</span> -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -D INPUT -p udp -m udp --dport <span style="color:#f92672">{{</span>port<span style="color:#f92672">}}</span> -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>iptables -D FORWARD -i wg0 -j ACCEPT
</span></span><span style="display:flex;"><span>iptables -D FORWARD -o wg0 -m state --state RELATED,ESTABLISHED -j ACCEPT
</span></span><span style="display:flex;"><span>iptables -D FORWARD -s <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> -d <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> -i wg0 -o wg0 -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>ip6tables -D FORWARD -i wg0 -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -D FORWARD -o wg0 -m state --state RELATED,ESTABLISHED -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -D FORWARD -s <span style="color:#f92672">{{</span>ipv6Cidr<span style="color:#f92672">}}</span> -d <span style="color:#f92672">{{</span>ipv6Cidr<span style="color:#f92672">}}</span> -i wg0 -o wg0 -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>iptables -t nat -D POSTROUTING -s <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> -o $VPN -j MASQUERADE
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>ip rule del from <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip route del default via 172.31.0.4 dev $VPN table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip route del 172.31.0.0/16 via 172.31.0.1 dev $VPN table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip route del <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> dev wg0 table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>ip -6 rule del from <span style="color:#f92672">{{</span>ipv6Cidr<span style="color:#f92672">}}</span> table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip -6 route del default via fd01:beee:beee::4 dev $VPN table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip -6 route del fd01:beee:beee::/48 via fd01:beee:beee::1 dev $VPN table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip -6 route del <span style="color:#f92672">{{</span>ipv6Cidr<span style="color:#f92672">}}</span> dev wg0 table <span style="color:#ae81ff">200</span>
</span></span></code></pre></div><h3 id="using-multiple-docker-bridges">Using Multiple Docker Bridges</h3>
<p>In my environment, WG-Easy is connected to several Docker bridges. Besides <code>vpn</code>, there are other bridges dedicated to other services. If you need to connect WG-Easy to another bridge network, I recommend keeping the <code>$VPN</code> variable from the configuration above instead of using the <code>{{device}}</code> template variable that WG-Easy provides. The <code>$VPN</code> variable automatically picks the network interface for the matching subnet, so the routing and firewall rules only apply to that interface and won’t accidentally affect other network services. For a second Docker bridge, you can add this command to PostUp:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>VPN_IF<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>ifconfig | grep -B1 172.31.0.8 | grep -o <span style="color:#e6db74">&#34;^\w*&#34;</span><span style="color:#66d9ef">)</span>
</span></span></code></pre></div><p>Remember to replace <code>172.31.0.8</code> with the IP of the WG-Easy container on that Docker bridge, which means you need to give WG-Easy a fixed IP on that bridge. Or you can adjust this pattern so grep filters by subnet instead of by IP address. That way, <code>$VPN_IF</code> resolves to the network interface the subnet is using. Then update the routing table and firewall rules as well, so traffic between the service networks stays isolated and still gets forwarded properly:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>iptables -t nat -A POSTROUTING -s &lt;NETWORK_CIDR&gt; -o &lt;NETWORK_INTERFACE&gt; -j MASQUERADE
</span></span><span style="display:flex;"><span>ip6tables -t nat -A POSTROUTING -s &lt;NETWORK_CIDR&gt; -o &lt;NETWORK_INTERFACE&gt; -j MASQUERADE  <span style="color:#75715e"># if this bridge has IPv6</span>
</span></span><span style="display:flex;"><span>ip route add &lt;NETWORK_CIDR&gt; via &lt;NETWORK_GATEWAY&gt; dev &lt;NETWORK_INTERFACE&gt; table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip -6 route add &lt;NETWORK_CIDR&gt; via &lt;NETWORK_GATEWAY&gt; dev &lt;NETWORK_INTERFACE&gt; table <span style="color:#ae81ff">200</span>  <span style="color:#75715e"># if this bridge has IPv6</span>
</span></span></code></pre></div><p>Some readers need to provide VPN service to multiple containers but don’t want to attach multiple Docker bridges to WG-Easy directly. For them, here’s a more fine-grained approach that gives you more control. Bind each service that needs the VPN to its own WireGuard client container (such as linuxserver/wireguard or an image like Gluetun), then use WG-Easy (or a similar admin interface) to create a separate WireGuard account for each service. Finally, import the matching configuration into that service’s WireGuard client container. The advantages are:</p>
<ul>
<li>Each service has its own WireGuard configuration file, so traffic isolation is more thorough;</li>
<li>You can monitor and limit a single service’s network access more directly in WG-Easy;</li>
<li>You no longer need rules like <code>iptables -A FORWARD -i eth+ -o tun0</code> that let everything through a network interface. Instead, you only grant trust to specific containers.</li>
</ul>
<p>This approach gives you finer-grained permission control for each container, for example real-time traffic monitoring, or revoking a service’s VPN access at any time. But running multiple WireGuard client containers does cost extra CPU and memory.</p>
<h2 id="deployment">Deployment</h2>
<h3 id="create-and-load-the-environment-variables">Create and Load the Environment Variables</h3>
<p>In the same directory as docker-compose.yml, create a .env file with the following content (see the Gluetun documentation linked above):</p>
<pre tabindex="0"><code>VPN_SERVICE_PROVIDER=XXX
VPN_TYPE=XXXX
WIREGUARD_PRIVATE_KEY=YOUR_PRIVATE_KEY
WIREGUARD_PRESHARED_KEY=YOUR_PSK
WIREGUARD_ADDRESSES=XXX
SERVER_REGIONS=XXX
</code></pre><h3 id="make-sure-the-docker-network-exists">Make Sure the Docker Network Exists</h3>
<p>As mentioned earlier, make sure the custom network named <code>vpn</code> has been created.</p>
<h3 id="start-the-containers">Start the Containers</h3>
<p>Run the command below and watch the startup logs of the wg-easy and gluetun-ws containers, to confirm that both the WireGuard client and the server come up properly.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>$ docker-compose up -d
</span></span></code></pre></div><h3 id="add-a-wireguard-client">Add a WireGuard Client</h3>
<ul>
<li>Open <code>http://&lt;WG-Easy-container-IP&gt;:51821</code> in a browser (depending on your WG-Easy configuration) to get to WG-Easy’s web interface. Or you can add a TCP mapping for port 51821 in Docker Compose and open the web admin interface at <code>http://&lt;host-IP&gt;:51821</code>. Using Nginx as a reverse proxy works too.</li>
<li>In the Hooks section of the Admin Panel, fill in the Post Up and Post Down configuration given above and save it. <strong>This step is really important.</strong></li>
<li>Create a new Peer (enter a name for it). WG-Easy generates the matching configuration file with AllowedIPs = 0.0.0.0/0, ::/0, and you download the .conf file.</li>
<li>Import this configuration on your local device (Windows/Mac/Linux/phone), start WireGuard and test it.</li>
</ul>
<h3 id="verify-the-vpn2vpn-tunnel">Verify the VPN2VPN Tunnel</h3>
<ul>
<li>After the client connects to WireGuard, open <a href="https://ipleak.net">https://ipleak.net</a> or <a href="https://ifconfig.co">https://ifconfig.co</a> and check the exit IP.</li>
<li>If it shows the IP of the upstream commercial VPN, the chained VPN tunnel is working.</li>
<li>You can run <code>curl ifconfig.co</code> in the Gluetun container, and you should see the public IP from the commercial VPN. Run the same thing in the WG-Easy container, and you should see the same exit IP (through the upstream VPN).</li>
</ul>
<h3 id="check-traffic-routing">Check Traffic Routing</h3>
<ul>
<li>In the WG-Easy container, run <code>ip rule show</code> / <code>ip route show table 200</code> to confirm that routing table 200 is in effect and the default route points to 172.31.0.4 (Gluetun).</li>
<li>In the Gluetun container, run <code>iptables -t nat -L -nv</code>, and you’ll see the matching MASQUERADE rules.</li>
<li>If you need to troubleshoot, run <code>tcpdump -i</code> on the relevant interface in each container to capture packets and check what the traffic is doing.</li>
</ul>
<h2 id="notes-and-considerations">Notes and Considerations</h2>
<h3 id="dns-leaks">DNS Leaks</h3>
<p>In this example, if you don’t take extra DNS security measures, there’s a risk of DNS leaks. When a container or client uses the host’s DNS or a public DNS directly, DNS queries may bypass the VPN tunnel and go upstream directly, which exposes your real IP and the websites you visit. Actually, Gluetun creates its own internal DNS server and forces all DNS traffic through it, so that using the local DNS doesn’t cause DNS leaks. But by default Gluetun only listens for DNS requests inside its own container, so other containers and the host can’t reach it. This also means WG-Easy can’t use Gluetun’s internal DNS service directly, so we can’t simply forward WG-Easy’s DNS traffic to Gluetun.</p>
<p>To avoid these problems, you usually need to deploy a separate DNS service (such as AdGuard Home or Pi-hole) and send all DNS queries through it, with upstream resolution going out through the VPN exit. This way, DNS queries also go through the VPN, and you get ad blocking and malicious domain filtering as well.</p>
<h4 id="deploy-adguard-home-or-a-similar-dns-service">Deploy AdGuard Home or a Similar DNS Service</h4>
<p>We can run a separate AdGuard Home container in the same Docker network (the <code>vpn</code> network, for example). AdGuard Home listens on both UDP and TCP port 53, and it has stronger features like ad blocking and malicious domain filtering. For example, you can add a service like the one below to docker-compose.yml (just as a demonstration):</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#f92672">adguard</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">image</span>: <span style="color:#ae81ff">adguard/adguardhome</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">restart</span>: <span style="color:#ae81ff">unless-stopped</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">networks</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">vpn</span>:
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">ipv4_address</span>: <span style="color:#ae81ff">172.31.0.16</span>
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">ipv6_address</span>: <span style="color:#e6db74">&#34;fd01:beee:beee::16&#34;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">ports</span>:
</span></span><span style="display:flex;"><span>    - <span style="color:#e6db74">&#34;53:53/udp&#34;</span>
</span></span><span style="display:flex;"><span>    - <span style="color:#e6db74">&#34;53:53/tcp&#34;</span>
</span></span><span style="display:flex;"><span>    - <span style="color:#e6db74">&#34;3000:3000/tcp&#34;</span> <span style="color:#75715e"># Web interface port</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">volumes</span>:
</span></span><span style="display:flex;"><span>    - <span style="color:#ae81ff">/data/adguard/workdir:/opt/adguardhome/work</span>
</span></span><span style="display:flex;"><span>    - <span style="color:#ae81ff">/data/adguard/conf:/opt/adguardhome/conf</span>
</span></span></code></pre></div><p>This way, AdGuard Home is exposed on port 53 of the host and also gets an IP in the Docker <code>vpn</code> network (here, 172.31.0.16). After that, we can reach this AdGuard Home container directly from WG-Easy, from client devices and even from other containers. You can also choose not to expose port 53 at all and only provide DNS through the <code>vpn</code> bridge.</p>
<p>Open AdGuard Home’s web interface (usually at <code>http://&lt;Host-IP&gt;:3000</code>) and check that the upstream DNS settings are correct and that ad blocking and the other policies work. Then you need to test it. Ideally, run <code>nslookup hostname 172.31.0.16</code> in the WG-Easy container to check whether the DNS path from WG-Easy to AdGuard Home works.</p>
<h4 id="change-the-dns-settings">Change the DNS Settings</h4>
<p>After AdGuard Home is deployed, configured and tested, we need to adjust the client DNS settings that WG-Easy generates, because by default WG-Easy sets the DNS servers to <code>1.1.1.1</code> and <code>2606:4700:4700::1111</code>. To do this, change the DNS in WG-Easy’s Admin Panel to the IPs of the AdGuard Home container: <code>172.31.0.16</code> and <code>fd01:beee:beee::16</code>.</p>
<p><img alt="DNS settings in the WG-Easy admin panel" loading="lazy" src="https://img.bktus.com/pinDVKdk.webp"></p>
<p>Note that the DNS settings of devices you’ve already added don’t follow the DNS settings in the Admin Panel automatically. You need to update the DNS settings in WireGuard on the device by hand, or adjust that device separately in WG-Easy, then download the configuration file again and apply it to the device.</p>
<p>Finally, to strictly limit WG-Easy’s outbound DNS access and rule out DNS leaks, we can add the following commands to the PostUp hook script:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span><span style="color:#75715e"># Allow packets to the AdGuard Home DNS port</span>
</span></span><span style="display:flex;"><span>iptables -A OUTPUT -o $VPN -d 172.31.0.16 -p udp --dport <span style="color:#ae81ff">53</span> -j ACCEPT
</span></span><span style="display:flex;"><span>iptables -A OUTPUT -o $VPN -d 172.31.0.16 -p tcp --dport <span style="color:#ae81ff">53</span> -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -A OUTPUT -o $VPN -d fd01:beee:beee::16 -p udp --dport <span style="color:#ae81ff">53</span> -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -A OUTPUT -o $VPN -d fd01:beee:beee::16 -p tcp --dport <span style="color:#ae81ff">53</span> -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Reject DNS queries from the rest of the network stack, so nothing leaks to the host</span>
</span></span><span style="display:flex;"><span>iptables -A OUTPUT -p tcp --dport <span style="color:#ae81ff">53</span> -j REJECT
</span></span><span style="display:flex;"><span>iptables -A OUTPUT -p udp --dport <span style="color:#ae81ff">53</span> -j REJECT
</span></span><span style="display:flex;"><span>ip6tables -A OUTPUT -p tcp --dport <span style="color:#ae81ff">53</span> -j REJECT
</span></span><span style="display:flex;"><span>ip6tables -A OUTPUT -p udp --dport <span style="color:#ae81ff">53</span> -j REJECT
</span></span></code></pre></div><p>The matching PostDown script is:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>iptables -D OUTPUT -o $VPN -d 172.31.0.16 -p udp --dport <span style="color:#ae81ff">53</span> -j ACCEPT
</span></span><span style="display:flex;"><span>iptables -D OUTPUT -o $VPN -d 172.31.0.16 -p tcp --dport <span style="color:#ae81ff">53</span> -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -D OUTPUT -o $VPN -d fd01:beee:beee::16 -p udp --dport <span style="color:#ae81ff">53</span> -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -D OUTPUT -o $VPN -d fd01:beee:beee::16 -p tcp --dport <span style="color:#ae81ff">53</span> -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>iptables -D OUTPUT -p tcp --dport <span style="color:#ae81ff">53</span> -j REJECT
</span></span><span style="display:flex;"><span>iptables -D OUTPUT -p udp --dport <span style="color:#ae81ff">53</span> -j REJECT
</span></span><span style="display:flex;"><span>ip6tables -D OUTPUT -p tcp --dport <span style="color:#ae81ff">53</span> -j REJECT
</span></span><span style="display:flex;"><span>ip6tables -D OUTPUT -p udp --dport <span style="color:#ae81ff">53</span> -j REJECT
</span></span></code></pre></div><h3 id="ipv6-support">IPv6 Support</h3>
<ul>
<li>If you don’t use IPv6, you can remove <code>ipv6_address</code>, the ip6tables rules and the other related settings from docker-compose.yml, and don’t enable IPv6 when you create the network.</li>
<li>If you do use IPv6, make sure both the host and the VPN provider support IPv6 tunnels.</li>
</ul>
<h3 id="performance-and-resources">Performance and Resources</h3>
<p>WireGuard itself is fairly demanding on the CPU, especially when it runs at full speed, so it needs reasonably good hardware.</p>
<h3 id="multi-hop">Multi-Hop</h3>
<p>This architecture is already a double hop (downstream → WG-Easy → Gluetun → upstream VPN → Internet). If you want to go further and set up more hops, you can pick a different region for the upstream VPN, or stack another VPN container on top. But keep in mind that performance goes down and maintenance gets more complex.</p>
<h3 id="firewall-and-security">Firewall and Security</h3>
<p>In this example, the WG-Easy container only opens UDP port 51820 by default. If you need to access it from the public internet, open that port in the host firewall (iptables/nftables) and only allow trusted IPs, to keep the attack surface small.</p>
<h3 id="postdown-script">PostDown Script</h3>
<p>To avoid leaving rules behind after teardown, make sure to add the matching <code>iptables -D</code>, <code>ip rule del</code>, <code>ip route del</code> and similar commands to WG-Easy’s PostDown. Otherwise you may end up with duplicate routes or conflicts after a restart.</p>
<h2 id="network-troubleshooting-example">Network Troubleshooting Example</h2>
<p>When a client can’t reach the LAN or the Internet, here are some common steps to find the problem quickly. This is also what I usually do myself. I believe that by going from coarse to fine like this, you can find the root cause of most network connection problems.</p>
<p>Below are the concrete steps. I only show the IPv4 commands here, but for IPv6 you can easily do the same checks by adjusting the commands a little.</p>
<h3 id="ping-tests-in-stages-local">Ping Tests in Stages (Local)</h3>
<ul>
<li><code>ping 10.8.0.1</code>: check whether the VPN tunnel is up.</li>
<li><code>ping 172.31.0.8</code>: check whether the client can reach the WG-Easy container (Docker bridge network).</li>
<li><code>ping 172.31.0.4</code>: check connectivity from the bridge network to the Gluetun container.</li>
<li><code>ping 8.8.8.8</code>: test whether Gluetun can reach the Internet.</li>
</ul>
<p>If a step fails, check the routing or firewall rules for that specific step.</p>
<h3 id="check-dns-resolution-local">Check DNS Resolution (Local)</h3>
<p>If you pass the tests above, the path is fine at the IP level, so next we look at DNS.</p>
<ul>
<li><code>nslookup example.com</code>: through the default DNS server.</li>
<li><code>nslookup example.com 172.31.0.16</code>: through AdGuard Home.</li>
<li><code>nslookup example.com 8.8.8.8</code>: test whether public DNS works.</li>
</ul>
<p>If you can’t get a correct response, the DNS request isn’t reaching the server it should reach.</p>
<h3 id="debug-inside-the-containers">Debug Inside the Containers</h3>
<p>If DNS is also fine, the problem may be a deeper conflict in the routing tables or firewall rules. We can go into the WG-Easy or the Gluetun container to debug:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>docker exec -it &lt;container-name/id-of-wg-easy&gt; /bin/bash
</span></span><span style="display:flex;"><span>docker exec -it &lt;container-name/id-of-gluetun&gt; /bin/sh
</span></span></code></pre></div><p>Run these inside the container:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>ip rule show             <span style="color:#75715e"># Check whether the routing rules are correct</span>
</span></span><span style="display:flex;"><span>ip route show table <span style="color:#ae81ff">200</span>  <span style="color:#75715e"># Verify routing table 200</span>
</span></span><span style="display:flex;"><span>ping -c <span style="color:#ae81ff">2</span> 8.8.8.8        <span style="color:#75715e"># Test outbound access from the container</span>
</span></span><span style="display:flex;"><span>ping -c <span style="color:#ae81ff">2</span> example.com    <span style="color:#75715e"># Test DNS inside the container</span>
</span></span><span style="display:flex;"><span>iptables-save            <span style="color:#75715e"># Check the firewall rules</span>
</span></span><span style="display:flex;"><span>ip route                 <span style="color:#75715e"># Verify the default routing table</span>
</span></span></code></pre></div><h3 id="more-advanced-methods-tcpdump-routing-tables-and-firewall-checks">More Advanced Methods: tcpdump, Routing Tables and Firewall Checks</h3>
<p>If these simple methods still can’t find the problem, there’s probably a deeper conflict in the routing table or firewall configuration. At this point, you can use <code>tcpdump -i &lt;interface&gt;</code> to capture packets and see whether DNS (UDP/TCP 53) or other traffic reaches the container or gets dropped. Then carefully check the routing tables on the host and in the containers (<code>ip rule show</code>, <code>ip route show</code>) for overlapping or conflicting entries, and make sure the ip rule priorities and table entries actually take effect. After that, go through the iptables/ip6tables rules again, especially the FORWARD, OUTPUT and nat tables, to see whether a wrong or duplicate rule is blocking the traffic.</p>
<h3 id="problems-caused-by-updates">Problems Caused by Updates</h3>
<p>Keep in mind that your problem may also come from an image update of one of the containers. In that case, check whether there was an automatic update recently, and try downgrading the version to fix it. If the problem goes away, you can pin the old image version to keep the service working until the issue is finally solved.</p>
]]></content:encoded></item></channel></rss>