0 Comments

After we successfully installed and configured XFCE on Ubuntu 14.04 and ASP.NET Core 1.0 in part 1 of this tutorial, we have to add NGINX to expose our great Hello World app to the internet. As I mentioned in part I, it’s not advisable to expose Kestrel directly, since it’s not meant to be used like that and has lot of limitation as a front-end web server: handling of multiple host names, authentication, HTTPS offloading, caching, just to mention few.

Nginx will receive requests for specific host name (we’ll point subdomain or domain to our Azure VM) and route them to internal Kestrel URL (localhost:5000 in this example). This configuration is called Reverse Proxy. Same thing can be done also with IIS Application Request and Routing module on Windows.

Point DNS to Azure VM

Let’s start with updating our DNS records. Open your DNS provider-of-choice and add CNAME record that points to Azure VM DNS name. Azure usually creates name like myvmname.westeurope.cloudapp.azure.com. Here, I created subdomain on this blog domain: http://coretest.hudosvibe.net

image

If you don’t want to update DNS records, then just edit you OS hosts file and point subdomain.domain.com to IP address of this Azure server!

NGINX installation

SSH into your VM or RDP into XFCE, and open bash. To install nginx type:

sudo apt-get update
sudo apt-get install nginx

If you open your browser and type Azure DNS name or subdomain into address bar, nginx welcome page should open! From within VM, you can try to open (with Midori) http://localhost, the same welcome page should open (the same web opened from my dev machine and from Ubuntu with Midori): 

image

image

NGINX Reverse proxy to Kestrel server configuration

Open your favorite editor (nano, geany) as sudo, create a new file with the content (again, change sub/domain name):

server {
    listen 80;
    listen [::]:80;

    server_name subdomain.domain.com;

    location / {
        proxy_pass http://localhost:5000;
    }
}

This will forward all requests to internal Kestrel web server that’s listening on the port 5000 (it’s started with dotnet run, right?).

Save the file as /etc/nginx/sites-available/subdomain.domain.com

We have to enable this website, and that’s done by creating a link to that file in /etc/nginx/sites-enabled:

sudo ln -s /etc/nginx/sites-available/subdomain.domain.com /etc/nginx/sites-enabled/

Now open nginx configuration file

sudo nano /etc/nginx/nginx.conf, or
sudo geany /etc/nginx/nginx.conf

find and uncomment this line: server_names_hash_bucket_size 64;

After restarting nginx this reverse proxy configuration should work:

sudo service nginx restart

image

More details on nginx configuration can be found here, and about reverse proxy please visit this link.

0 Comments

xfce

