reverse proxy – Nginx proxy_pass only works with the trailing slash

I'm trying to host a Shiny server on the / shiny / subpath of my website. Using the configuration below, I can access my demo application via www.example.com/shiny/demo/.

However, when I exclude the last slash from the URI, I find myself instead at the address www.example.com/demo/, which does not exist. I do not know which part of my configuration leads to this redirection: although I have rewrite_log on I do not see anything in error.log. If I remove the rewrite or delete the final slash from proxy_pass, everything stops working. The end slash in the location directive is not strictly necessary (I think?), But I would prefer to keep it, removing it pretending to make no difference either. I'm not sure if I need proxy_redirect, but removing it does not solve the problem.

Any suggestions on how I can fix my configuration so that / shiny / demo redirection to / demo / go to / shiny / demo / instead?

My configuration, with the corresponding location at the bottom:

# Redirect to HTTPS
server {
listen 80 default_server;
listening [::]: 80 default_server;

server_name example.com www.example.com;
return 301 https: // $ servername $ request_uri;
}

# Redirect to the www subdomain
server {
listen 443 ssl default_server;
listening [::]: 443 ssl default_server;

ssl_certificate / path / to / cert;
ssl_certificate_key / path / to / key;

add_header Strict-Transport-Security "max-age = 15768000; includeSubDomains; preload" always;

server_name example.com;
return 301 https: //www.example.com$request_uri;
}

# Main server
server {
listen to 443 ssl http2;
listening [::]: 443 ssl http2;

root / path / to / root;

index.php index;
server_name www.example.com;

ssl_certificate / path / to / cert;
ssl_certificate_key / path / to / key;

shared ssl_session_cache: SSL: 50m;
ssl_session_timeout 1d;
ssl_session_tickets off;

add_header Strict-Transport-Security "max-age = 15768000; includeSubDomains; preload" always;


location / {
if ($ request_uri ~ ^ / (. *) . html $) {return 302 / $ 1; }
try_files $ uri / $ uri $ uri.html $ uri.php $ is_args $ query_string;
}

location ~  .php $ {
if ($ request_uri ~ ^ / ([^?]*) . php ($ | ?)) {return 302 / $ 1 $ is_args $ query_string; }
include snippets / fastcgi-php.conf;
fastcgi_pass unix: /run/php/php7.0-fpm.sock;
}


location ^ ~ / brilliant / {
rewrite_log on;
rewrite ^ / brilliant /(.*)$ / 1 pause $;
proxy_pass http: // localhost: 1234 /;
proxy_redirect http: // localhost: 1234 / $ scheme: // $ server_name / shiny /;

proxy_http_version 1.1;
proxy_set_header Upgrading $ http_upgrade;
proxy_set_header "upgrade" connection;
proxy_set_header X-Real-IP $ remote_addr;
proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
proxy_set_header Host $ http_host;
proxy_set_header X-NginX-Proxy true;
}
}