I have MediaCMS set up on a subdomain, and I would like to be able to access it from path on the root domain. That is, it is currently hosted and accessible at https://media.example.com, and I would like to be able to go to https://example.com/media to use it.

I've tried the following configuration file for the example.com server block, but when I access example.com/media, it gives an error, which I assume is generated by MediaCMS, that simply says "you are lost!". Does anyone know how to get this working?

server {
  root /var/www/example.com/html;
  index index.php index.html index.htm index.nginx-debian.html;
  server_name example.com www.example.com;

  resolver 8.8.8.8;

  location /media/ {
    proxy_set_header Host media.example.com;
    proxy_pass https://media.example.com;
    proxy_set_header X-Forwarded-Proto $scheme;
  }


  location / {
    try_files $uri $uri/ =404;

    location ^~ /phpmyadmin/ {
      auth_basic "Admin Login";
      auth_basic_user_file /etc/nginx/pma_pass;

      location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
      }  
    }
  }


  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
  }
        

  listen [::]:443 ssl ipv6only=on; # managed by Certbot
  listen 443 ssl; # managed by Certbot
  ssl_certificate /etc/letsencrypt/media/example.com/fullchain.pem; # managed>
  ssl_certificate_key /etc/letsencrypt/media/example.com/privkey.pem; # manag>
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}

server {
  if ($host = www.example.com) {
    return 301 https://$host$request_uri;
  } # managed by Certbot


  if ($host = example.com) {
    return 301 https://$host$request_uri;
  } # managed by Certbot


  listen 80;
  listen [::]:80;

  server_name example.com www.example.com;
  return 404; # managed by Certbot
}