Here are few tips how to create and run ASP.NET Core application under Linux (Ubuntu 14.04 LTS), on a cheap Azure VM (0.75GB RAM, 0.25 CPU cores). Windows Server requires a little bit more memory (way around that is to increase the size while you're configuring the machine, and then lower it while not working in RDP).

Goals of this exercise are:

  • Cheap Azure VM with Linux (around 10-15$/month). There are other hosting option, for example on Hetzner, for ~5€/m

  • RDP protocol, not VNC (VNC uses Jpegs to transfer screen updates, that's very slow compared to RDP protocol)

  • Some graphical UI that's not standard Ubuntu Gnome (to save memory) for basic text editing task (since I'm still struggling with linux bash text editors and copy-paste between them)

  • Dot Net Core installed, ASP.NET Core app

  • NGINX in front of kestrel, to serve as a Reverse Proxy(similar to Application Request Routing on IIS)

Luckily, there's a great blog post on how to install UI on top of Ubuntu Server, and uses XFCE:

http://colhountechblog.azurewebsites.net/how-to-setup-remote-rdp-access-on-an-azure-vm-running-linux-ubuntu-14-04-lts/

TIP: to use bash and ssh from windows, there are few options: new native Bash on Win10, Git bash, or putty. They all have ssh.
Open bash and type this to ssh into new linux vm: ssh -l username IP/DNS name 

So please, go ahead, follow those simple steps, I'll wait here.

If you're running Azure VM, firewall needs to be configured for RDP:
open VM blade, Settings, Network Interface, Settings, Network Security Group, Settings, Inbound Security Rules. There, add rule for destination port 3389.

Also, it's useful to assign DNS name to that machine, to avoid using dynamic IP address.

You should be able to RDP into the machine now! First time I got some error with "connection error", but I tried again immediately, and was able to RDP and log in into XFCE.

We can now install two apps:

  • Midori web browser (open terminal, type sudo apt-get install midori)

  • Geany text editor (in terminal type sudo apt-get install geany. It will be installed under /usr/bin folder. If you navigate there with file browser, you can right click to it and choose Send To/Desktop Create Link). Btw, don’t expect code completion, it's just syntax highlighting. And if you feel brave enough, there’s always VIM. Purpose of installing text editor is not to develop an app on web server, but just to edit configuration files!

Now we should able to open Midori and page https://www.microsoft.com/net/core#ubuntu, and follow steps to install dotnet core. Needless to say, that worked fine for me:)

To create new asp.net app, follow this: https://aspnet-aspnet.readthedocs-hosted.com/en/latest/getting-started.html . Geany text editor can be used now to edit project.json and CS files (or Nano from bash). Check for namespaces when copy-pasting from tutorial, it's probably different from the one created by dotnet new.

Executing dotnet run will start Kestel, and sample page can be opened in Midori! Happy days!

XFCE running with Midori, Kestrel, Geany takes around 70% of 0.75GB RAM, which is OK for playing with new dotnet and even running small-medium websites in production.  

Next blog post will explain how to put Nginx in front of kestrel, since that's recommended production configuration (that's how node and other platforms are exposed to internet, there is always some reverse proxy in front of platform web server – kestrel doesn't know how to cache, authenticate request, handle https, etc)

0 Comments

It usually goes like this:

  • Lets hack a prototype or proof of concept
  • Deploy to production
  • The hell, people are visiting my web site and I didn't add any application logging!

Happens to me all the time. I'm adding some logging framework when it's too late, users complained, site went down, don't even know what happened! Not really sure why is that, but my theory is that most popular framework requires XML configuration in web/app config, and it's often very difficult to find all the config options, so I tend to leave "logging feature" for the end. I also hate XML-based configuration for those types of stuff. Some logging framework supports code-based configuration, like Serilog.

Some options for logging are:

  • Elmah - really easy to add, but logs only exception
  • Nlog or Log4net - really similar frameworks, more of a personal preference which one to use
  • Serilog - interesting new framework, easy to use code based configuration yay!
  • AppInsight, New Relic, Splunk - much bigger and complex solutions and services, can be plugged in to some other framework like nlog or serilog, as a storage of events. They offer whole set of analytics, diagnostics and reporting tools. Really useful feature of those are ability to log server events - number of web request, memory consumptions, database requests and timings, etc

No matter what framework you pick, it's important to set API over logging right, and then implement it in the framework of your choice. In one of my recent projects, we created logging semantics in two dimensions: level and type:

  • Level is standard logging level like verbose, debug, info, error, etc
  • Type is something that describes an event, provides better semantics about the event, and can be, for example:
    • Diagnostics - system and application related events
    • Operations - business related events
    • Performance - benchmarks

So every log event needs to describe also those two dimensions; level and type. Translating that to an interface, it can look like:

Some logging frameworks and services like Serilog and Splunk supports storing of structured objects (DTOs) into a store, which are then later available for querying. That can be really useful feature, since those objects are stored in their "native" structure (JSON) inside NoSQL store, they are not flattened into one string, all information are preserved (think of queue messages, commands, requests, that can be stored and later inspected in case of a problem).

To support sending of objects API can be expanded little bit:

void Log(LogLevel level, LogType type, string message, params object[]  data)

There's one more neat trick that can be used to make API semantics little bit better and easier to use; we can add extension methods to for ILoggger:

And so on. You get the point:)

With  those extension methods, usage can look like this:

which will result in log entries that can look like:

14/10/2015 7:00:00 [Info] [Operation] Trying to pay the order { OrderId:1, Prop:1, Prop2:2, …}
14/10/2015 7:00:01 [Error] [Operation] Error processing payment request
  full stack trace of exception…
14/10/2015 7:00:10 [Debug] [Performance] Payment service executed in 1234ms

with log entries like this, it much easier to get the context of the entry. We can search for all Performance entries for example, and find bottlenecks, or get only business related (Operation) entries and filter out technical one.