{"id":187,"date":"2023-03-06T19:03:00","date_gmt":"2023-03-06T18:03:00","guid":{"rendered":"http:\/\/192.168.41.160:8081\/?p=187"},"modified":"2024-10-21T15:19:50","modified_gmt":"2024-10-21T13:19:50","slug":"haproxy-reverse-proxy-ssl-termination-ip-forwarding","status":"publish","type":"post","link":"https:\/\/timo-brunn.de\/index.php\/2023\/03\/06\/haproxy-reverse-proxy-ssl-termination-ip-forwarding\/","title":{"rendered":"HAProxy Reverse Proxy (+SSL Termination, IP Forwarding)"},"content":{"rendered":"\n<div class=\"wp-block-columns alignwide is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"has-text-align-right wp-block-post-date\"><time datetime=\"2023-03-06T19:03:00+01:00\">M\u00e4rz 6, 2023<\/time><\/div>\n\n\n<h2 class=\"wp-block-heading\">Vorwort<\/h2>\n\n\n\n<p>Ich hatte mir selbst die Aufgabe gesetzt, nach meiner Wireguard umstellung, meinen reinen Reverse Proxy auf einen HAProxy umzuziehen.<\/p>\n\n\n\n<p>Falls eine Anleitung zu einem NGINX Reverse Proxy gebraucht wird, kann gerne dieser Beitrag befolgt werden: <a href=\"\/index.php\/2022\/01\/07\/nginx-reverse-proxy-ssl-termination-ip-forwarding-proxy-caching\/\" data-type=\"URL\" data-id=\"http:\/\/192.168.41.160:8081\/index.php\/2022\/01\/07\/nginx-reverse-proxy-ssl-termination-ip-forwarding-proxy-caching\/\" target=\"_blank\" rel=\"noreferrer noopener\">NGINX Reverse Proxy (+SSL Termination, IP Forwarding &amp; Proxy Caching)<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Vorbereitung<\/h2>\n\n\n\n<p>Als Betriebssystem kommt diesmal ein Ubuntu 22.04 Minimal zum Einsatz. Die neuste LTS Version zum aktuellen Zeitpunkt.<br>Die Version von HAProxy in den AlmaLinux Repositories ist leider &#8222;uralt&#8220; und ist laut HAProxy schon gar nicht mehr Unterst\u00fctzt. (Version 1.8)<\/p>\n\n\n\n<p>In den aktuellen Ubuntu 22.04 Repositorys ist Version 2.4 enthalten:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">timo@shdefsnhaproxy-01:~$ apt show haproxy\nPackage: haproxy\nVersion: 2.4.18-0ubuntu1.2\nPriority: optional\nSection: net\nOrigin: Ubuntu\nMaintainer: Ubuntu Developers &lt;ubuntu-devel-discuss@lists.ubuntu.com&gt;\nOriginal-Maintainer: Debian HAProxy Maintainers &lt;team+haproxy@tracker.debian.org&gt;\n<\/code><\/pre>\n\n\n\n<p>Ich m\u00f6chte selbst allerdings Version 2.6 installieren da diese HTTP3 over QUIC unterst\u00fctzt.<br>Das k\u00f6nnen wir unter Ubuntu ganz einfach, wie folgend machen:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">sudo add-apt-repository ppa:vbernat\/haproxy-2.6<\/code><\/pre>\n\n\n\n<p>Das ganze dann mit Y bzw. J best\u00e4tigen und fertig ist die Installation des Repositorys.<\/p>\n\n\n\n<p>Wenn wir uns die Version jetzt noch einmal anschauen, sehen wir das wir nun mit Version 2.6.9 unterwegs sind:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">timo@shdefsnhaproxy-01:~$ apt show haproxy\nPackage: haproxy\nVersion: 2.6.9-1ppa1~jammy\nPriority: optional\nSection: net\nMaintainer: Debian HAProxy Maintainers &lt;team+haproxy@tracker.debian.org&gt;\nInstalled-Size: 4012 kB\n<\/code><\/pre>\n\n\n\n<p>Das dann einfach wie man es kennt installieren:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">sudo apt install haproxy<\/code><\/pre>\n\n\n\n<p>Wieder mit Y best\u00e4tigen und fertig ist die HAProxy Installation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Konfiguration von HAProxy<\/h2>\n\n\n\n<p>Die Konfiguration von HAProxy finde ich selbst straight forward.<\/p>\n\n\n\n<p>Sie besteht aus den 4 &#8222;Kategorien&#8220; <br>&#8211; global<br>&#8211; default<br>&#8211; frontend<br>&#8211; backend<\/p>\n\n\n\n<p>Unter Frontend erstellen wir die &#8222;Schnittstellen&#8220;, mit denen unser HAProxy am Ende vom Internet aus angesprochen wird.<br>Backend beschreibt die eigentlichen Webserver.<\/p>\n\n\n\n<p>In meinem Falle muss ich global und default nicht anfassen, <a href=\"https:\/\/cbonte.github.io\/haproxy-dconv\/2.6\/configuration.html#5.2-check\" data-type=\"URL\" data-id=\"https:\/\/cbonte.github.io\/haproxy-dconv\/2.6\/configuration.html#5.2-check\" target=\"_blank\" rel=\"noreferrer noopener\">man kann hier allerdings auch die check variable f\u00fcr healthchecks als &#8222;default&#8220; angeben.<\/a><\/p>\n\n\n\n<p>Die Konfiguration k\u00f6nnen wir unter ubuntu so bearbeiten:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">sudo nano \/etc\/haproxy\/haproxy.cfg<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Reverse Proxy konfiguration<\/h2>\n\n\n\n<p>Um HAProxy als Reverse Proxy nutzen zu k\u00f6nnen, m\u00fcssen wir ein Frontend und ein Backend erstellen:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"nginx\" class=\"language-nginx\">frontend default\n   mode http\n   bind :80\n\n   default_backend timo-web\n\nbackend timo-web\n  mode http\n  server timo-web 192.168.43.12:80<\/code><\/pre>\n\n\n\n<p>Mit dieser Konfiguration wird nun jeder Traffic der beim HAProxy auf Port 80 ankommt (und HTTP spricht) auf das backend <strong><em>timo-web<\/em><\/strong> umgeleitet.<\/p>\n\n\n\n<p>Im Normalfall haben wir allerdings mehrere Backends mit verschiedenen Domains. <br>F\u00fcr eine Konfiguration mit 2 Domains sieht das entsprechend wie folgend aus:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"nginx\" class=\"language-nginx\">frontend default\n   mode http\n   bind :80\n\n   use_backend timo-web if { hdr(host) eq timo-brunn.de }\n   use_backend timo-data if { hdr(host) eq data.timo-brunn.de }\n   default_backend redirect_to_blank\n\nbackend redirect_to_blank\n   mode http\n   http-request redirect location https:\/\/www.youtube.com\/watch?v=dQw4w9WgXcQ code 302\n\n\nbackend timo-web\n  mode http\n  server timo-web 192.168.43.12:80\n\nbackend timo-data\n  mode http\n  server timo-data 192.168.43.13:80<\/code><\/pre>\n\n\n\n<p>Mit obenstehender Konfiguration \u00e4u\u00dfert sich das jetzt wie folgend:<br>HTTP Traffic an die Domain Timo-Brunn.de wird an den Webserver 192.168.43.12 weitergeleitet<br>Traffic f\u00fcr die Domain data.Timo-Brunn.de wird an 192.168.43.13 weitergeleitet.<br><br>Und Traffic, der ohne Host Header ankommt, wird zu YouTube umgeleitet.<\/p>\n\n\n\n<p>Wichtig ist hierbei, dass HAProxy den Host Header \u00fcberpr\u00fcft, der im HTTP Request steht.<br>Dieser kann bei speziellen Ports auch mit Port erscheinen!<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"456\" height=\"148\" src=\"http:\/\/192.168.41.160:8081\/wp-content\/uploads\/2023\/03\/image.png\" alt=\"\" class=\"wp-image-199\" srcset=\"https:\/\/timo-brunn.de\/wp-content\/uploads\/2023\/03\/image.png 456w, https:\/\/timo-brunn.de\/wp-content\/uploads\/2023\/03\/image-300x97.png 300w\" sizes=\"auto, (max-width: 456px) 100vw, 456px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">SSL Termination<\/h2>\n\n\n\n<p>SSL Termination ist beim HAProxy ziemlich einfach. Hier muss auf dem Frontend lediglich SSL aktiviert werden und der Client darauf umgeleitet werden. <br>Unser Beispiel sieht danach so aus:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"nginx\" class=\"language-nginx\">frontend default\n   mode http\n   bind :80\n   bind :443 ssl crt \/srv\/cert\/ alpn h2,http\/1.1\n   http-request redirect scheme https unless { ssl_fc }\n\n   use_backend timo-web if { hdr(host) eq timo-brunn.de }\n   use_backend timo-data if { hdr(host) eq data.timo-brunn.de }\n   default_backend redirect_to_blank\n\nbackend redirect_to_blank\n   mode http\n   http-request redirect location https:\/\/www.youtube.com\/watch?v=dQw4w9WgXcQ code 302\n\n\nbackend timo-web\n  mode http\n  server timo-web 192.168.43.12:80\n\nbackend timo-data\n  mode http\n  server timo-data 192.168.43.13:80<\/code><\/pre>\n\n\n\n<p>Die SSL Zertifikate lege ich hier im Beispiel einfach in dem Ordner <em><strong>\/srv\/cert\/<\/strong><\/em> ab.<br>Man muss nicht direkt auf das Zertifikat zielen, kann man aber.<\/p>\n\n\n\n<p><strong><em><a rel=\"noreferrer noopener\" href=\"https:\/\/cbonte.github.io\/haproxy-dconv\/2.6\/configuration.html#5.2-alpn\" target=\"_blank\">alpn <\/a><\/em><\/strong>aktiviert bestimmte Protokolle auf dem Frontend. In diesem Beispiel:<br>&#8211; http\/1.1 -&gt; HTTP Version 1.1<br>&#8211; h2 -&gt; HTTP\/2 <br>&#8211; h3 -&gt; HTTP\/3 (<a href=\"https:\/\/www.haproxy.com\/blog\/announcing-haproxy-2-6\/\" target=\"_blank\" rel=\"noreferrer noopener\">ohne QUIC<\/a>)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">IP Forwarding<\/h2>\n\n\n\n<p>IP Forwarding unter HAProxy ist so simple das selbst ein Baby das schafft \ud83d\ude09<br>Hierf\u00fcr setzt man einfach die Option &#8222;forwardfor&#8220;.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"nginx\" class=\"language-nginx\">frontend default\n   mode http\n   bind :80\n   bind :443 ssl crt \/srv\/cert\/ alpn h3,h2,http\/1.1\n   http-request redirect scheme https unless { ssl_fc }\n\n   use_backend timo-web if { hdr(host) eq timo-brunn.de }\n   use_backend timo-data if { hdr(host) eq data.timo-brunn.de }\n   default_backend redirect_to_blank\n\nbackend redirect_to_blank\n   mode http\n   http-request redirect location https:\/\/www.youtube.com\/watch?v=dQw4w9WgXcQ code 302\n\n\nbackend timo-web\n  mode http\n  option forwardfor\n  server timo-web 192.168.43.12:80\n\nbackend timo-data\n  mode http\n  option forwardfor\n  server timo-data 192.168.43.13:80<\/code><\/pre>\n\n\n\n<p>Das kann auch unter &#8222;default&#8220; geschehen. <br>Ich mache es im backend :).<\/p>\n\n\n\n<p>Danach noch einmal die Konfiguration pr\u00fcfen:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">sudo haproxy -f \/etc\/haproxy\/haproxy.cfg -c<\/code><\/pre>\n\n\n\n<p>Und danach den HAProxy einmal aktivieren und neu starten (ein reload reicht auch):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">sudo systemctl enable --now haproxy\nsudo systemctl restart haproxy<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vorwort Ich hatte mir selbst die Aufgabe gesetzt, nach meiner Wireguard umstellung, meinen reinen Reverse Proxy auf einen HAProxy umzuziehen. Falls eine Anleitung zu einem NGINX Reverse Proxy gebraucht wird, kann gerne dieser Beitrag befolgt werden: NGINX Reverse Proxy (+SSL Termination, IP Forwarding &amp; Proxy Caching) Vorbereitung Als Betriebssystem kommt diesmal ein Ubuntu 22.04 Minimal [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,31,6,10,32,4],"tags":[15,14,29,19,25,20,21,18,30,24],"class_list":["post-187","post","type-post","status-publish","format-standard","hentry","category-german","category-linux","category-reverse-proxy","category-ssl-tls","category-ubuntu","category-webserver","tag-deutsch","tag-german","tag-haproxy","tag-ip-forwarding","tag-ip-weiterleitung","tag-proxy","tag-reverse-proxy","tag-ssl-termination","tag-ubuntu","tag-webserver"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/timo-brunn.de\/index.php\/wp-json\/wp\/v2\/posts\/187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/timo-brunn.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/timo-brunn.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/timo-brunn.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/timo-brunn.de\/index.php\/wp-json\/wp\/v2\/comments?post=187"}],"version-history":[{"count":10,"href":"https:\/\/timo-brunn.de\/index.php\/wp-json\/wp\/v2\/posts\/187\/revisions"}],"predecessor-version":[{"id":319,"href":"https:\/\/timo-brunn.de\/index.php\/wp-json\/wp\/v2\/posts\/187\/revisions\/319"}],"wp:attachment":[{"href":"https:\/\/timo-brunn.de\/index.php\/wp-json\/wp\/v2\/media?parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/timo-brunn.de\/index.php\/wp-json\/wp\/v2\/categories?post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/timo-brunn.de\/index.php\/wp-json\/wp\/v2\/tags?post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}