microsoft / service-fabric-aspnetcore

This repo contains ASP.NET Core integration for Service Fabric Reliable Services.
Other
153 stars 50 forks source link

Setup with Nginx and reverse proxy #66

Open fleed opened 5 years ago

fleed commented 5 years ago

I have the following setup:

Nginx -> SF Reverse Proxy -> .net core web application (hosted as stateless service)

The problem: all links in the Asp.net core application are generated using the machine name and the internal port of the service, and not with the original address. Example: Browser url: http://mywebsite.com/ Generated links: http://{machine_name}/SFApplicationType/SFService/{path_to_the_page} Expected links: http://mywebsite.com/{path_to_the_page}

Remark: if I browse to the address http://mywebsite.com/{path_to_the_page}, I can see the page (still with broken links)

My Nginx configuration:

location / {
            proxy_pass   http://127.0.0.1:19081/SFApplicationType/SFService/;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host            $host;
            proxy_set_header X-Forwarded-For $remote_addr;
        }

my .net core app stateless service:

/// <inheritdoc />
        protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners() =>
            new[]
                {
                    new ServiceInstanceListener(
                        serviceContext => new WebListenerCommunicationListener(
                            serviceContext,
                            "ServiceEndpoint",
                            (url, listener) =>
                                {
                                    return new WebHostBuilder().UseKestrel()
                                        .ConfigureServices(
                                            (context, services) =>
                                                {
                                                    // services initialization
                                                }).UseContentRoot(Directory.GetCurrentDirectory())
                                        .UseStartup<Startup>()
                                        .UseUrls(url)
                                        .UseServiceFabricIntegration(
                                            listener,
                                            ServiceFabricIntegrationOptions.UseReverseProxyIntegration)
                                        .Build();
                                }))
                };

How can I get the expected links?

Thank you

fleed commented 5 years ago

I was able to my goal with the following fragments:

return new WebHostBuilder()
                   .UseKestrel()
                   .UseStartup<Startup>()
                   .UseIISIntegration()
                   .UseServiceFabricIntegration(
                           listener,
                           ServiceFabricIntegrationOptions.UseReverseProxyIntegration)
                   .UseUrls(url)
                   .Build();

Startup.cs [Configure (IApplicationBuilder app)]

var headersOptions = new ForwardedHeadersOptions
                {
                    ForwardedHeaders = ForwardedHeaders.All,
                    ForwardLimit = null,
                    RequireHeaderSymmetry = false
                };
                headersOptions.KnownNetworks.Clear();
                headersOptions.KnownProxies.Clear();
                app.UseForwardedHeaders(headersOptions);

nginx config:

            proxy_pass http://localhost:19081/SFApplication/WebApplication/;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header Referer $http_referer;
            proxy_set_header X-Forwarded-For $remote_addr;

I'm not sure if all of them are needed or there are side effects, so I'm still testing all scenarios.