My gatsby site redirects to trailing slash and then redirects again back to non-trailing slash.
nginx config file:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name example.com;
return 301 https://www.$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
server_name www.example.com;
location ~ /.well-known {
allow all;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location ~* \.(?:html)$ {
add_header Cache-Control "public, max-age=0, must-revalidate";
}
location = /sw.js {
add_header Cache-Control "public, max-age=0, must-revalidate";
}
location /page-data {
add_header Cache-Control "public, max-age=0, must-revalidate";
}
location /static {
add_header Cache-Control "public, max-age=31536000, immutable";
}
location ~* \.(?:js|css)$ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
error_page 404 /404.html;
}
When I open my website with a trailing slash, I see a redirect to the same URL but without a trailing slash:
example.com/hello-world/ -> example.com/hello-world
That's the expected behavior.
But when I open my website without a trailing slash, I see a redirect to a trailing slash, and then a redirect to without a trailing slash again.
example.com/hello-world -> example.com/hello-world/ -> example.com/hello-world
This should not happen. Where did I go wrong in my configuration? Any help is very much appreciated.