<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Traefik | blog.moe.ph</title><link>https://blog.moe.ph/tags/traefik/</link><atom:link href="https://blog.moe.ph/tags/traefik/index.xml" rel="self" type="application/rss+xml"/><description>Traefik</description><generator>Hugo Blox Builder (https://hugoblox.com)</generator><language>en-us</language><lastBuildDate>Wed, 06 Nov 2019 00:00:00 +0000</lastBuildDate><image><url>https://blog.moe.ph/media/logo_hu_230979b687bad52e.png</url><title>Traefik</title><link>https://blog.moe.ph/tags/traefik/</link></image><item><title>Site Redirection in Traefik using RedirectRegex Middleware</title><link>https://blog.moe.ph/blog/site-redirection-in-traefik-using-redirectregex-middleware/</link><pubDate>Wed, 06 Nov 2019 00:00:00 +0000</pubDate><guid>https://blog.moe.ph/blog/site-redirection-in-traefik-using-redirectregex-middleware/</guid><description>&lt;p&gt;Implementing URL redirection in Traefik works like normal service with middleware processing. To setup redirection, setup the base router with middleware, and some dummy service.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;http:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; routers:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; shop-secure:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; entryPoints:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - &amp;#34;web-fe-secure&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; rule: &amp;#34;Host(`shop.moe.ph`)&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; middlewares:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - &amp;#34;shopredirect&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; service: &amp;#34;shop-redirect&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; tls: {}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For middleware, you will need to use redirectRegex middleware processor. Just put the URL that you want to process, and it&amp;rsquo;s replacement.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;http:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; middlewares:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; shopredirect:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; redirectRegex:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; regex: &amp;#34;^https://shop.moe.ph/(.*)&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; replacement: &amp;#34;https://shopee.ph/moe_ph&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Unfortunately, this router-middleware processing of URL redirection still needs service block. If you are redirecting your URL to another domain, just put a dummy service.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-gdscript3" data-lang="gdscript3"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;shop&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;loadBalancer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;servers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;http://localhost/&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To do more complex redirection, just check the Traefik&amp;rsquo;s
documentation.&lt;/p&gt;</description></item><item><title>King Traefik 2.0, Queen Django, and Jack passHostHeader</title><link>https://blog.moe.ph/blog/king-traefik-20-queen-django-and-jack-passhostheader/</link><pubDate>Fri, 30 Aug 2019 00:00:00 +0000</pubDate><guid>https://blog.moe.ph/blog/king-traefik-20-queen-django-and-jack-passhostheader/</guid><description>&lt;p&gt;Deploying Django application means deploying it behind Nginx webserver most of the time. We are also using this setup. Deploying it this way is easy, since you need to set &lt;i&gt;proxy_pass&lt;/i&gt; and &lt;i&gt;proxy_set_header&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;The only problem with this setup is when you only have 1 Nginx webserver for multiple Django backend. When you deploy new Django apps, you need to add new setting for it and restart the server. You might be able to reload it by sending SIGHUP to Nginx master process, but we are using container, so it&amp;rsquo;s hassle. You also need to reload it every time you edited something.&lt;/p&gt;
&lt;p&gt;That’s when we look into alternative reverse proxy. We look for more cloud-native proxy, and that’s when we found about Traefik. Traefik is a cloud native edge router. It has integrations into multiple cluster technology. It also supports live reload of configuration files for dynamic settings. We are using podman to manage containers, so we are utilizing the dynamic file reload.&lt;/p&gt;
&lt;p&gt;We start playing with Traefik, and use version 2. Tinkering with Traefik is not that easy if you’re not familiar with the earlier version.&lt;/p&gt;
&lt;p&gt;The setting that really ticked our testing is passing the proxy host header to backend. The first settings that we look through are middleware headers settings of Traefik. This is the most logical place to look first, if you are playing with headers, right? We played with these settings.&lt;/p&gt;
&lt;p&gt;Seems okay, but it didn’t actually work. The host header is still not pass to the backend. I hold some grudge to Traefik this time, then I realized that I need to check the settings for earlier version. There are Traefik settings scattered through Github, so I found this boolean setting.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;passHostHeader = true
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I guess this setting should be present to Traefik 2.0. After checking the dynamic configuration reference, I found this setting.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-gdscript3" data-lang="gdscript3"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;Service01&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;loadBalancer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;passHostHeader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;true&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I set this to true and the backend is now responding.&lt;/p&gt;
&lt;p&gt;I guess everyone should know about this setting.&lt;/p&gt;</description></item></channel></rss>