{"id":3112,"date":"2026-11-23T14:24:04","date_gmt":"2026-11-23T14:24:04","guid":{"rendered":"https:\/\/hostry.com\/blog\/?p=3112"},"modified":"2026-11-23T14:24:05","modified_gmt":"2026-11-23T14:24:05","slug":"n8n-reverse-proxy-nginx-caddy","status":"publish","type":"post","link":"https:\/\/hostry.com\/blog\/n8n-reverse-proxy-nginx-caddy\/","title":{"rendered":"n8n Reverse Proxy Errors: Fix Trust Proxy and Connection Lost"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-cover-b.webp\" alt=\"n8n reverse proxy errors: requests pass through a front proxy and nginx before they reach n8n, and each hop adds an address to the forwarding list\" class=\"wp-image-3114\" srcset=\"https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-cover-b.webp 1200w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-cover-b-300x169.webp 300w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-cover-b-1024x576.webp 1024w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-cover-b-450x253.webp 450w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-cover-b-768x432.webp 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Your self-hosted n8n sits behind Nginx or Caddy, and now its log shows <span class=\"highlighted\">ERR_ERL_UNEXPECTED_X_FORWARDED_FOR<\/span>, the editor keeps losing its connection, or the Webhook node offers a <span class=\"highlighted\">localhost:5678<\/span> link. In an n8n reverse proxy setup, the trust proxy error goes away when you set <span class=\"highlighted\">N8N_PROXY_HOPS<\/span> to the number of proxies in front of n8n. Use 1 for nginx alone and 2 with a front proxy such as Cloudflare. If the editor shows Connection lost, forward the Upgrade and Connection headers and raise nginx&#8217;s 60-second read timeout. If webhook links show localhost, set <span class=\"highlighted\">N8N_WEBHOOK_URL<\/span>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The trust proxy message is a log line, and n8n keeps serving while it appears: login still returned 200 in the lab. Variables such as <span class=\"highlighted\">N8N_TRUST_PROXY<\/span> do not exist in n8n 2.48.8 and are ignored without a warning. Only N8N_PROXY_HOPS changes the behaviour.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A reverse proxy is the web server that stands in front of n8n, accepts the visitor&#8217;s secure connection and passes each request on. Unless n8n is told how many such servers stand in front of it, every request looks as if it came from the proxy itself. It also has to be told its public address, and the proxy has to keep the editor&#8217;s live connection open. When one of these is missing, n8n still starts, so the fault can go unnoticed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Find your symptom, then go to its section:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>What you see<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td><span class=\"highlighted\">ERR_ERL_UNEXPECTED_X_FORWARDED_FOR<\/span> in the n8n log<\/td><td>N8N_PROXY_HOPS unset or 0 behind a proxy<\/td><td><strong><a href=\"#trust-proxy\">N8N_PROXY_HOPS=1<\/a><\/strong><\/td><\/tr><tr><td>No error, but every visitor shares one IP, or any IP is accepted<\/td><td>Wrong non-zero hop count<\/td><td><strong><a href=\"#hops\">Count the proxies<\/a><\/strong><\/td><\/tr><tr><td><span class=\"highlighted\">401 Unauthorized<\/span> on <span class=\"highlighted\">\/rest\/push<\/span>, editor without live updates<\/td><td>Upgrade and Connection headers not forwarded<\/td><td><strong><a href=\"#connection-lost\">Add the two headers<\/a><\/strong><\/td><\/tr><tr><td>&#8220;Connection lost&#8221; now and then, close code 1006<\/td><td>nginx&#8217;s 60 s read timeout races n8n&#8217;s 60 s ping<\/td><td><strong><a href=\"#idle-timeout\">proxy_read_timeout 3600s<\/a><\/strong><\/td><\/tr><tr><td>&#8220;Invalid origin!&#8221; right after a 101, close code 1008<\/td><td>Proxy sends the wrong Host<\/td><td><strong><a href=\"#invalid-origin\">Forward Host or X-Forwarded-Host<\/a><\/strong><\/td><\/tr><tr><td>Webhook URL shows <span class=\"highlighted\">localhost:5678<\/span> or <span class=\"highlighted\">:5678<\/span><\/td><td>No public URL set<\/td><td><strong><a href=\"#webhook-url\">N8N_WEBHOOK_URL<\/a><\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Every message and number below was reproduced with n8n 2.48.8 (npm, Node.js 24.22.3) behind nginx 1.24.0 and Caddy 2.6.2 on Ubuntu 24.04, in network namespaces with test addresses. n8n ran with NODE_ENV=production, the production mode that the official Docker image sets. The front proxy was an nginx that appends to the forwarding header the way Cloudflare does; Cloudflare itself was not tested. With no browser in the lab, the editor&#8217;s live connection was tested with ws 8.20.1, the WebSocket library bundled with n8n, and with curl. Where the commands say n8n.example.com, 203.0.113.x or 198.51.100.x, put your own domain and addresses.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"trust-proxy\">ERR_ERL_UNEXPECTED_X_FORWARDED_FOR: the trust proxy message in the n8n log<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The message comes from n8n&#8217;s rate limiter, the part that caps how many attempts one visitor address can make in a time window. It warns that n8n cannot see real visitor addresses. n8n ignores the <strong>X-Forwarded-For<\/strong> header, the list of addresses a request passed through, until N8N_PROXY_HOPS says how many proxies to trust.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:14px\"><strong>Fix<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>N8N_PROXY_HOPS=1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Set the hop count as a number in n8n&#8217;s environment and restart n8n. The <strong><a href=\"#hops\">next section<\/a><\/strong> shows which number, and the <strong><a href=\"#configs\">configs section<\/a><\/strong> shows the Docker block. Without Docker, the usual places are the systemd service that starts n8n (an <span class=\"highlighted\">Environment=N8N_PROXY_HOPS=1<\/span> line) or the shell that runs <span class=\"highlighted\">n8n start<\/span>. The lab passed the variables on the command line. The <strong><a href=\"https:\/\/docs.n8n.io\/hosting\/configuration\/environment-variables\/deployment\/\" target=\"_blank\" rel=\"noopener\">n8n deployment variables page<\/a><\/strong> lists N8N_PROXY_HOPS with a default of 0.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use a number. <span class=\"highlighted\">N8N_PROXY_HOPS=one<\/span> or <span class=\"highlighted\">=true<\/span> prints <span class=\"highlighted\">Invalid number value for N8N_PROXY_HOPS: one<\/span> at startup and then behaves as 0. A related code, <span class=\"highlighted\">ERR_ERL_INVALID_IP_ADDRESS<\/span>, means the proxy writes an address with a port into X-Forwarded-For, as IIS ARR does.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Without the fix, the rate limiter logs this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ValidationError: The 'X-Forwarded-For' header is set but the Express 'trust proxy' setting is false (default). This could indicate a misconfiguration which would prevent express-rate-limit from accurately identifying users. See https:\/\/express-rate-limit.github.io\/ERR_ERL_UNEXPECTED_X_FORWARDED_FOR\/ for more information.\n    at Object.xForwardedForHeader (\/opt\/rp-lab\/app\/node_modules\/express-rate-limit\/dist\/index.cjs:187:13)\n    [...]\n  code: 'ERR_ERL_UNEXPECTED_X_FORWARDED_FOR',<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The same nine requests went through nginx, counting error lines after each one. With N8N_PROXY_HOPS unset, login returned 200 and the process kept running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>=== b-prod-nohops: NODE_ENV=production\nGET \/healthz 200  errors so far: 0\nGET \/ 200  errors so far: 0\nGET \/rest\/settings 200  errors so far: 0\nGET \/webhook\/rp-lab-hook 200  errors so far: 0\nPOST \/rest\/login #1 200  errors so far: 1\nPOST \/rest\/login #2 200  errors so far: 1\nPOST \/rest\/login #3 200  errors so far: 1\nGET \/rest\/resolve-password-token #1 400  errors so far: 2\nGET \/rest\/resolve-password-token #2 400  errors so far: 2\nn8n process: still running\n=== d-prod-hops1: NODE_ENV=production N8N_PROXY_HOPS=1   -&gt; errors so far: 0 for all 9 requests<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The line is printed once per rate-limited route: once for login, once more for the password-reset token check. Health checks, the editor page and webhooks never trigger it. It cannot appear at startup either, because it needs an incoming request.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It also needs <strong>NODE_ENV=production<\/strong>, the setting that switches on n8n&#8217;s production-only checks, such as the IP rate limits and the origin check. The official Docker image sets it in its Dockerfile, while a plain npm install does not, and without it n8n mounts no IP rate limiter at all. In 2.48.8, <span class=\"highlighted\">\/rest\/login<\/span> allows 1000 requests per 5 minutes per IP plus 5 per minute per login e-mail. Routes such as <span class=\"highlighted\">\/rest\/resolve-password-token<\/span> allow 5 per 5 minutes per IP.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Forum answers suggest N8N_TRUST_PROXY, EXPRESS_TRUST_PROXY, N8N_USE_X_FORWARDED_HOST or N8N_DISABLE_ORIGIN_CHECK. None of the four appears anywhere in the 2.48.8 source, and none is mentioned at startup. The only place that sets trust proxy is this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>n8n\/dist\/abstract-server.js:59:        const proxyHops = this.globalConfig.proxy_hops;\nn8n\/dist\/abstract-server.js:60:        if (proxyHops &gt; 0)\nn8n\/dist\/abstract-server.js:61:            this.app.set('trust proxy', proxyHops);\nN8N_TRUST_PROXY: 0 files ; EXPRESS_TRUST_PROXY: 0 files ; N8N_USE_X_FORWARDED_HOST: 0 files ; N8N_DISABLE_ORIGIN_CHECK: 0 files<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-stamps.webp\" alt=\"Two gatekeepers stamp a parcel on its way to a server: each proxy in front of n8n adds one address to X-Forwarded-For\" class=\"wp-image-3120\" srcset=\"https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-stamps.webp 1200w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-stamps-300x169.webp 300w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-stamps-1024x576.webp 1024w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-stamps-450x253.webp 450w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-stamps-768x432.webp 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><figcaption>Every proxy stamps the request with the address it came from, and n8n counts the stamps back from the newest one.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"hops\">How many hops to set in N8N_PROXY_HOPS<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In an n8n reverse proxy setup, a hop is one proxy on the way from the visitor to n8n. Each one adds the address it received the request from to the end of X-Forwarded-For. n8n reads the list from the right and skips as many entries as N8N_PROXY_HOPS says.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The right number is the count of proxies that append to X-Forwarded-For between the internet and n8n. A wrong number above zero logs nothing.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Path<\/th><th>N8N_PROXY_HOPS<\/th><th>Also needed<\/th><\/tr><\/thead><tbody><tr><td>Visitor -&gt; Nginx -&gt; n8n<\/td><td>1<\/td><td>nothing<\/td><\/tr><tr><td>Visitor -&gt; Front proxy (e.g. Cloudflare) -&gt; Nginx -&gt; n8n<\/td><td>2<\/td><td>a front proxy that appends to X-Forwarded-For<\/td><\/tr><tr><td>Visitor -&gt; Caddy -&gt; n8n<\/td><td>1<\/td><td>nothing<\/td><\/tr><tr><td>Visitor -&gt; Front proxy -&gt; Caddy -&gt; n8n<\/td><td>2<\/td><td><span class=\"highlighted\">trusted_proxies<\/span> with the front proxy&#8217;s ranges<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"800\" src=\"https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-hops.webp\" alt=\"N8N_PROXY_HOPS counted from the right: the visitor 203.0.113.10 passes a front proxy and Nginx, X-Forwarded-For grows to 203.0.113.10, 198.51.100.50, and n8n with 2 hops finds the visitor\" class=\"wp-image-3107\" srcset=\"https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-hops.webp 1200w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-hops-300x200.webp 300w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-hops-1024x683.webp 1024w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-hops-450x300.webp 450w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-hops-768x512.webp 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><figcaption>Each proxy appends one address; N8N_PROXY_HOPS tells n8n how many to step back from the right.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The lab watched n8n&#8217;s own <span class=\"highlighted\">X-RateLimit-Remaining<\/span> header for two visitors, A (203.0.113.10) and B (203.0.113.20), plus A sending a forged X-Forwarded-For. A shared counter means n8n sees both as one address:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>client -&gt; nginx -&gt; n8n, N8N_PROXY_HOPS=1\nA 203.0.113.10               ip=203.0.113.10  remaining=4\nB 203.0.113.20               ip=203.0.113.20  remaining=4\nA forged XFF 198.51.100.66   ip=203.0.113.10  xff=\"198.51.100.66, 203.0.113.10\"  remaining=3   &lt;- forgery ignored\nclient -&gt; nginx -&gt; n8n, N8N_PROXY_HOPS=2   (one hop too many)\nA forged XFF 198.51.100.66   ip=198.51.100.66  remaining=4   &lt;- spoofed, fresh bucket\nValidationError lines in n8n log: 0\n\nclient -&gt; front proxy -&gt; nginx -&gt; n8n, N8N_PROXY_HOPS=1   (one hop too few)\nA 203.0.113.10   ip=198.51.100.50  xff=\"203.0.113.10, 198.51.100.50\"  remaining=4\nB 203.0.113.20   ip=198.51.100.50  remaining=3     &lt;- every visitor = the front proxy\nValidationError lines in n8n log: 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>One hop too few<\/strong> makes every visitor look like the front proxy at 198.51.100.50. They share one rate-limit bucket, so one visitor&#8217;s password-reset attempts count against everyone. <strong>One hop too many<\/strong> lets a visitor choose their own address by sending a forged header, and each forged value gets a fresh bucket. That weakens the login protection, so check your own server for it (the routine at the end does).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The hop count works only if the front proxy appends the visitor&#8217;s address to X-Forwarded-For. <strong><a href=\"https:\/\/developers.cloudflare.com\/fundamentals\/reference\/http-headers\/\" target=\"_blank\" rel=\"noopener\">Cloudflare&#8217;s HTTP headers reference<\/a><\/strong> says it appends to an existing header; for any other front proxy, check its documentation. The lab used an nginx stand-in, and Cloudflare Tunnel was not covered.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Caddy discards X-Forwarded-For from senders it does not trust, so Caddy alone stayed safe even with a hop count too high. Behind a front proxy, that default turned every visitor into 198.51.100.50 at any hop count. Adding <span class=\"highlighted\">trusted_proxies<\/span> with the front proxy&#8217;s ranges and N8N_PROXY_HOPS=2 fixed it. The <strong><a href=\"https:\/\/caddyserver.com\/docs\/caddyfile\/directives\/reverse_proxy\" target=\"_blank\" rel=\"noopener\">Caddy reverse_proxy reference<\/a><\/strong> describes the same default.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"connection-lost\">n8n Connection lost behind nginx: the WebSocket headers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The editor keeps a <strong>WebSocket<\/strong>, a long-lived two-way connection, open to n8n on <span class=\"highlighted\">\/rest\/push<\/span> to show executions as they run. nginx does not pass on the upgrade request that opens it unless told to, and the editor then gets no live updates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:14px\"><strong>Fix: forward the upgrade<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># http context\nmap $http_upgrade $connection_upgrade {\n    default upgrade;\n    ''      close;\n}\n# inside location \/\nproxy_http_version 1.1;\nproxy_set_header Upgrade    $http_upgrade;\nproxy_set_header Connection $connection_upgrade;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is the pattern from <strong><a href=\"https:\/\/nginx.org\/en\/docs\/http\/websocket.html\" target=\"_blank\" rel=\"noopener\">nginx&#8217;s WebSocket proxying page<\/a><\/strong>. Caddy needs nothing extra: its <span class=\"highlighted\">reverse_proxy<\/span> handles the upgrade. The full file is in the <strong><a href=\"#configs\">configs section<\/a><\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The &#8220;Connection lost&#8221; banner itself was not observed in the lab. With a valid login cookie, through an nginx config that forwarded Host and X-Forwarded-* but not Upgrade and Connection, the push request got:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>HTTP\/1.1 401 Unauthorized\nUnauthorized\nws: 0.1s handshake refused: HTTP 401 \"Unauthorized\"\naccess log: 203.0.113.10 \"GET \/rest\/push?pushRef=curlcheck HTTP\/1.1\" 401 up=401<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The 401 points at the login, yet the same cookie got <span class=\"highlighted\">101 Switching Protocols<\/span> once the headers were added. Without the upgrade, n8n takes the request for its other push transport and refuses it. With only <span class=\"highlighted\">proxy_pass<\/span>, the request failed earlier with a 500 and &#8220;Invalid origin!&#8221; in the log.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><span class=\"highlighted\">N8N_PUSH_BACKEND=sse<\/span> is a fallback for a proxy that cannot upgrade; with the headers above you do not need it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"idle-timeout\">Why Connection lost comes and goes: the 60-second race<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With the headers right, the editor can still drop its connection within minutes. nginx closes a proxied connection when nothing arrives for 60 seconds, and n8n sends its keep-alive ping every 60 seconds. Two equal timers decide whether the connection survives, so it fails at random.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The ping interval is a fixed timer in n8n&#8217;s source (<span class=\"highlighted\">setInterval(() =&gt; this.pingAll(), 60 * 1000)<\/span>). The 60 s default for <span class=\"highlighted\">proxy_read_timeout<\/span> is in the <strong><a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_proxy_module.html\" target=\"_blank\" rel=\"noopener\">ngx_http_proxy_module reference<\/a><\/strong>. Eight idle connections ran for 6 minutes with nginx defaults, four direct and four through the front proxy:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>run 2 (default 60 s):\ndefault-direct-1.txt   6 pings, last: 360.0s closed code=1000\ndefault-direct-2.txt   1 pings, last: 63.7s closed code=1006      (ping at 3.7s, drop at 63.7s)\ndefault-direct-3.txt   6 pings, last: 360.1s closed code=1000\ndefault-direct-4.txt   6 pings, last: 360.0s closed code=1000\ndefault-front-1.txt    6 pings, last: 360.0s closed code=1000\ndefault-front-2.txt    2 pings, last: 123.7s closed code=1006\ndefault-front-3.txt    1 pings, last: 116.7s closed code=1006\ndefault-front-4.txt    1 pings, last: 109.7s closed code=1006     (ping at 49.7s, drop at 109.7s)\n  \"upstream timed out\" lines: front error.log 3, vps error.log 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Counting an earlier two-connection run, 5 of 10 idle connections were cut within 6 minutes. Every drop came exactly 60.0 s after the previous ping, at the moment the next one was due. Close code 1006 means the connection ended without a WebSocket close frame, the normal goodbye message. The nginx error log names it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>upstream timed out (110: Connection timed out) while proxying upgraded connection, client: 198.51.100.50, [...] request: \"GET \/rest\/push?pushRef=[...]\"<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"800\" src=\"https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-race.webp\" alt=\"Timeline of n8n Connection lost behind nginx: n8n pings every 60 s, nginx closes after 60 s idle, the connection drops at the next ping; with 3600 s the line runs on without a drop\" class=\"wp-image-3108\" srcset=\"https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-race.webp 1200w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-race-300x200.webp 300w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-race-1024x683.webp 1024w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-race-450x300.webp 450w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/n8n-reverse-proxy-nginx-caddy-race-768x512.webp 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><figcaption>A 60 s ping against a 60 s idle timeout: the drop lands at the next ping, and 3600 s removes the race.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:14px\"><strong>Fix: give the timeout room<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>proxy_read_timeout 3600s;\nproxy_send_timeout 3600s;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With 3600 s on both nginx layers, 0 of 8 connections dropped. The lab set the timeout for the whole site; limiting it to <span class=\"highlighted\">\/rest\/push<\/span> was not tested. Caddy 2.6.2 with no timeout settings dropped 0 of 3. With a front proxy, raise its timeout too. The same 60 s default also cuts long webhook calls with a 504, a case covered in <strong><a href=\"https:\/\/hostry.com\/blog\/n8n-out-of-memory-vps\/\">the guide to n8n crashes and timeouts on a VPS<\/a><\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"invalid-origin\">&#8220;Invalid origin!&#8221; after a 101: fix the Host header<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The browser sends an <strong>Origin header<\/strong> with every WebSocket request, naming the site that opened it. n8n compares it with the site name the proxy passes on. If the proxy replaces that name, n8n accepts the upgrade and closes the connection a moment later.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:14px\"><strong>Fix: forward the public name, leave Origin alone<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>proxy_set_header Host             $host;\nproxy_set_header X-Forwarded-Host $host;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">X-Forwarded-Host alone, with Host still internal, already kept the connection open. Caddy sends both by default. Both lines are in the <strong><a href=\"#configs\">full nginx file<\/a><\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A 101 alone proves nothing. In the lab, nginx forwarded the WebSocket headers but sent an internal Host name:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>=== nginx vps-hostrewrite.conf (WebSocket headers OK, Host n8n-internal, no X-Forwarded-Host)\n0.0s HTTP 101 upgrade=websocket connection=upgrade\n0.0s open\n0.1s message: Invalid origin!\n0.1s closed code=1008 reason=\"\"\nn8n log: Origin header does NOT match the expected origin. (Origin: \"https:\/\/n8n.example.com\" -&gt; \"n8n.example.com\", Expected: \"n8n-internal\" -&gt; \"n8n-internal\", Protocol: \"https\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">n8n takes the expected name from X-Forwarded-Host if present, otherwise from Host. Setting N8N_HOST, N8N_EDITOR_BASE_URL, N8N_WEBHOOK_URL or N8N_PROXY_HOPS did not change the result. Stripping Origin on the proxy failed too, with the same 1008.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The check runs only with NODE_ENV=production. A proxy that rewrites Host can therefore work for months under an npm install and break the day you move to the Docker image. If that move is ahead of you, <strong><a href=\"https:\/\/hostry.com\/blog\/n8n-3-upgrade-self-hosted\/\">the n8n 3.0 upgrade notes for self-hosters<\/a><\/strong> cover the switch to containers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"webhook-url\">Webhook URL shows localhost or :5678 behind a proxy: set N8N_WEBHOOK_URL<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Webhook node shows URLs that n8n builds from its own settings. It does not look at the address in your browser. Until you set a public URL, n8n offers <span class=\"highlighted\">http:\/\/localhost:5678\/webhook\/&#8230;<\/span>, which outside services cannot reach.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The three URL fields from <span class=\"highlighted\">\/rest\/settings<\/span>, read as a logged-in user, for different variable sets:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>=== a: (no URL variables)\n  urlBaseWebhook: http:\/\/localhost:5678\/   urlBaseWebhookTest: http:\/\/localhost:5678\/   urlBaseEditor: http:\/\/localhost:5678\n=== b: N8N_HOST=n8n.example.com N8N_PROTOCOL=https\n  urlBaseWebhook: https:\/\/n8n.example.com:5678\/   urlBaseWebhookTest: https:\/\/n8n.example.com:5678\/   urlBaseEditor: https:\/\/n8n.example.com:5678\n=== e: N8N_WEBHOOK_URL=https:\/\/n8n.example.com\/\n  urlBaseWebhook: https:\/\/n8n.example.com\/   urlBaseWebhookTest: https:\/\/n8n.example.com\/   urlBaseEditor: https:\/\/n8n.example.com   (no WEBHOOK_URL warning)\n=== f: WEBHOOK_URL=https:\/\/wh.example.net\/ N8N_EDITOR_BASE_URL=https:\/\/n8n.example.com\n  urlBaseWebhook: https:\/\/wh.example.net\/   urlBaseWebhookTest: https:\/\/n8n.example.com\/   urlBaseEditor: https:\/\/n8n.example.com\n=== g: N8N_WEBHOOK_URL=https:\/\/wh.example.net\/ N8N_EDITOR_BASE_URL=https:\/\/n8n.example.com\n  urlBaseWebhook: https:\/\/wh.example.net\/   urlBaseWebhookTest: https:\/\/wh.example.net\/   urlBaseEditor: https:\/\/n8n.example.com<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">N8N_HOST and N8N_PROTOCOL alone keep the <span class=\"highlighted\">:5678<\/span>, because the port setting still says 5678. N8N_WEBHOOK_URL alone fixes the production, test and editor URLs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Many guides still teach the older <span class=\"highlighted\">WEBHOOK_URL<\/span>. It works, with a startup line telling you to use N8N_WEBHOOK_URL instead, but it is not a pure alias. Combined with N8N_EDITOR_BASE_URL, test webhooks follow the editor URL (run f), while the new name moves both (run g). When both names are set, N8N_WEBHOOK_URL wins.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong><a href=\"https:\/\/docs.n8n.io\/hosting\/configuration\/environment-variables\/endpoints\/\" target=\"_blank\" rel=\"noopener\">n8n endpoints variables page<\/a><\/strong> lists WEBHOOK_URL as deprecated from 2.35.0; the new name and the warning already appear in 2.30.0.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"configs\">A working n8n nginx config and Caddy reverse proxy<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both n8n reverse proxy configs below passed the same end-to-end check: login, the editor&#8217;s live connection, production and test webhooks, and no trust-proxy or origin warnings in the n8n log. They do <strong>TLS termination<\/strong>: the proxy holds the HTTPS certificate and decrypts traffic, and n8n answers plain HTTP on port 5678, reachable only from the server itself.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For nginx, these are the lines that matter, inside <span class=\"highlighted\">location \/<\/span>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>proxy_set_header Upgrade           $http_upgrade;\nproxy_set_header Connection        $connection_upgrade;\nproxy_set_header Host              $host;\nproxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;\nproxy_read_timeout 3600s;<\/code><\/pre>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>Full file: \/etc\/nginx\/conf.d\/n8n.conf (35 lines)<\/summary>\n<pre class=\"wp-block-code\"><code># \/etc\/nginx\/conf.d\/n8n.conf  (http context). Lab: certificate from a local CA; on a real server\n# certbot writes the two ssl_certificate lines.\nmap $http_upgrade $connection_upgrade {\n    default upgrade;\n    ''      close;\n}\n\nserver {\n    listen 80;\n    server_name n8n.example.com;\n    return 301 https:\/\/$host$request_uri;\n}\n\nserver {\n    listen 443 ssl;\n    server_name n8n.example.com;\n    ssl_certificate     \/etc\/letsencrypt\/live\/n8n.example.com\/fullchain.pem;  # certbot path, not in the lab run\n    ssl_certificate_key \/etc\/letsencrypt\/live\/n8n.example.com\/privkey.pem;    # certbot path, not in the lab run\n\n    client_max_body_size 32m;\n\n    location \/ {\n        proxy_pass http:\/\/127.0.0.1:5678;\n        proxy_http_version 1.1;\n        proxy_set_header Upgrade           $http_upgrade;\n        proxy_set_header Connection        $connection_upgrade;\n        proxy_set_header Host              $host;\n        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;\n        proxy_set_header X-Forwarded-Proto $scheme;\n        proxy_set_header X-Forwarded-Host  $host;\n        proxy_buffering off;\n        proxy_read_timeout 3600s;\n        proxy_send_timeout 3600s;\n    }\n}<\/code><\/pre>\n<\/details>\n\n\n\n<p class=\"wp-block-paragraph\">The Caddyfile is three lines. On a real server Caddy gets a Let&#8217;s Encrypt certificate for the name by itself (not tested in the lab, which had no public DNS):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>n8n.example.com {\n\treverse_proxy 127.0.0.1:5678\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Behind a front proxy, give Caddy the front proxy&#8217;s address ranges (198.51.100.50 was the lab&#8217;s stand-in) and set N8N_PROXY_HOPS=2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>n8n.example.com {\n\treverse_proxy 127.0.0.1:5678 {\n\t\ttrusted_proxies 198.51.100.50\/32\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With the official Docker image, the n8n variables go into the service&#8217;s <span class=\"highlighted\">environment:<\/span> block. The lab set them on an npm install, so this excerpt was not part of the run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># excerpt for docker-compose.yml, not part of the lab run\nservices:\n  n8n:\n    image: docker.n8n.io\/n8nio\/n8n\n    ports:\n      - \"127.0.0.1:5678:5678\"\n    environment:\n      - N8N_PROXY_HOPS=1          # 2 with a front proxy\n      - N8N_HOST=n8n.example.com\n      - N8N_PROTOCOL=https\n      - N8N_WEBHOOK_URL=https:\/\/n8n.example.com\/<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <span class=\"highlighted\">127.0.0.1:<\/span> prefix keeps port 5678 off the public interface. Docker writes its own firewall rules, so a plain <span class=\"highlighted\">5678:5678<\/span> can stay reachable despite UFW, as <strong><a href=\"https:\/\/hostry.com\/blog\/ai-agent-exposed-to-internet-vps\/\">our post on AI agents left open on a VPS<\/a><\/strong> shows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All of this assumes root on the server and control over the proxy config. A <strong><a href=\"https:\/\/hostry.com\/products\/vps\/\">Hostry Linux VPS on KVM<\/a><\/strong> gives you both, with Ubuntu images and a panel firewall that can keep everything except ports 80 and 443 closed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"check\">A quick outside check of the n8n reverse proxy<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A loading editor page proves little, since most of these faults do not show there. The commands below are a quick check from your laptop, the way a visitor reaches n8n. They do not test every fault, and the list after them says what each one catches.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before you run them: the settings call needs the login cookie, because the URL fields only appear for a logged-in user. The rate-limit headers exist only with NODE_ENV=production, which the Docker image sets. Each counter call uses one of the 5 requests per 5 minutes on that endpoint, so wait between runs. Use your own address and owner login.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -s -o \/dev\/null -w '%{http_code} -&gt; %{redirect_url}\\n' http:\/\/n8n.example.com\/healthz\ncurl -s -w ' %{http_code}\\n' https:\/\/n8n.example.com\/healthz\ncurl -s -c jar -o \/dev\/null -w '%{http_code}\\n' -X POST https:\/\/n8n.example.com\/rest\/login \\\n  -H 'Content-Type: application\/json' -H 'browser-id: check' \\\n  -d '{\"emailOrLdapLoginId\":\"you@example.com\",\"password\":\"...\"}'\ncurl -s -b jar -H 'browser-id: check' https:\/\/n8n.example.com\/rest\/settings | grep -o '\"urlBase[A-Za-z]*\":\"[^\"]*\"'\ncurl -s -i -N --http1.1 --max-time 3 -b jar -H 'Connection: Upgrade' -H 'Upgrade: websocket' \\\n  -H 'Sec-WebSocket-Version: 13' -H 'Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==' \\\n  -H 'Origin: https:\/\/n8n.example.com' 'https:\/\/n8n.example.com\/rest\/push?pushRef=check'\ncurl -s -D - -o \/dev\/null 'https:\/\/n8n.example.com\/rest\/resolve-password-token?token=x' | grep -i x-ratelimit-remaining\ncurl -s -D - -o \/dev\/null -H 'X-Forwarded-For: 198.51.100.66' 'https:\/\/n8n.example.com\/rest\/resolve-password-token?token=x' | grep -i x-ratelimit-remaining<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\"><li>Plain HTTP should redirect to HTTPS: 301 from nginx, 308 from Caddy.<\/li><li>The health check should answer <span class=\"highlighted\">{&#8220;status&#8221;:&#8221;ok&#8221;} 200<\/span>.<\/li><li>Login should return 200 and save a cookie in the file <span class=\"highlighted\">jar<\/span>.<\/li><li>All three URL fields should start with your public address, with no localhost and no :5678. This catches the webhook URL fault.<\/li><li>The WebSocket call should print the 101 headers and nothing after them. A 401 means missing upgrade headers; &#8220;Invalid origin!&#8221; after the 101 means a wrong Host. curl ends with exit code 28, its timeout, which is expected.<\/li><li>The last two calls compare the rate-limit counter, and they catch only a hop count that is too high: the forged call then shows the same number again. A drop (4 then 3) rules that out only, because the counter also drops with N8N_PROXY_HOPS unset and with one hop too few.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Through the nginx config above with N8N_PROXY_HOPS=1, the lab&#8217;s version of this routine printed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1) plain HTTP redirects:      301 -&gt; https:\/\/n8n.example.com\/healthz\n2) health over HTTPS:         {\"status\":\"ok\"} 200\n3) login:                     200\n4) URLs n8n will show:        \"urlBaseWebhook\":\"https:\/\/n8n.example.com\/\" \"urlBaseEditor\":\"https:\/\/n8n.example.com\" \"urlBaseWebhookTest\":\"https:\/\/n8n.example.com\/\"\n5) editor push WebSocket:     HTTP\/1.1 101 Switching Protocols   (no \"Invalid origin!\")\n6) production webhook:        {\"message\":\"Workflow was started\"} 200\n7) hop count (remaining, plain then forged XFF): 4 then 3\n8) test webhook: run: {\"data\":{\"waitingForWebhook\":true,\"formTestUrls\":[]}} ; call: {\"message\":\"Workflow was started\"} 200\n9) ws client: 0.0s HTTP 101 ... open ... 3.0s still open\n10) n8n log: ValidationError lines: 0; 'Origin header does NOT match' lines: 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On the server, the n8n log should contain neither <span class=\"highlighted\">ERR_ERL_UNEXPECTED_X_FORWARDED_FOR<\/span> nor <span class=\"highlighted\">Origin header does NOT match<\/span>. This log check is the only one here that catches N8N_PROXY_HOPS unset or 0.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One hop too few needs a second network. Call the counter endpoint from two client IPs, such as your laptop and a phone on mobile data, and check that each gets its own counter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:14px\"><strong>What this means for you<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The smallest first step is one line, N8N_PROXY_HOPS=1 behind a single nginx or Caddy (2 with a front proxy), and a restart. Then log in once and search the n8n log for ERR_ERL_UNEXPECTED_X_FORWARDED_FOR; no line means n8n now trusts the proxy. That check cannot tell whether the count is too low, so with a front proxy, compare the counter from two networks once. If the editor loses its connection or webhook links show localhost, add the matching line from the table at the top: the two upgrade headers, proxy_read_timeout 3600s, or N8N_WEBHOOK_URL.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>n8n behind Nginx or Caddy logs a trust proxy error, drops the editor connection or hands out localhost webhook links. The trust proxy error needs N8N_&#8230;<\/p>\n","protected":false},"author":6,"featured_media":3115,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"hb_lab_versions":"Ubuntu 24.04, n8n 2.48, Node.js 24, nginx 1.24, Caddy 2.6","hb_offer_kicker":"","hb_offer_title":"","hb_offer_text":"","footnotes":""},"categories":[1,78,14],"tags":[],"class_list":["post-3112","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-virtual-private-server","category-apps","category-web-servers"],"read_time":13,"_links":{"self":[{"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/posts\/3112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/comments?post=3112"}],"version-history":[{"count":3,"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/posts\/3112\/revisions"}],"predecessor-version":[{"id":3121,"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/posts\/3112\/revisions\/3121"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/media\/3115"}],"wp:attachment":[{"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/media?parent=3112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/categories?post=3112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/tags?post=3112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}