[ASP.NET 5] Production Ready Web Server on Linux. Kestrel + nginx

By | June 4, 2015

Advertisement from Google

asp.net 5 vnext + nginxYou can run ASP.NET 5 applications on Linux using kestrel web server. This is development web server, so it has limited functionality. But sometimes you need more functions, for example, https or virtual hosts support. Today we will see how to configure a system, so you can access your application through a domain name.

First, you need ASP.NET 5 environment up and running.  You can use my article to install and configure everything, but it’s a bit outdated, so it’s better to use official documentation.

After you done with your ASP.NET 5 application, run it with following command:

dnx . kestrel

By default, kestrel will start listening port 5004 on default network interface ( So your application will be available here:

You cannot just change port from 5004 to 80, because it is not allowed in Linux to open ports lower that 1024 with default user permissions. And you do not want to run your application as a root.

To fix this issue you can use iptables to redirect all traffic from 80 to 5004 port,  but it’s not the best solution, because if you wanna have more that one application listening on the port 80  it will be not possible with iptables. So, we are gonna use nginx to solve this issue.

Install nginx. In Debian-based Linux:

Advertisement from Google

sudo apt-get install nginx

Create server block (virtual host):

sudo nano /etc/nginx/sites-available/example.com

with a following config:

server {
        listen 80;
        server_name example.com;

        location / {
                proxy_set_header    Host $host;
                proxy_set_header    X-Real-IP   $remote_addr;
                proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;

and enable nginx site.

We just configured a reverse proxy. All request to example.com will go to where you have ASP.NET 5 application runned with a kestrel server.

With nginx you can do much more than that. You can use https protocol, enable load balancing, caching, etc.

In the next article, you can find script to run Kestrel in the background and on the system’s startup

7 thoughts on “[ASP.NET 5] Production Ready Web Server on Linux. Kestrel + nginx

  1. Pingback: The Morning Brew - Chris Alcock » The Morning Brew #1876

  2. Masroor Ehsan

    Thanks for the brief step-by-step tutorial… I’m not sure kestrel is production-grade yet… but things seem to be moving towards the right direction

  3. Pingback: Running ASP.NET 5 on Cloud9 | todotnetblog

  4. Pingback: Running ASP.NET 5 on Cloud9 | Convert.ToDotNet()

  5. Pingback: Algunos consejos al hacer hosting de aplicaciones ASP.NET 5 (vNext) con Kestrel)

  6. xameeramir

    Following [jsinh](http://stackoverflow.com/u/376196)’s [blog](http://blog.jsinh.in/hosting-asp-net-5-web-application-on-linux/),

    I have created a web root directory for the ASP.NET project to reside using the below commands:

    sudo mkdir /var/www
    sudo chown www-data:www-data /var/www
    sudo mkdir /var/www/example.com
    sudo chown www-data:www-data /var/www/example.com

    The domain name which I have used is literally [example.com](http://example.com)

    Removed `default` file from below directories and restarted nginx using `sudo service nginx restart`


    Added the below code to `project.json`:

    “dependencies”: {
    “Kestrel”: “1.0.0-beta6”,
    “Microsoft.AspNet.Diagnostics”: “1.0.0-beta6”,
    “Microsoft.AspNet.Hosting”: “1.0.0-beta6”,
    “Microsoft.AspNet.Server.WebListener”: “1.0.0-beta6”,
    “Microsoft.AspNet.StaticFiles”: “1.0.0-beta6”
    “frameworks”: {
    “dnx451”: { },
    “dnxcore50”: { },
    “commands”: {
    “kestrel”: “Microsoft.AspNet.Hosting –server Kestrel –server.urls http://localhost:5004

    Created `Startup.cs`:

    using Microsoft.AspNet.Builder;
    namespace HostKWebOnLinux
    public class Startup
    public void Configure(IApplicationBuilder app)

    executing `dnu restore` prints a lot of things on console, giving me an impression that packages are restored.

    executing `dnx kestrel` results in:

    >Error: Unable to load application or execute command ‘Microsoft.AspNet.Hosting’. Available commands: kestrel.

    I’m inside `var/www/example.com` and have tried both `dnx . kestrel` and `dnx kestrel`

    This is how `var/www/example.com` looks like after Ctrl + H. There is no view file or nothing like that.


    – How do I overcome this issue and run the project?

    My final aim is to host ASP.NET on Ubuntu 14.4 using Nginx and Kestrel

    [1]: http://i.stack.imgur.com/KIldX.png

  7. oluwaseun awotipe

    hi, your tutorial about deploying aspnet5 onlinux was an eye opening for me and my team,really thank you for the post. but i do have some questions which i would like you to shed more light on , my question are below.
    1. Making a change in application config. Is auto start possible .How will runing application get updated of change in configuration? (for example in IIS app pool get updated automatically).
    2. asp.net session seems to be temporary when session is active in command line (dnx run is issued) is there a way to run that with non user profile like service account in windows.
    3. cuurently to is there any dependency of coreclr on mono.


Leave a Reply

Your email address will not be published. Required fields are marked *