diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/StackedKernelPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/StackedKernelPass.php
index bf8efc0..6a269e8 100644
--- a/core/lib/Drupal/Core/DependencyInjection/Compiler/StackedKernelPass.php
+++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/StackedKernelPass.php
@@ -9,6 +9,7 @@
 
 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Reference;
 
 /**
  * Provides a compiler pass for stacked HTTP kernels.
@@ -37,8 +38,17 @@ public function process(ContainerBuilder $container) {
     }
     array_multisort($middleware_priorities, SORT_DESC, $middleware_arguments, SORT_DESC);
 
+    $previous_id = 'http_kernel.basic';
     foreach ($middleware_arguments as $id => $push_arguments) {
       $http_kernel_factory->addMethodCall('push', $push_arguments);
+      // Get the definition again and add the previous middleware id as the
+      // first param.
+      $definition = $container->getDefinition($id);
+      $arguments = $definition->getArguments();
+      array_unshift($arguments, new Reference($previous_id));
+      $definition->setArguments($arguments);
+
+      $previous_id = $id;
     }
   }
 
