title: SignalR + Redis in cluster not working
body: <h2>Background</h2>
  
  <p>When running the website with single application instance (container) - SignalR is working perfectly. </p>
  
  <p>When scaling out to more instances (>1), it throws errors and is just not working. I looked for an explanation on the internet and found that I need to configure my Signalr to work in cluster. I choose <a href="https://docs.microsoft.com/en-us/aspnet/signalr/overview/performance/scaleout-with-redis" rel="noreferrer">Redis as my backplane</a>.</p>
  
  <p>I worked through many tutorials on how to do it right and it's just not working for me. </p>
  
  <h2>Environment</h2>
  
  <p>I working asp.net core v2.1 hosted in Google cloud. The application is deployed as Docker container, using Kestrel + nginx. The docker container runs in a Kubernetes cluster behind a load balancer.</p>
  
  <h2>My Configuration</h2>
  
  <p>Startup.cs:</p>
  
  <pre><code>public class Startup
  {
      public Startup(IConfiguration configuration,
          IHostingEnvironment hostingEnvironment)
      {
          Configuration = configuration;
          HostingEnvironment = hostingEnvironment;
      }
  
      // This method gets called by the runtime. Use this method to add services to the container.
      public void ConfigureServices(IServiceCollection services)
      {
          // ...
          services.AddSignalR().AddRedis(options =&gt;
          {
              options.Configuration = new ConfigurationOptions
              {
                  EndPoints =
                      {
                          { ConfigurationManager.Configuration["settings:signalR:redis:host"], int.Parse(ConfigurationManager.Configuration["settings:signalR:redis:port"])}
                      },
                  KeepAlive = 180,
                  DefaultVersion = new Version(2, 8, 8),
                  Password = ConfigurationManager.Configuration["settings:signalR:redis:password"]
              };
          });
          // ...
      }
  
      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
      {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
          else
          {
              app.UseExceptionHandler("/errors/general");
              app.UseHsts();
          }
  
          // nginx forward
          app.UseForwardedHeaders(new ForwardedHeadersOptions
          {
              ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
          });
  
          app.UseSignalR(routes =&gt;
          {
              routes.MapHub&lt;StatisticsHub&gt;("/hubs/myhub");
          });
      }
  }
  </code></pre>
  
  <p>In order to verify that the connection to Redis server succeeded, I checking the output window of the Kastrel:
  <a href="https://i.stack.imgur.com/Dcg94.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Dcg94.png" alt="enter image description here"></a></p>
  
  <p>Same behaviour (connected) found also on servers (2 replicas, not on development enviroment).</p>
  
  <p>In order to verify that the Signalr is "really" using the Redis (not just connecting), I used <code>redis-cli</code> to connect the Redis server and found that:</p>
  
  <p><a href="https://i.stack.imgur.com/a00xW.png" rel="noreferrer"><img src="https://i.stack.imgur.com/a00xW.png" alt="enter image description here"></a></p>
  
  <p>From this I can understand that there are some "talks" on Redis. </p>
  
  <p>I removed the website LoadBalancer (GCP) and deployed it agian. Now with Sticky-Session: ClientIP. This loadbalancer is routing requests to the different containers.</p>
  
  <p>The only place that I didnt changed is the nginx configuration. I'm wrong?</p>
  
  <h2>The result</h2>
  
  <p>SignalR not working on the browser. These errors are from the browser console:</p>
  
  <pre><code>scripts.min.js:1 WebSocket connection to 'wss://site.com/hubs/myhub?id=VNxKLazEKr9FKM4GPZRDhA' failed: Error during WebSocket handshake: Unexpected response code: 404
  scripts.min.js:1 Error: Failed to start the transport 'WebSockets': undefined
  Error: Connection disconnected with error 'Error: Server returned handshake error: Handshake was canceled.'.
  scripts.min.js:1 Error: Connection disconnected with error 'Error: Server returned handshake error: Handshake was canceled.'.
  files.min.js:1 Uncaught (in promise) Error: Error connecting to signalR. Error= Error
  ...
  </code></pre>
  
  <h2>Question</h2>
  
  <p>What I missed? What to check?</p>