3

I'm trying to host my asp.net core (.NET 5.0) on linux with nginx. My App contains a SignalR Hub.

I Managed to make the hosting work with the default configuration from microsoft:

https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-5.0

server {
listen        80;
server_name   example.com *.example.com;
location / {
    proxy_pass         http://127.0.0.1:5000;
    proxy_http_version 1.1;
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection keep-alive;
    proxy_set_header   Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
}}

I get access to asp.net core website but I know recieve this error: enter image description here

Fortunately microsoft got me cover and suggest me this configuration for Nginx:

https://learn.microsoft.com/en-us/aspnet/core/signalr/scale?view=aspnetcore-5.0#linux-with-nginx

http {
  map $http_connection $connection_upgrade {
    "~*Upgrade" $http_connection;
    default keep-alive;
}

  server {
    listen 80;
    server_name example.com *.example.com;

    # Configure the SignalR Endpoint
    location /signalRHub {
      # App server url
      proxy_pass http://localhost:5000;

      # Configuration for WebSockets
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
      proxy_cache off;

      # WebSockets were implemented after http/1.0
      proxy_http_version 1.1;

      # Configuration for ServerSentEvents
      proxy_buffering off;

      # Configuration for LongPolling or if your KeepAliveInterval is longer than 60 seconds
      proxy_read_timeout 100s;

      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
    }
  }
}

I've tried changing the proxy pass from http://localhost:5000; to http://127.0.0.1:5000; like in the first example without success.

When I start Nginx I recieve this: enter image description here

I also have added this to my Startup class Configure method. I believe this is necessary but I'm not sure.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStringLocalizer<Startup> localizer)
      {
            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });
...
}

Everything is working fine on my windows dev machine but everything breaks down on linux and nginx configuration.

Deploying on linux is not something that I wanted but I'm stuck with it for costs reason.

Thanks in advance for your time and help.

R.Haughton
  • 227
  • 4
  • 16
  • SignalR requires a working websocket. It's a bit tricky to properly setup on nginx reverse proxy. Also, whatever nginx does, it has nothing to do with your app - you should fix your nginx setup first. Your app server needs not even be running for this. – oakad Aug 12 '21 at 10:00

0 Answers0