[{"content":"You put HAProxy in front of two Apache servers, everything balances nicely, and then you open the access log on one of the web servers. Every single request comes from the same IP: the load balancer.\nThis guide explains why that happens, the quick fix (log the X-Forwarded-For header), and the cleaner fix with mod_remoteip, plus the detail most guides skip: the quick fix can be faked by any client.\nThe setup balancer: HAProxy, public side 192.168.10.10, internal side 192.168.100.1. apache1 (192.168.100.100) and apache2 (192.168.100.101): Apache with PHP on Debian. The client is a laptop at 192.168.10.1. HAProxy backend, round robin with health checks:\nfrontend web bind *:80 default_backend web_servers backend web_servers balance roundrobin option httpchk GET / server apache1 192.168.100.100:80 check server apache2 192.168.100.101:80 check Requests alternate between the two servers, as expected:\n$ for i in 1 2 3 4; do curl -s http://www.example.org/ | grep Server; done \u0026lt;h1\u0026gt;Server: apache1\u0026lt;/h1\u0026gt;\u0026lt;p\u0026gt;Client IP: 192.168.100.1\u0026lt;/p\u0026gt; \u0026lt;h1\u0026gt;Server: apache2\u0026lt;/h1\u0026gt;\u0026lt;p\u0026gt;Client IP: 192.168.100.1\u0026lt;/p\u0026gt; \u0026lt;h1\u0026gt;Server: apache1\u0026lt;/h1\u0026gt;\u0026lt;p\u0026gt;Client IP: 192.168.100.1\u0026lt;/p\u0026gt; \u0026lt;h1\u0026gt;Server: apache2\u0026lt;/h1\u0026gt;\u0026lt;p\u0026gt;Client IP: 192.168.100.1\u0026lt;/p\u0026gt; Look at \u0026ldquo;Client IP\u0026rdquo;: it is 192.168.100.1, the balancer. The Apache log says the same:\ndebian@apache1:~$ sudo grep curl /var/log/apache2/access.log | tail -n 2 192.168.100.1 - - [29/Oct/2026:13:53:42 +0100] \u0026#34;GET / HTTP/1.1\u0026#34; 200 335 \u0026#34;-\u0026#34; \u0026#34;curl/8.14.1\u0026#34; 192.168.100.1 - - [29/Oct/2026:13:53:42 +0100] \u0026#34;GET / HTTP/1.1\u0026#34; 200 335 \u0026#34;-\u0026#34; \u0026#34;curl/8.14.1\u0026#34; Why: there are two TCP connections, not one A reverse proxy or load balancer does not \u0026ldquo;pass through\u0026rdquo; the client\u0026rsquo;s connection. There are two separate connections:\nclient 192.168.10.1 --TCP--\u0026gt; HAProxy --TCP (new connection)--\u0026gt; apache1 Apache\u0026rsquo;s %h in the log format is the source IP of the TCP connection it received. That connection was opened by HAProxy, so Apache can only ever see HAProxy\u0026rsquo;s address there. Nothing is misconfigured; the client\u0026rsquo;s IP simply isn\u0026rsquo;t in that connection.\nThe only way to get it to Apache is to put it inside the HTTP request, as a header.\nStep 1: Make HAProxy send X-Forwarded-For Add option forwardfor to the backend (or to defaults):\nbackend web_servers option forwardfor balance roundrobin option httpchk GET / server apache1 192.168.100.100:80 check server apache2 192.168.100.101:80 check Check and reload:\n$ sudo haproxy -c -f /etc/haproxy/haproxy.cfg $ sudo systemctl reload haproxy Now each request reaching Apache carries X-Forwarded-For: 192.168.10.1. But Apache does not log that header by default, so the log still shows the balancer. You have two ways to use it.\nStep 2, quick way: log the header Create a log format that writes the header instead of %h. On Debian, put it in its own config snippet:\n$ sudo nano /etc/apache2/conf-available/balanced-log.conf LogFormat \u0026#34;%{X-Forwarded-For}i %l %u %t \\\u0026#34;%r\\\u0026#34; %\u0026gt;s %O\u0026#34; balanced $ sudo a2enconf balanced-log Then use it in the site. In /etc/apache2/sites-available/000-default.conf, change the CustomLog line:\nCustomLog ${APACHE_LOG_DIR}/access.log balanced $ sudo apache2ctl configtest \u0026amp;\u0026amp; sudo systemctl reload apache2 Do the same on every backend server. Now the log shows the real client:\ndebian@apache1:~$ sudo grep -v \u0026#34;HTTP/1.0\u0026#34; /var/log/apache2/access.log | tail -n 2 192.168.10.1 - - [29/Oct/2026:18:57:40 +0100] \u0026#34;GET / HTTP/1.1\u0026#34; 200 335 192.168.10.1 - - [29/Oct/2026:18:57:40 +0100] \u0026#34;GET / HTTP/1.1\u0026#34; 200 335 Why the grep -v \u0026quot;HTTP/1.0\u0026quot;? Your log is now full of lines with an empty client field and HTTP/1.0. Those are HAProxy\u0026rsquo;s health checks (option httpchk): they come from HAProxy itself, with no X-Forwarded-For, every couple of seconds. Filtering out HTTP/1.0 is a quick way to read only real traffic.\nThe problem with the quick way: anyone can fake the header X-Forwarded-For is just a request header. A client can send its own:\n$ curl -H \u0026#34;X-Forwarded-For: 1.2.3.4\u0026#34; http://www.example.org/ By default, option forwardfor adds the real address to the header the client sent rather than replacing it, so Apache receives something like 1.2.3.4, 192.168.10.1. With the quick log format, that whole string goes into your log, and anything reading the first value will believe the request came from 1.2.3.4.\nFor a lab, that\u0026rsquo;s fine. For anything where logs matter (fail2ban, abuse reports, rate limiting in the app), use the next method.\nStep 2, better way: mod_remoteip mod_remoteip makes Apache replace the connection\u0026rsquo;s client IP with the one from the header, but only when the request comes from a proxy you trust. Everything that uses the client IP (logs, Require ip, PHP\u0026rsquo;s REMOTE_ADDR) then sees the real client.\nEnable it:\n$ sudo a2enmod remoteip $ sudo nano /etc/apache2/conf-available/remoteip.conf RemoteIPHeader X-Forwarded-For RemoteIPInternalProxy 192.168.100.1 RemoteIPHeader: which header holds the client IP. RemoteIPInternalProxy: the balancer\u0026rsquo;s address as Apache sees it. Use your HAProxy\u0026rsquo;s internal IP. Requests from any other address are not trusted, so a client hitting Apache directly cannot inject a fake IP. $ sudo a2enconf remoteip One more change, which is the part people miss: Debian\u0026rsquo;s default combined format logs %h, the raw connection address. mod_remoteip updates %a. Edit the LogFormat ... combined line in /etc/apache2/apache2.conf and change the leading %h to %a:\nLogFormat \u0026#34;%a %l %u %t \\\u0026#34;%r\\\u0026#34; %\u0026gt;s %O \\\u0026#34;%{Referer}i\\\u0026#34; \\\u0026#34;%{User-Agent}i\\\u0026#34;\u0026#34; combined Keep the site\u0026rsquo;s CustomLog as combined, then:\n$ sudo apache2ctl configtest \u0026amp;\u0026amp; sudo systemctl reload apache2 Which one should you use? Log %{X-Forwarded-For}i mod_remoteip Setup One LogFormat Module + trusted proxy + %a Logs Real IP Real IP App sees real IP (REMOTE_ADDR) No Yes Spoofable by the client Yes No (only trusted proxy is believed) Checklist HAProxy: option forwardfor in the backend, reload. Apache, quick: LogFormat \u0026quot;%{X-Forwarded-For}i ...\u0026quot; and use it in CustomLog. Apache, proper: a2enmod remoteip, RemoteIPHeader X-Forwarded-For, RemoteIPInternalProxy \u0026lt;balancer IP\u0026gt;, and %h → %a in the log format. Repeat on every backend server. Ignore the HTTP/1.0 lines: they are HAProxy health checks. ","permalink":"https://tech.adriandelpar.com/apache-real-client-ip-behind-haproxy/","summary":"\u003cp\u003eYou put HAProxy in front of two Apache servers, everything balances nicely, and then you open the access log on one of the web servers. Every single request comes from the same IP: the load balancer.\u003c/p\u003e\n\u003cp\u003eThis guide explains why that happens, the quick fix (log the \u003ccode\u003eX-Forwarded-For\u003c/code\u003e header), and the cleaner fix with \u003ccode\u003emod_remoteip\u003c/code\u003e, plus the detail most guides skip: the quick fix can be faked by any client.\u003c/p\u003e","title":"Apache Logs Show the Load Balancer's IP, Not the Client's: Fixing It Behind HAProxy"},{"content":"Debian 13 can install systemd-boot instead of GRUB, but only from the Expert install mode of the installer. If you already have a normal install with GRUB, you can switch afterwards, and most guides make it look like two commands: apt install systemd-boot and bootctl install.\nOn a machine with Secure Boot enabled, that is not enough. I did this on a Debian 13 VM (OVMF with the Microsoft keys enrolled) and ran into three problems the short guides skip:\nThe boot entry that bootctl creates does not boot with Secure Boot on. The obvious efibootmgr command to fix it silently boots GRUB again. Installing the package overwrites the fallback boot path with an unsigned loader, which only shows up the day you lose your NVRAM. This guide covers the full switch, with real output from each step.\nWhat you need to understand first: the Secure Boot chain With Secure Boot, the firmware only runs EFI programs signed with keys it trusts, and on almost every PC those are Microsoft\u0026rsquo;s keys. Debian\u0026rsquo;s GRUB and systemd-boot are signed by Debian, not Microsoft. That is why Debian uses shim: a tiny loader signed by Microsoft that carries Debian\u0026rsquo;s key inside and then loads the next stage.\nfirmware -\u0026gt; shim (signed by Microsoft) -\u0026gt; systemd-boot (signed by Debian) -\u0026gt; kernel (signed by Debian) Keep this chain in mind. Every problem below comes from something trying to skip shim.\nThe other key difference: GRUB has its own drivers and can read your kernel from /boot on ext4 or LVM. systemd-boot can only read the EFI System Partition (ESP, FAT32). So on Debian, kernel-install copies every kernel and initrd into the ESP, and each menu entry is a small .conf file. There is no update-grub equivalent to run.\nBefore you start The system must boot in UEFI mode. systemd-boot does not support legacy BIOS. Check Secure Boot is actually on, so you can compare later: $ mokutil --sb-state SecureBoot enabled Make a backup or snapshot. If you get the boot entry wrong, the machine will not boot. On a VM, clone it first (I used virt-clone). Step 1: Move the kernel command line With GRUB, kernel parameters live in /etc/default/grub. With systemd-boot, kernel-install reads them from /etc/kernel/cmdline. Get your root filesystem UUID:\n$ findmnt -no UUID / 6bb0ca11-xxxx-xxxx-xxxx-648ec36894d9 Create /etc/kernel/cmdline with your editor:\n$ sudo nano /etc/kernel/cmdline root=UUID=6bb0ca11-xxxx-xxxx-xxxx-648ec36894d9 ro quiet Use your own UUID. If you had extra parameters in GRUB_CMDLINE_LINUX_DEFAULT, add them here too.\nStep 2: Install systemd-boot (the signed one) $ sudo apt install systemd-boot systemd-boot-efi-amd64-signed systemd-boot-efi-amd64-signed is the build signed by Debian. Without it, shim has nothing it is allowed to load.\nThe package immediately creates entries for every installed kernel:\n$ sudo bootctl list type: Boot Loader Specification Type #1 (.conf) title: Debian GNU/Linux 13 (trixie) (6.12.117+deb13-amd64) (default) (not reported/new) id: \u0026lt;machine-id\u0026gt;-6.12.117+deb13-amd64.conf source: /boot/efi//loader/entries/\u0026lt;machine-id\u0026gt;-6.12.117+deb13-amd64.conf linux: /boot/efi//\u0026lt;machine-id\u0026gt;/6.12.117+deb13-amd64/linux initrd: /boot/efi//\u0026lt;machine-id\u0026gt;/6.12.117+deb13-amd64/initrd.img-6.12.117+deb13-amd64 options: root=UUID=6bb0ca11-xxxx-xxxx-xxxx-648ec36894d9 systemd.machine_id=\u0026lt;machine-id\u0026gt; ... Check that options contains the UUID from step 1. You may see two entries if you have two kernels installed (for example the one from the ISO and an updated one).\nSince kernels now live in the ESP, check there is room. Each kernel plus initrd takes a few tens of MB:\n$ df -h /boot/efi Filesystem Size Used Avail Use% Mounted on /dev/vda1 758M 116M 642M 16% /boot/efi Step 3: Fix the boot entry so it goes through shim When the package installs, bootctl adds a firmware entry called Linux Boot Manager that points straight at systemd-bootx64.efi. With Secure Boot on, the firmware refuses it, because that file is not signed by Microsoft. Find it and delete it:\n$ sudo efibootmgr Look for Linux Boot Manager and note its number (it was 0001 in my case):\n$ sudo efibootmgr -b 0001 -B Now create an entry that loads shim and passes it the path of systemd-boot as an argument. First identify the ESP disk and partition:\n$ lsblk -f NAME FSTYPE FSVER LABEL UUID MOUNTPOINTS vda ├─vda1 vfat FAT32 D04A-xxxx /boot/efi ├─vda2 ext4 1.0 6bb0ca11-xxxx-xxxx-xxxx-648ec36894d9 / └─vda3 swap 1 3a3ab77e-xxxx-xxxx-xxxx-d89c33cddf74 [SWAP] The ESP is vda1, so disk /dev/vda, partition 1:\n$ sudo efibootmgr --create --disk /dev/vda --part 1 \\ --label \u0026#34;Debian systemd-boot\u0026#34; \\ --loader \u0026#39;\\EFI\\debian\\shimx64.efi\u0026#39; \\ --unicode \u0026#39;\\EFI\\systemd\\systemd-bootx64.efi \\0\u0026#39; What each option does:\n--loader: what the firmware runs, which is shim. --unicode: the argument passed to shim, which is the path of the next loader. If shim receives no argument, it loads grubx64.efi from its own folder. The gotcha: the trailing \\0 My first attempt was the same command without the \\0 at the end. The entry was created, the machine rebooted, and GRUB came up. Shim was not picking up the path and fell back to grubx64.efi.\nWhat fixed it was copying exactly how the Debian expert installer writes this entry on a fresh systemd-boot install. Listing that entry with efibootmgr -v shows the argument ends in \\0. With it, the new entry looks like this:\nBoot0001* Debian systemd-boot\tHD(1,GPT,...)/File(\\EFI\\debian\\shimx64.efi) File(.\\EFI\\systemd\\systemd-bootx64.efi \\0) The new entry goes first in BootOrder. Leave the old debian entry (shim -\u0026gt; GRUB) in place for now as a way back.\nStep 4: Reboot and verify You should see the systemd-boot menu instead of GRUB. Then:\n$ mokutil --sb-state SecureBoot enabled $ sudo bootctl status System: Firmware: UEFI 2.70 (Debian distribution of EDK II 1.00) Secure Boot: enabled (user) Boot into FW: supported Current Boot Loader: Product: systemd-boot 257.13-1~deb13u1 ... Loader: └─/EFI/systemd/systemd-bootx64.efi Current Entry: \u0026lt;machine-id\u0026gt;-6.12.117+deb13-amd64.conf The two lines that matter: Secure Boot: enabled and Product: systemd-boot.\nStep 5: Remove GRUB GRUB is a protected package in Debian, so apt refuses to remove it normally (a safety net against leaving a system unbootable). Only do this after step 4 worked:\n$ sudo apt purge --allow-remove-essential grub-efi-amd64 grub-efi-amd64-signed grub-efi-amd64-bin grub-common grub2-common REMOVING: grub-common* grub-efi-amd64-bin* grub-efi-amd64-unsigned* os-prober* grub-efi-amd64* grub-efi-amd64-signed* grub2-common* WARNING: The following essential packages will be removed. ... Read the list before confirming: shim-signed and systemd-boot must not be in it.\n$ sudo apt autoremove --purge ... update-initramfs: Generating /boot/initrd.img-6.12.117+deb13-amd64 Updating kernel version 6.12.117+deb13-amd64 in systemd-boot... That last line is systemd-boot copying the regenerated initrd into the ESP on its own. This is the job update-grub used to do.\nClean up GRUB leftovers and the old firmware entry. Do not delete the EFI/debian folder: shim lives there.\n$ sudo rm /boot/efi/EFI/debian/grubx64.efi $ sudo rm -r /boot/grub $ sudo efibootmgr -b 0003 -B (0003 was my old debian entry; use your number.)\n$ dpkg -l | grep grub $ No output: GRUB is gone.\nStep 6: Fix the fallback path (do not skip this) Firmware has a fallback path, EFI/BOOT/BOOTX64.EFI, which it uses when there is no valid boot entry. That happens more than you think: a firmware reset, a CMOS battery, a VM recreated with a fresh NVRAM.\nWhen you install the systemd-boot package by hand, bootctl replaces that fallback file with unsigned-by-Microsoft systemd-boot. With Secure Boot on, the fallback will not boot. You will not notice until the day your NVRAM is reset. (A fresh install from the expert installer does not have this problem.)\nI only found this because I reset the VM\u0026rsquo;s NVRAM later and it would not boot. Fix it now:\n$ sudo cp /boot/efi/EFI/debian/shimx64.efi /boot/efi/EFI/BOOT/BOOTX64.EFI $ sudo cp /boot/efi/EFI/systemd/systemd-bootx64.efi /boot/efi/EFI/BOOT/grubx64.efi $ sudo rm /boot/efi/EFI/BOOT/fbx64.efi Why each line:\nThe fallback becomes shim again. Shim with no argument loads grubx64.efi from its own folder, so we put systemd-boot there under that name. If fbx64.efi (shim\u0026rsquo;s \u0026ldquo;fallback\u0026rdquo; helper) is present, shim runs it first, and it would recreate a boot entry pointing at the GRUB you just removed. I tested it by wiping the NVRAM (virsh start \u0026lt;vm\u0026gt; --reset-nvram). The machine booted through the fallback with Secure Boot still enabled, and I recreated the \u0026ldquo;Debian systemd-boot\u0026rdquo; entry with the same efibootmgr --create command from step 3.\nStep 7 (optional): Firmware and EFI shell entries The menu lives in /boot/efi/loader/loader.conf. On Debian 13 the default was already what I wanted:\n$ sudo cat /boot/efi/loader/loader.conf timeout 10 #console-mode keep default \u0026lt;machine-id\u0026gt;-* auto-firmware yes default \u0026lt;machine-id\u0026gt;-* matches only the Debian entries, which all start with the machine ID. auto-firmware yes adds a Reboot Into Firmware Interface entry. To add an EFI shell, Debian packages it. systemd-boot can only launch files that are inside the ESP, so copy it there:\n$ sudo apt install efi-shell-x64 $ sudo mkdir -p /boot/efi/EFI/tools $ sudo cp /usr/share/efi-shell-x64/shellx64.efi /boot/efi/EFI/tools/shellx64.efi $ sudo nano /boot/efi/loader/entries/efishell.conf title EFI Shell efi /EFI/tools/shellx64.efi sort-key zz-shell efi instead of linux: it launches an EFI program, not a kernel. sort-key zz-shell: sorts it below Debian. Expect this to fail with Secure Boot on. shellx64.efi is not signed, so selecting it gives a security violation. That is Secure Boot working as intended. To use the shell, disable Secure Boot temporarily in the firmware settings.\nOne warning from experience: on OVMF, after I disabled Secure Boot from the firmware menu, the VM lost its enrolled keys (mokutil reported Platform is in Setup Mode) and would not let me re-enable it. The way back was resetting the NVRAM, which is exactly why step 6 matters.\nRemoving old kernels If you want a single Debian entry, purge the old kernel package:\n$ sudo apt purge linux-image-6.12.107+deb13-amd64 kernel-install also removes its entry and its files from the ESP. If bootctl list still shows it as reported/absent, that is the list from the last boot; it disappears after the next reboot.\nIs it worth it? Pros\nEach entry is a few lines of plain text. There is no generated config. Kernel updates add and remove their own entries. bootctl shows and changes everything (default entry, one-time boot, timeout). It detects firmware access and other loaders on the ESP automatically. Cons\nUEFI only. No rescue console like GRUB\u0026rsquo;s. On Debian 13 it is still rough when done by hand: the shim entry and the fallback path need manual work, as shown above. GRUB remains Debian\u0026rsquo;s default. systemd-boot is the default on distributions such as Pop!_OS and EndeavourOS (UEFI installs).\nQuick checklist /etc/kernel/cmdline with your root UUID. apt install systemd-boot systemd-boot-efi-amd64-signed. Delete \u0026ldquo;Linux Boot Manager\u0026rdquo;; create a shim entry with --unicode '\\EFI\\systemd\\systemd-bootx64.efi \\0'. Reboot, check bootctl status and mokutil --sb-state. Purge GRUB with --allow-remove-essential, keep EFI/debian. Restore shim as EFI/BOOT/BOOTX64.EFI, put systemd-boot as EFI/BOOT/grubx64.efi, remove fbx64.efi. ","permalink":"https://tech.adriandelpar.com/replace-grub-with-systemd-boot-debian-13-secure-boot/","summary":"\u003cp\u003eDebian 13 can install systemd-boot instead of GRUB, but only from the \u003cstrong\u003eExpert install\u003c/strong\u003e mode of the installer. If you already have a normal install with GRUB, you can switch afterwards, and most guides make it look like two commands: \u003ccode\u003eapt install systemd-boot\u003c/code\u003e and \u003ccode\u003ebootctl install\u003c/code\u003e.\u003c/p\u003e\n\u003cp\u003eOn a machine with \u003cstrong\u003eSecure Boot enabled\u003c/strong\u003e, that is not enough. I did this on a Debian 13 VM (OVMF with the Microsoft keys enrolled) and ran into three problems the short guides skip:\u003c/p\u003e","title":"How to Replace GRUB with systemd-boot on Debian 13 (Trixie) Without Breaking Secure Boot"},{"content":"Classic symptom after putting a load balancer in front of two PHP servers: users get logged out at random, a cart empties itself, a multi-step form forgets what you typed. Nothing is \u0026ldquo;broken\u0026rdquo; on either server. The problem is where PHP keeps sessions.\nBelow is a minimal way to reproduce it, the explanation, and the fix with HAProxy\u0026rsquo;s cookie-based persistence (sticky sessions).\nReproduce it in one minute Two Apache + PHP backends (apache1, apache2) behind HAProxy with balance roundrobin. On both servers, create /var/www/html/session.php:\n\u0026lt;?php session_start(); $_SESSION[\u0026#39;visits\u0026#39;] = ($_SESSION[\u0026#39;visits\u0026#39;] ?? 0) + 1; echo \u0026#34;\u0026lt;h1\u0026gt;Server: \u0026#34; . gethostname() . \u0026#34;\u0026lt;/h1\u0026gt;\u0026#34;; echo \u0026#34;\u0026lt;p\u0026gt;Visits in this session: \u0026#34; . $_SESSION[\u0026#39;visits\u0026#39;] . \u0026#34;\u0026lt;/p\u0026gt;\u0026#34;; ?\u0026gt; Now request it four times. curl does not keep cookies by itself, so use a cookie jar (-c to save, -b to send) to behave like a browser:\n$ rm -f cookies.txt $ for i in 1 2 3 4; do curl -s -c cookies.txt -b cookies.txt http://www.example.org/session.php; echo; done \u0026lt;h1\u0026gt;Server: apache2\u0026lt;/h1\u0026gt;\u0026lt;p\u0026gt;Visits in this session: 1\u0026lt;/p\u0026gt; \u0026lt;h1\u0026gt;Server: apache1\u0026lt;/h1\u0026gt;\u0026lt;p\u0026gt;Visits in this session: 1\u0026lt;/p\u0026gt; \u0026lt;h1\u0026gt;Server: apache2\u0026lt;/h1\u0026gt;\u0026lt;p\u0026gt;Visits in this session: 2\u0026lt;/p\u0026gt; \u0026lt;h1\u0026gt;Server: apache1\u0026lt;/h1\u0026gt;\u0026lt;p\u0026gt;Visits in this session: 2\u0026lt;/p\u0026gt; The counter should go 1, 2, 3, 4. It goes 1, 1, 2, 2.\nWhy: the session lives on one server\u0026rsquo;s disk By default PHP stores session data in files on the server that created them (on Debian, /var/lib/php/sessions). The browser only stores the session ID, in the PHPSESSID cookie.\nSo with round robin:\nRequest 1 goes to apache2. It creates session file sess_abc with visits = 1. Request 2 goes to apache1, carrying PHPSESSID=abc. apache1 has no file called sess_abc, so for it this is a new session: visits = 1. Request 3 goes back to apache2, which finds its own file: visits = 2. The session is split in two halves, one per server, and each server only counts the requests it saw.\nThere are two ways out:\nShared session storage: keep sessions in Redis, Memcached or a database that both servers use. This is the robust fix for production, because any server can serve any request. Sticky sessions: make the load balancer always send a given client to the same server. No application changes. This is what we do here. The fix: cookie-based persistence in HAProxy HAProxy can insert its own cookie saying which server handled the client, and route by it afterwards. Two changes in the backend:\nbackend web_servers cookie SERVERID insert indirect nocache option forwardfor balance roundrobin option httpchk GET / server apache1 192.168.100.100:80 check cookie s1 server apache2 192.168.100.101:80 check cookie s2 cookie SERVERID insert: HAProxy adds a SERVERID cookie to the response. indirect: HAProxy removes its cookie before passing the request to the backend, so your application never sees it. nocache: marks responses carrying the cookie as non-cacheable, so a shared cache can\u0026rsquo;t hand one user\u0026rsquo;s SERVERID to everybody. cookie s1 / cookie s2 on each server line: the value written in the cookie for that server. Each must be unique. Check and reload:\n$ sudo haproxy -c -f /etc/haproxy/haproxy.cfg $ sudo systemctl reload haproxy Test again Start with an empty cookie jar so the first request is balanced normally:\n$ rm -f cookies.txt $ for i in 1 2 3 4; do curl -s -c cookies.txt -b cookies.txt http://www.example.org/session.php; echo; done \u0026lt;h1\u0026gt;Server: apache1\u0026lt;/h1\u0026gt;\u0026lt;p\u0026gt;Visits in this session: 1\u0026lt;/p\u0026gt; \u0026lt;h1\u0026gt;Server: apache1\u0026lt;/h1\u0026gt;\u0026lt;p\u0026gt;Visits in this session: 2\u0026lt;/p\u0026gt; \u0026lt;h1\u0026gt;Server: apache1\u0026lt;/h1\u0026gt;\u0026lt;p\u0026gt;Visits in this session: 3\u0026lt;/p\u0026gt; \u0026lt;h1\u0026gt;Server: apache1\u0026lt;/h1\u0026gt;\u0026lt;p\u0026gt;Visits in this session: 4\u0026lt;/p\u0026gt; Same server every time, counter goes up by one.\nThe response headers show both cookies doing their jobs:\n$ curl -sI http://www.example.org/session.php | grep -i set-cookie set-cookie: PHPSESSID=d9e41cf40a10dd99038dba98420e5092; path=/ set-cookie: SERVERID=s2; path=/ PHPSESSID is created by PHP and identifies the session. SERVERID is inserted by HAProxy and identifies the server that holds it. Common gotchas Testing with plain curl and thinking it doesn\u0026rsquo;t work. Without -c/-b, curl never sends cookies back, so every request looks like a new client and gets balanced again. Browsers keep cookies; curl doesn\u0026rsquo;t unless you tell it to.\nDuplicate cookie values. If two server lines have the same cookie value, HAProxy can\u0026rsquo;t tell them apart. Make each one unique.\nA server goes down. If apache1 fails its health check, clients pinned to s1 are sent to another server, and their session (stored on apache1) is gone. Sticky sessions keep users on one server; they don\u0026rsquo;t make sessions survive a server failure. For that, you need shared session storage.\nUneven load. Persistence means a busy client stays on the same server. With many users it averages out; with a few heavy users it may not.\nSummary Round robin + PHP\u0026rsquo;s default file sessions = each server sees only part of the session. Quick fix: cookie SERVERID insert indirect nocache in the backend and a unique cookie \u0026lt;value\u0026gt; on each server line. Test with a cookie jar (curl -c cookies.txt -b cookies.txt). For high availability, move sessions to shared storage (Redis, Memcached or a database). ","permalink":"https://tech.adriandelpar.com/php-session-lost-behind-haproxy-sticky-sessions/","summary":"\u003cp\u003eClassic symptom after putting a load balancer in front of two PHP servers: users get logged out at random, a cart empties itself, a multi-step form forgets what you typed. Nothing is \u0026ldquo;broken\u0026rdquo; on either server. The problem is where PHP keeps sessions.\u003c/p\u003e\n\u003cp\u003eBelow is a minimal way to reproduce it, the explanation, and the fix with HAProxy\u0026rsquo;s cookie-based persistence (sticky sessions).\u003c/p\u003e\n\u003ch2 id=\"reproduce-it-in-one-minute\"\u003eReproduce it in one minute\u003c/h2\u003e\n\u003cp\u003eTwo Apache + PHP backends (\u003ccode\u003eapache1\u003c/code\u003e, \u003ccode\u003eapache2\u003c/code\u003e) behind HAProxy with \u003ccode\u003ebalance roundrobin\u003c/code\u003e. On \u003cstrong\u003eboth\u003c/strong\u003e servers, create \u003ccode\u003e/var/www/html/session.php\u003c/code\u003e:\u003c/p\u003e","title":"PHP Sessions Keep Resetting Behind HAProxy: Sticky Sessions with a Cookie"},{"content":"I was moving a Django app from \u0026ldquo;uWSGI running in a terminal\u0026rdquo; to a proper systemd service. The service would not start, kill did nothing useful, and later systemctl restart took a minute and a half. All three problems had the same cause: uWSGI does not treat SIGTERM as \u0026ldquo;stop\u0026rdquo;.\nIf you only want the fix, it is this line in your unit file:\nKillSignal=SIGQUIT The rest of this post explains what is going on, so you can recognise it next time.\nThe setup A Django project with uWSGI installed in the project\u0026rsquo;s virtualenv, listening in HTTP mode on localhost. A web server (Apache or nginx) sits in front as a reverse proxy.\nuwsgi.ini:\n[uwsgi] http = 127.0.0.1:8080 chdir = /srv/myproject wsgi-file = myproject/wsgi.py processes = 4 threads = 2 master = true The first test was by hand, in the foreground:\n(venv) $ uwsgi --ini /srv/myproject/uwsgi.ini ... uWSGI http bound on 127.0.0.1:8080 fd 4 spawned uWSGI master process (pid: 6722) spawned uWSGI worker 1 (pid: 6723, cores: 2) ... It worked. Then I wrote the systemd unit.\nSymptom 1: the service never comes up /etc/systemd/system/myproject_uwsgi.service:\n[Unit] Description=uWSGI service for my Django project After=network.target [Service] User=myuser Group=myuser WorkingDirectory=/srv/myproject Environment=\u0026#34;PATH=/srv/myproject/venv/bin\u0026#34; ExecStart=/srv/myproject/venv/bin/uwsgi --ini /srv/myproject/uwsgi.ini Restart=always RestartSec=5 [Install] WantedBy=multi-user.target $ sudo systemctl daemon-reload $ sudo systemctl enable --now myproject_uwsgi $ sudo systemctl status myproject_uwsgi ● myproject_uwsgi.service - uWSGI service for my Django project Loaded: loaded (/etc/systemd/system/myproject_uwsgi.service; enabled; ...) Active: activating (auto-restart) (Result: exit-code) since ... Main PID: 9564 (code=exited, status=1/FAILURE) activating (auto-restart) plus status=1/FAILURE means the process starts, dies immediately, and Restart=always brings it back every 5 seconds, forever.\nThe reason was simple once I looked at the port:\n$ ss -tlnp | grep 8080 LISTEN 0 100 127.0.0.1:8080 0.0.0.0:* users:((\u0026#34;uwsgi\u0026#34;,pid=8220,fd=4),(\u0026#34;uwsgi\u0026#34;,pid=8212,fd=4)) The manual test instance was still running in another terminal and holding port 8080. The service could not bind to it. Fine, kill it.\nSymptom 2: kill doesn\u0026rsquo;t kill it $ kill 8212 $ ss -tlnp | grep 8080 LISTEN 0 100 127.0.0.1:8080 0.0.0.0:* users:((\u0026#34;uwsgi\u0026#34;,pid=10255,fd=5),(\u0026#34;uwsgi\u0026#34;,pid=8212,fd=5)) Same master PID, still listening. In its terminal, uWSGI printed:\n...gracefully (RE)spawned uWSGI master process (pid: 8212) It did not die. It reloaded itself.\nWhy: uWSGI\u0026rsquo;s signal table kill with no option sends SIGTERM. Almost every Unix daemon reads that as \u0026ldquo;shut down cleanly\u0026rdquo;. uWSGI does not. Its own documentation lists the signals like this:\nSignal What uWSGI does SIGHUP graceful reload of workers and master SIGTERM \u0026ldquo;brutal\u0026rdquo; reload of workers and master SIGINT kill the whole stack immediately (this is Ctrl+C) SIGQUIT kill the whole stack immediately So the uWSGI docs themselves say: to shut it down, use SIGINT or SIGQUIT, or turn on the die-on-term option if you want SIGTERM to mean \u0026ldquo;stop\u0026rdquo;.\nTo get rid of the stray manual instance:\n$ kill -INT 8212 $ ss -tlnp | grep 8080 $ Port free. The service started on the next restart attempt.\nSymptom 3 (waiting to happen): systemctl stop hangs for 90 seconds This is the part that would have bitten me later. When you run systemctl stop or systemctl restart, systemd sends SIGTERM by default to the service\u0026rsquo;s processes, waits for them to exit, and if they don\u0026rsquo;t, sends SIGKILL after the stop timeout (90 seconds by default).\nPut that together with the table above:\nsystemctl restart myproject_uwsgi systemd sends SIGTERM. uWSGI reloads instead of exiting. systemd waits\u0026hellip; and waits\u0026hellip; then SIGKILLs it after about 90 seconds. People hit this regularly; there are bug reports of a uWSGI service taking exactly 1m30s to stop, ending in a journal line like State 'stop-sigterm' timed out. Killing.\nThe fix Tell systemd which signal to use to stop the service. KillSignal= is the standard systemd option for that:\n[Service] ... Restart=always RestartSec=5 KillSignal=SIGQUIT $ sudo systemctl daemon-reload $ sudo systemctl restart myproject_uwsgi $ sudo systemctl status myproject_uwsgi ● myproject_uwsgi.service - uWSGI service for my Django project Active: active (running) since ...; 2s ago Main PID: 11439 (uwsgi) Tasks: 10 CGroup: /system.slice/myproject_uwsgi.service ├─11439 /srv/myproject/venv/bin/uwsgi --ini /srv/myproject/uwsgi.ini ├─11459 ... └─11466 ... Now stop and restart are instant, and the master plus all workers are inside the service\u0026rsquo;s cgroup, so nothing is left behind.\nSIGQUIT is also what the uWSGI documentation uses in its own systemd example. SIGINT works too.\nAlternative: die-on-term If you would rather keep systemd\u0026rsquo;s default SIGTERM (for example, because other tools such as docker stop also send SIGTERM), make uWSGI follow the convention instead:\n[uwsgi] ... die-on-term = true Pick one approach. KillSignal=SIGQUIT is the smallest change if you only manage uWSGI through systemd.\nOther things worth checking in the unit Use the venv\u0026rsquo;s uWSGI binary with its full path. systemd does not activate virtualenvs. Calling /srv/myproject/venv/bin/uwsgi is what makes it load the Django from that venv. Run it as a user that can read the project. Many examples use User=www-data. If your project is inside a home directory with drwx--x--- permissions, www-data cannot even enter it, and with SQLite it also needs write access to the database file and its folder. Don\u0026rsquo;t use daemonize in uwsgi.ini when systemd manages it; systemd expects the process to stay in the foreground. Bonus: why your CSS is a 404 under uWSGI Same project, different surprise: under runserver the stylesheet loaded, under uWSGI it was a 404, even with DEBUG = True:\n$ curl -I http://127.0.0.1:8080/static/style.css HTTP/1.1 404 Not Found runserver includes a helper that serves /static/; the WSGI application that uWSGI runs does not. The fix is to collect static files into STATIC_ROOT and let something else serve them. Note STATIC_ROOT must be a different folder from anything in STATICFILES_DIRS:\n# settings.py STATIC_ROOT = BASE_DIR / \u0026#34;staticfiles\u0026#34; (venv) $ python manage.py collectstatic 128 static files copied to \u0026#39;/srv/myproject/staticfiles\u0026#39;. Then either let uWSGI map the URL to that folder:\nstatic-map = /static=/srv/myproject/staticfiles or, better in production, serve /static/ directly from nginx or Apache in front.\nSummary kill \u0026lt;pid\u0026gt; and systemctl stop send SIGTERM. uWSGI reloads on SIGTERM. To stop it by hand: kill -INT \u0026lt;pid\u0026gt; (or Ctrl+C). In the systemd unit: KillSignal=SIGQUIT, or die-on-term = true in uwsgi.ini. If a new uWSGI service loops in activating (auto-restart), check ss -tlnp for an old instance holding the port. ","permalink":"https://tech.adriandelpar.com/uwsgi-does-not-stop-sigterm-systemd-killsignal/","summary":"\u003cp\u003eI was moving a Django app from \u0026ldquo;uWSGI running in a terminal\u0026rdquo; to a proper systemd service. The service would not start, \u003ccode\u003ekill\u003c/code\u003e did nothing useful, and later \u003ccode\u003esystemctl restart\u003c/code\u003e took a minute and a half. All three problems had the same cause: \u003cstrong\u003euWSGI does not treat SIGTERM as \u0026ldquo;stop\u0026rdquo;.\u003c/strong\u003e\u003c/p\u003e\n\u003cp\u003eIf you only want the fix, it is this line in your unit file:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-ini\" data-lang=\"ini\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"na\"\u003eKillSignal\u003c/span\u003e\u003cspan class=\"o\"\u003e=\u003c/span\u003e\u003cspan class=\"s\"\u003eSIGQUIT\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eThe rest of this post explains what is going on, so you can recognise it next time.\u003c/p\u003e","title":"uWSGI Won't Stop (and Your systemd Service Keeps Failing): SIGTERM Means Reload"},{"content":"Sysadmin Field Notes is a collection of fixes for Linux and infrastructure problems: Debian, boot loaders, web servers, reverse proxies, load balancers, containers, virtualization and automation.\nEvery guide here comes from a real lab. I build the setup, hit the problem, fix it, and then write down what happened, with the real commands and the real output. Hostnames, IP addresses and identifiers are changed, but the steps are the ones that worked.\nThe goal is simple: when you search for an error message, you should find a page that explains why it happens and how to fix it, not just a command to paste.\nIf a guide is wrong, outdated, or did not work for you, please let me know through the contact page.\n","permalink":"https://tech.adriandelpar.com/about/","summary":"\u003cp\u003eSysadmin Field Notes is a collection of fixes for Linux and infrastructure problems: Debian, boot loaders, web servers, reverse proxies, load balancers, containers, virtualization and automation.\u003c/p\u003e\n\u003cp\u003eEvery guide here comes from a real lab. I build the setup, hit the problem, fix it, and then write down what happened, with the real commands and the real output. Hostnames, IP addresses and identifiers are changed, but the steps are the ones that worked.\u003c/p\u003e","title":"About"},{"content":"Found a mistake in a guide, or a step that no longer works on a newer version? Have a question about one of the setups?\nWrite to adriandelparspam@gmail.com and include the guide\u0026rsquo;s URL and the exact error or output you are seeing.\n","permalink":"https://tech.adriandelpar.com/contact/","summary":"\u003cp\u003eFound a mistake in a guide, or a step that no longer works on a newer version? Have a question about one of the setups?\u003c/p\u003e\n\u003cp\u003eWrite to \u003ca href=\"mailto:adriandelparspam@gmail.com\"\u003eadriandelparspam@gmail.com\u003c/a\u003e and include the guide\u0026rsquo;s URL and the exact error or output you are seeing.\u003c/p\u003e","title":"Contact"},{"content":"Last updated: December 6, 2026\nThis Privacy Policy explains what information is collected when you visit Sysadmin Field Notes (the \u0026ldquo;Site\u0026rdquo;) and how it is used.\nInformation we collect The Site does not have user accounts, comments or forms, and does not ask you for personal information. If you contact us by email, we receive your email address and the content of your message, and use them only to reply.\nLike most websites, the hosting provider may automatically record technical data such as your IP address, browser type and the pages you visit, for security and to keep the Site running.\nAdvertising and cookies The Site may show ads served by Google AdSense. Third-party vendors, including Google, use cookies to serve ads based on your previous visits to this Site or other websites.\nGoogle\u0026rsquo;s use of advertising cookies enables it and its partners to serve ads to you based on your visits to this Site and/or other sites on the Internet.\nYou can opt out of personalized advertising by visiting Google Ads Settings. You can also learn how Google uses information from sites that use its services at How Google uses information from sites or apps that use our services.\nUsers in the European Economic Area, the United Kingdom and Switzerland are asked for consent before personalized ads and non-essential cookies are used, through a consent message shown on the Site.\nYour choices You can block or delete cookies in your browser settings. Blocking cookies may affect how ads are shown, but the Site will keep working.\nYour rights Depending on where you live (for example, under the GDPR in the EU), you may have the right to access, correct or delete personal data we hold about you. To make a request, use the contact page.\nChanges We may update this policy from time to time. The date at the top of this page shows when it was last changed.\n","permalink":"https://tech.adriandelpar.com/privacy-policy/","summary":"\u003cp\u003e\u003cem\u003eLast updated: December 6, 2026\u003c/em\u003e\u003c/p\u003e\n\u003cp\u003eThis Privacy Policy explains what information is collected when you visit Sysadmin Field Notes (the \u0026ldquo;Site\u0026rdquo;) and how it is used.\u003c/p\u003e\n\u003ch2 id=\"information-we-collect\"\u003eInformation we collect\u003c/h2\u003e\n\u003cp\u003eThe Site does not have user accounts, comments or forms, and does not ask you for personal information. If you contact us by email, we receive your email address and the content of your message, and use them only to reply.\u003c/p\u003e","title":"Privacy Policy"}]