Archive for the ‘programming’ Category


… and you want to run an ASP.Net Core application on it (be it a web API, an MVC project or whatever).
Suppose you have a supported model, like a model 4B Pi with 8 Gigs of RAM (seems familiar? Well maybe…).

If you’re fine running Kestrel and accessing it directly on port 5000, good for you. Just look at the Microsoft docs on how to set up a Systemd unit to have your app run automatically at startup and you’re good to go.

But maybe you want to run Kestrel behind a reverse proxy, like nginx.

Cool.

Again, for a simple configuration where your app runs at the root of the site, the Microsoft docs are actually fine. They also instruct you on how to forward the headers from nginx to Kestrel and how to modify your application accordingly.

But maybe you want to run your app as a subdirectory (subdirectory, not a subdomain, which should be a simpler task, as the app will reside at the root of the subdomain) of your main site.

Cool, but there are some caveats.

The first one is that missing a single trailing ‘/’ in the configuration file of nginx makes the difference between a working configuration and a non-working configuration.

After some trials and errors I got to the following sample configuration file (which is the “default” file in the “sites-available” / “sites-enabled” directory from which nginx read it’s config) .

server {
        listen 80;

        index index.html index.htm index.nginx-debian.html;

        server_name your-raspberry-name.your-domain;

        location / {
                root            /var/www/html;
                try_files       $uri $uri/ =404;
        }

        location /apps {
                alias           /var/www/apps;
                try_files       $uri $uri/ =404;
        }

        location /apps/your-app-name/ {
                proxy_pass              http://localhost:5000/;
                proxy_http_version      1.1;
                proxy_set_header        Upgrade $http_upgrade;
                proxy_set_header        Connection keep-alive;
                proxy_set_header        Host $host;
                proxy_cache_bypass      $http_upgrade;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header        X-Forwarded-Proto $scheme;
        }
}

The part of interest is, of course, the ‘location /apps/your-app-name/’ (in my case, my app is located in a sub-subdirectory because… why not?) section.
Notice the trailing ‘/’s in both the location and the proxy_pass directive.

But as I wrote, there are some caveats, not just one.

The second caveat is that, as the app is running in Kestrel, it thinks it’s running at the root of the site, not in a subdirectory (which tecnically is true, from a certain point of view). If you have an MVC app or a SPA which tries to load assets (stylesheets, scripts, images, etc.) from the path it believes it’s running from, it will fail. The result will be a crippled app, where the page will load without any asset (or, in the case of a SPA, it will not load at all).

After a lot of googling and reading every q&a on Stack Overflow even remotely related to this problem, I finally found a comment by Scott Hanselman on some random github issue that pointed me in the right direction (to be honest, that comment also pointed to the correct page to the related Microsoft docs).
The solution consist of a whopping 4 (yeah… four) LOC that have to be put inside the ‘Configure’ method:

app.Use((context, next) => {
    context.Request.PathBase = new PathString("/apps/your-app-name");
    return next.Invoke();
});

of course the argument of the PathString constructor can be retrieved from a configuration file or whatever, but for clarity sake, I have hardcoded the string in this example.
Notice the missing trailing ‘/’ in the argument.

With those two configurations I got a sample app (an app created from one ot the standard templates) running as expected inside a subdirectory with nginx and Kestrel.

Bye


Dic 31

/* A truly complicated way to... do it :) */

#include<stdio.h>

int main( int argc, char* argv[] )
{
   int values[] =
   {
      0, 25, 15, 0, 9,
      -89, 46, 23, 18, -87,
      57, 12, -4, 17, -82,
      18, -2, 1, 0, -16
   };
   char c = ( 'N' + 'C' ) / 2;
   int i;
   for( i = 0; i < 20; i++ )
   {
      c += values[ i ];
      printf( "%c", c );
   }
   return 0;
}

/* Bye */