<metaproperty="og:title"content="Setup dns with adblock and dot/doh with pi-hole and unbound">
<metaproperty="og:description"content="Just another day I seted up my own private dns server that has adblocking ( and other stuff ) using pihole and uses unbound as a resolver. To safelly connect to the dns server I&rsquo;m using DNS over HTTPS for my browser&rsquo;s and HTTPS over TLS for stuffy for my whole desktop and private dns in android ( Android has DoH support but only for google and cloudflare right now). Let&rsquo;s get on to setting everything up">
<p>Just another day I seted up my own private dns server that has adblocking ( and other stuff ) using pihole and uses unbound as a resolver. To safelly connect to the dns server I’m using DNS over HTTPS for my browser’s and HTTPS over TLS for stuffy for my whole desktop and private dns in android ( Android has DoH support but only for google and cloudflare right now). Let’s get on to setting everything up</p>
<p>Let’s start with setting up pihole. I will be installing it with their script on a debian system for easier unbound integration ( unbound doesn’t have an official docker container ).</p>
<p>I recommend to read up on the pihole’s docs on exactly how to install it since pihole get’s frequent updates. <ahref="https://docs.pi-hole.net/main/basic-install/">DOCS</a></p>
<p>I recommend you to install the admin page for easier managmenet and ability to change the upstream dns server ( needed for changing it to unbound later on ). To be able to access the admin page I use an nginx configuration like this one.</p>
</span></span><spanstyle="display:flex;"><span><spanstyle="color:#81c8be">listen</span><spanstyle="color:#a6d189">[::]:443</span><spanstyle="color:#a6d189">ssl</span>; <spanstyle="color:#737994;font-style:italic"># managed by Certbot
</span></span></span><spanstyle="display:flex;"><span><spanstyle="color:#737994;font-style:italic"></span><spanstyle="color:#81c8be">listen</span><spanstyle="color:#ef9f76">443</span><spanstyle="color:#a6d189">ssl</span>; <spanstyle="color:#737994;font-style:italic"># managed by Certbot
</span></span></span><spanstyle="display:flex;"><span><spanstyle="color:#737994;font-style:italic"></span><spanstyle="color:#81c8be">ssl_certificate</span><spanstyle="color:#a6d189">/etc/letsencrypt/live/example.com/fullchain.pem</span>; <spanstyle="color:#737994;font-style:italic"># managed by Certbot
</span></span></span><spanstyle="display:flex;"><span><spanstyle="color:#737994;font-style:italic"></span><spanstyle="color:#81c8be">ssl_certificate_key</span><spanstyle="color:#a6d189">/etc/letsencrypt/live/example.com/privkey.pem</span>; <spanstyle="color:#737994;font-style:italic"># managed by Certbot
</span></span></span><spanstyle="display:flex;"><span><spanstyle="color:#737994;font-style:italic"></span><spanstyle="color:#81c8be">include</span><spanstyle="color:#a6d189">/etc/letsencrypt/options-ssl-nginx.conf</span>; <spanstyle="color:#737994;font-style:italic"># managed by Certbot
</span></span></span><spanstyle="display:flex;"><span><spanstyle="color:#737994;font-style:italic"></span><spanstyle="color:#81c8be">ssl_dhparam</span><spanstyle="color:#a6d189">/etc/letsencrypt/ssl-dhparams.pem</span>; <spanstyle="color:#737994;font-style:italic"># managed by Certbot
</span></span><spanstyle="display:flex;"><span><spanstyle="color:#81c8be">return</span><spanstyle="color:#ef9f76">404</span>; <spanstyle="color:#737994;font-style:italic"># managed by Certbot
</span></span></code></pre></div><p>The main point of this config is the <code>/admin</code> location that you need to pass the lighttpd port to acces the website, you can just do it on your main website also.
Also to make lighttpd work with nginx listening on port 80 you need to edit the <code>server.port</code> to port you wan’t to use in lighttpd config file located at <code>/etc/lighttpd/lighttpd.conf</code> and then just restart lighttpd</p>
<h2id="2-unbound">2. Unbound</h2>
<p>For this part I will just link the pi-hole’s unbound documentation because it is the most correct one and updated as things change regulary.
<p>For dns over tls you need to first have a ssl certificate. I recommend on using certbot to generate one with this command <code>certbot --nginx -d dot.example.com</code>.</p>
<p>Next you will need a reverse proxy, in my case I use nginx. You will need to add this configuration to your main nginx config located at <code>/etc/nginx/nginx.conf</code>. <strong>Make sure to add this outside of the http block and change example.com to your domain</strong></p>
</span></span></code></pre></div><p>Also make sure to enable port 853, example ufw command is <code>ufw allow 853/tcp</code>. Then restart nginx, to test if this configuration is working you can use your android phone by setting the private dns address to <code>dot.example.com</code> and then visit the website<ahref="https://dnsleaktest.com">dnsleaktest</a></p>
<h2id="4-dns-over-https">4. DNS over HTTPS</h2>
<p>For using dns over https we will be installing additional package called dnsdinst. On debian systems just run <code>apt install dnsdinst</code>. Next you will need to setup dnsdinst config and restart it. Make sure to change example.com.</p>
</code></pre><p>Next we will need another ssl certificate for the doh domain, for that we will once again using certbot with this command <code>certbot --nginx -d doh.example.com</code>after that add this configuratin to nginx either in sites-available and linking it to sites enabled or in http block in main nginx configuration.</p>
<divclass="highlight"><pretabindex="0"style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><codeclass="language-nginx"data-lang="nginx"><spanstyle="display:flex;"><span><spanstyle="color:#737994;font-style:italic"># Proxy Cache storage - so we can cache the DoH response from the upstream
</span></span><spanstyle="display:flex;"><span><spanstyle="color:#737994;font-style:italic"># Return 404 to all responses, except for those using our published DoH URI
</span></span><spanstyle="display:flex;"><span><spanstyle="color:#737994;font-style:italic"># Proxy HTTP/1.1, clear the connection header to enable Keep-Alive
</span></span><spanstyle="display:flex;"><span><spanstyle="color:#737994;font-style:italic"># Enable Cache, and set the cache_key to include the request_body
</span></span></code></pre></div><p>After restarting nginx with this configuration you can it to your web browser as a DNS over HTTPS resolver and once again checkout <ahref="https://dnsleaktest.com">dnsleaktest</a> website and check if it is all working.</p>
<p>Hope this has been helpfull and if anybody has any way on how to make this guied better you can open a pull request or make an issue on the website’s <ahref="https://code.cronyakatsuki.xyz/crony/website">repo</a>.</p>