Please consider trying inside your server block:
rewrite ^/(.*)/$ /$1 permanent;
via: https://www.scalescale.com/tips/nginx/nginx-remove-trailing-slash/
This looks for anything with a request URI starting with a '/' and ending with a slash (the '$' at the end of the search section immediately after the rewrite) and replaces it with the '/' at the beginning and the URI without the ending slash at the end. The final part indicates that it will serve 301 redirects.
Update:
To avoid removing the slash from URIs with GET parameters, please try:
if ($query_string != "") {
rewrite ^/(.*)/$ /$1 permanent;
}
It's generally not recommended to use if statements, but if you were to use a map like this:
map $query_string $trailing_slash {
"" ""
default "/";
}
rewrite ^/(.*)/$ /$1$trailing_slash permanent;
it would result in infinite redirects for any string with a query, since it would redirect to itself (both source and destination would have the trailing slash in them). Therefore, we need to use a conditional for the rewrite.