Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nginx and kestrel setup docs for linux? #1170

Closed
davemulford opened this issue Dec 11, 2015 · 12 comments
Closed

nginx and kestrel setup docs for linux? #1170

davemulford opened this issue Dec 11, 2015 · 12 comments

Comments

@davemulford
Copy link

I'm trying to setup nginx + kestrel in order to serve an ASP.NET 5 app from Linux Mint. Is there any guidance on this? I've read docs.asp.net/installing-on-linux, as well as, a two-part blog post written back in June here and here.

Right now, I have nginx setup to forward requests to kestrel on port 5000. Things seem to work (i.e. I can see kestrel working without error in a terminal), but the browser just spins. If I Ctrl+C to quit kestrel, the page is then sent to the browser and rendered. I've verified that this is indeed what's happening with Fiddler.

My goal is to have nginx and kestrel run in daemon mode, and automatically start at boot. Is this currently possible?

@muratg
Copy link
Contributor

muratg commented Dec 11, 2015

@danroth27

@markushaslinger
Copy link

+1 on a complete linux tutorial.
While I have it working running kestrel manually from the commandline I wasn't able to daemonize it yet (using runit or daemontools or the like).

@mw007 sounds like a redirect loop issue, could you show me your nginx config?

@davemulford
Copy link
Author

I too have kestrel working from the command line, as long as I go to kestrel's port directly. When I try to use nginx as a proxy to kestrel is when things don't work.

I did work on this a bit more and found that I get different results when running kestrel in daemon mode, as opposed to using dnx kestrel or dnx web at the command line. I think this has to do with kestrel running as different users, and dnvm is choosing a different run time. I can't prove that though as I'm still new at this.

@marce155 here is my nginx config. Thanks for looking into this.

@markushaslinger
Copy link

@mw007 your config looks fine actually...
Mine is quite similar, just using $http_host for the Host header (which shouldn't be the cause of your problem) and my public domain instead of localhost as server_name (while unsurprisingly localhost works locally as well for me).
When I started I made a mistake in the config leading to a redirect loop with similar issues you described, so I thought maybe you did something similar, but that's not the case. Sorry, can't help you here...

With this configuration it's running perfectly fine for me, as long as I start kestrel as my user and not daemonized as root (see also this issue).

@davemulford
Copy link
Author

No problem Markus, thanks for looking. I do have an update on this though!

I was able to get this working once I backed up a revision. 1.0.0-rc1-update1 works while 1.0.0-rc2-16258 did not. However, returning the request takes over a minute -- the browser spins and spins, then finally is sent the html to render.

This only works when I run dnx web while logged in. Running as a service does not work. I plan on getting that going today.

If someone could close this ticket in the meantime, that would be awesome. I don't seem to have the privileges to do so.

@nguyenm100
Copy link

having the same problem using nginx to proxy_pass to kestrel for my mvc app. going directly to the port via the browser works. also i can nginx proxy_pass to aspnet sample (HelloMVC) app ONLY if Startup.cs has app.UseWelcomePage() invoked. If this line is removed then nginix + kestrel doesn't work (browser spins) but, again, going straight to the port does

using 1.0.0-rc1-update1

can we keep this ticket open?

@davemulford
Copy link
Author

@nguyenm100 you'll want to have a look at the workarounds section below. Try the second item listed and let me know if you have the same experience.

I believe this to be a bug either in nginx itself, or in how kestrel communicates with nginx. Basically, nginx doesn't get the signal that kestrel is done serving a request, so the client connection remains open. If kestrel is shut down via Ctrl+C or nginx's proxy_read_timeout is reached, the request is served as expected.

These are the software versions I'm using

  • kestrel commit a696eb89
  • nginx latest stable 1.8.0
  • dnx 1.0.0-rc1-update1 for coreclr
  • Linux Mint 17 Qiana (corresponds to Ubuntu 14.04)

These are the steps to reproduce

Start nginx using this config
sudo service nginx start

Change to the root directory of my app
cd /home/david/aspnet/WebAppSample

Start kestrel
dnx kestrel

Then browse to http://localhost in a browser. Note that I have also browsed to the page from a Windows machine on the local network. I did this so that I could capture web traffic in fiddler4.

This is the behaviour I'm experiencing

The browser "spins" as though it's waiting for the request to be served.

Workarounds

  • Press Ctrl+C to shut down kestrel
  • Add proxy_read_timeout 5s; to your nginx config, then restart nginx with sudo service nginx restart
  • Wait 60 seconds (nginx's default proxy_read_timeout value)

Both solutions (while not ideal) will cause the pages to be served.

I'm going to try using the latest nginx snapshot and see how that goes. There is a listing in their 1.9.9 changelog that mentions a bugfix while proxying to unix sockets.

@nguyenm100
Copy link

@mw007 -- thanks for posting this. I can get the samples/1.0.0-rc1-update1/HelloMvc to work using your workarounds. However, when I remove the "app.UseWelcomePage()" call from Startup.cs, it bombs but I did get a stack trace:

InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.CompilationAbstractions.ILibraryExporter' while attempting to activate 'Microsoft.AspNet.Mvc.Razor.Compilation.RoslynCompilationService'.
Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.PopulateCallSites(ServiceProvider provider, ISet1 callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound) Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.CreateCallSite(ServiceProvider provider, ISet1 callSiteChain)
Microsoft.Extensions.DependencyInjection.ServiceProvider.GetResolveCallSite(IService service, ISet`1 callSiteChain)

which looks like a known problem aspnet-contrib/AspNet.Security.OpenIdConnect.Server#190

btw- am on ubuntu 14.04.3

@nguyenm100
Copy link

I got this to work per: aspnet/KestrelHttpServer#468 or aspnet/KestrelHttpServer#418

@davemulford
Copy link
Author

Thank you @nguyenm100! A comment from aspnet/KestrelHttpServer#418 did the trick for me.

This is what I added to my nginx configuration file to solve the problem.

location / {
    ....
    proxy_set_header Connection "";
    proxy_http_version 1.1;
}

@TheRubble
Copy link

Purely for info as I had similar issues using HAProxy, I needed to add the following to the defaults section:

  • add option forceclose
  • option http-server-close
  • option http-pretend-keepalive

Apologies for being slightly off topic, but relevant.

@tugberkugurlu
Copy link
Contributor

@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants