From 70e4ed78e4a72de65666541fc59dcbf74448a423 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 5 Feb 2026 12:52:31 +0000 Subject: [PATCH] fix(nginx): use map directive for trailing slash redirect Previous approaches using location blocks weren't working. This uses a map directive to detect trailing slashes and a server-level if statement to redirect, which is processed before location matching. The map captures the path without the trailing slash, and the if statement returns a 301 redirect when a trailing slash is detected. Slack thread: https://apify.slack.com/archives/C0L33UM7Z/p1770281199186039 https://claude.ai/code/session_01GqcrG6JU9Y1YaSA5ns87Yz --- nginx.conf | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/nginx.conf b/nginx.conf index 9a16f9c1f1..3cbe3d1692 100644 --- a/nginx.conf +++ b/nginx.conf @@ -9,6 +9,12 @@ map $request_uri $has_no_extension { default 0; } +# Detect trailing slash (except root /) and capture path without it +map $request_uri $trailing_slash_uri { + ~^(.+)/(\?.*)?$ $1$2; + default ""; +} + server { listen 0.0.0.0:8080; server_name docs.apify.com docs.apify.loc; @@ -21,6 +27,11 @@ server { set $backend "https://apify.github.io/apify-docs"; resolver 1.1.1.1 8.8.8.8 valid=30s ipv6=off; + # redirect trailing slashes (except root /) - processed before location matching + if ($trailing_slash_uri) { + return 301 $trailing_slash_uri; + } + location = / { if ($serve_markdown) { rewrite ^ /llms.txt last; @@ -34,12 +45,6 @@ server { proxy_pass $backend$uri; } - # remove trailing slashes from all URLs (except root /) - # exact match locations (e.g., location = /sdk/js/) take priority over this regex - location ~ ^(.+)/$ { - rewrite ^(.+)/$ $1$is_args$args? redirect; - } - location / { set $rewrite_condition "$serve_markdown$has_no_extension"; set $proxy_path $request_uri;