3
votes

I'm setting up a Service Fabric application which contains:

  • an Nginx instance as frontend (single instance, port 80)
  • some applications written with Asp.net core (1 website, some API services) (multiple instances, dynamic port)
  • a Gateway service for address resolution (single instance, port 8081)

For nginx, I'm using a solution available as Nuget package.
The gateway and, in general, the example to run .NET core app have been taken here

It is suggested by the .NET core team itself to host applications behind a real web server liken nginx. Therefore I'd like to deploy my Service Fabric application with an instance of nginx as entry point, which redirects to the Gateway service, which will do the service resolution for the replicated stateless services.

My question is about the address that I need to use in the nginx.conf to point to the Gateway address. While trying locally, I can use the local address 127.0.0.1 and it works as expected, but what happens if on a real cluster my Nginx and Gateway instances are deployed to different machines?

This is my application manifest:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="SFApplicationType" ApplicationTypeVersion="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric">
  <Parameters>
    <Parameter Name="NginxPoC_InstanceCount" DefaultValue="1" />
    <Parameter Name="Gateway_InstanceCount" DefaultValue="1" />
  </Parameters>
  <ServiceManifestImport>
    <ServiceManifestRef ServiceManifestName="NginxPoCPkg" ServiceManifestVersion="1.0.0" />
    <Policies>
      <RunAsPolicy CodePackageRef="Code" UserRef="Admin" EntryPointType="All" />
    </Policies>
  </ServiceManifestImport>
  <ServiceManifestImport>
    <ServiceManifestRef ServiceManifestName="Gateway" ServiceManifestVersion="1.0.0" />
  </ServiceManifestImport>
  <DefaultServices>
    <Service Name="NginxPoC">
      <StatelessService ServiceTypeName="NginxPoCType" InstanceCount="[NginxPoC_InstanceCount]">
        <SingletonPartition />
      </StatelessService>
    </Service>
    <Service Name="Gateway">
      <StatelessService ServiceTypeName="GatewayType" InstanceCount="[Gateway_InstanceCount]">
        <SingletonPartition />
      </StatelessService>
    </Service>
  </DefaultServices>
  <Principals>
    <Users>
      <User Name="Admin">
        <MemberOf>
          <SystemGroup Name="Administrators" />
        </MemberOf>
      </User>
    </Users>
  </Principals>
</ApplicationManifest>

and this is my current nginx.conf file:

server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass   http://127.0.0.1:8081;
        }
}

Update 2016-10-09

As requested in the discussion, I've created a test project here. Every contribute to the project is welcome.

1
Create question, but if you are creating a gateway for service resolution - why is it again that you need nginx infront of that since that also is just a proxy? I have experienced with the same idea but choose not to have nginx infront, so I am curious.Poul K. Sørensen
The .NET core team suggests having IIS or nginx in front of Kestrel. Additionally, I'll get benefits like gzip, http2, etc.fra
Let us know if these suggestions are from public links. It makes sense, and maybe there are other things worth reading from these suggestions.Poul K. Sørensen
docs.asp.net/en/latest/fundamentals/… Quote: Kestrel is designed to be run behind a proxy (for example IIS or Nginx) and should not be deployed directly facing the Internet. And here some tips: channel9.msdn.com/Series/aspnetmonsters/…fra
I coded a proxy gateway to avoid using nginx, but based on this I properly will use nginx. But then using nginx i dont see why you also want gateway project. Cant you just tell nginx to forward to the services?Poul K. Sørensen

1 Answers

1
votes

f you deploy the nginx and gateway service to all nodes (InstanceCount = -1) you should be good. If the gateway service is down on one node, you would of course not be able to forward the request from nginx to a gateway service on another node. For this, you need the nginx service to look-up the gateway service.

You can get the service endpoint address for the gateway using a REST call: https://msdn.microsoft.com/en-us/library/azure/dn707638.aspx