How to Configure Nginx for Internal Access to Raspberry Pi 4 ASP.NET Core 3.0 Website

How to Configure Nginx for Internal Access to Raspberry Pi 4 ASP.NET Core 3.0 Website
How to Configure Nginx for Internal Access to Raspberry Pi 4 ASP.NET Core 3.0 Website

Celebrate National Day

How to Configure Nginx for Internal Access to Raspberry Pi 4 ASP.NET Core 3.0 Website

Click the blue text above to follow “Wang Yujie Blog”

How to Configure Nginx for Internal Access to Raspberry Pi 4 ASP.NET Core 3.0 Website

Introduction

A few days ago, I posted two articles: “Installing .NET Core 3.0 Runtime and SDK on Raspberry Pi 4” and “‘Auto-start’ .NET Core 3.0 Environment on Raspberry Pi”. However, there is still a pit: our website can only be accessed via localhost. Although the dotnet environment variable can be ‘auto-started’, the website itself cannot start with the system. Today, let’s see how to make the setup more complete.

Just Run: Kestrel Hosting

If your requirement is just temporary internal network access, you can use Kestrel to host the web server. You just need to give the dotnet command a –urls parameter to set the allowed hostname and port number. I do not want to restrict the hostname, so I used * here.

dotnet Empower.dll –urls “http://*:8080”

Now, your internal network machine can access the website on the Raspberry Pi:

How to Configure Nginx for Internal Access to Raspberry Pi 4 ASP.NET Core 3.0 Website

However, this method has certain drawbacks. For example, if your code crashes, as long as there is an exception, the dotnet process will end, and you must manually restart it to continue using the website. Moreover, Kestrel’s functionality is not as powerful as a normal web server. Therefore, in a more realistic environment, we still need to use a normal web server (like Nginx) to do reverse proxy and automatically restart the dotnet process.

Using Nginx + systemd

First, install and start nginx

sudo apt-get install nginx

sudo /etc/init.d/nginx start

Open the configuration file

sudo nano /etc/nginx/sites-available/default

Replace with the following content

server {

listen 80 default_server;

server_name _;

location / {

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;

}

}

Where server_name set to _; means no restriction on hostname access. proxy_pass corresponds to Kestrel’s default terminal address.

Apply the settings

sudo nginx -t

sudo nginx -s reload

Now, start your ASP.NET Core website, and you should be able to access it on port 80 in the internal network.

dotnet Empower.dll

How to Configure Nginx for Internal Access to Raspberry Pi 4 ASP.NET Core 3.0 Website

Now, we have one last step, which is to make the dotnet process automatically restart whenever it crashes. This can be achieved using the systemd service.

sudo nano /etc/systemd/system/kestrel-empowerapp.service

The content is as follows

[Unit]

Description=ASP.NET Core 3.0 App – Empower

[Service]

WorkingDirectory=/home/pi/dotnet-playground/empower/portable-fdd

ExecStart=/home/pi/dotnet-arm32/dotnet /home/pi/dotnet-playground/empower/portable-fdd/Empower.dll

Restart=always

# Restart service after 10 seconds if the dotnet service crashes:

RestartSec=10

KillSignal=SIGINT

SyslogIdentifier=dotnet-empower

User=pi

Environment=ASPNETCORE_ENVIRONMENT=Production

Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]

WantedBy=multi-user.target

Note that systemd requires us to use absolute paths.

Register and start the service:

sudo systemctl enable kestrel-empowerapp.service

sudo systemctl start kestrel-empowerapp.service

sudo systemctl status kestrel-empowerapp.service

How to Configure Nginx for Internal Access to Raspberry Pi 4 ASP.NET Core 3.0 Website

Now, try restarting your Raspberry Pi, the website will automatically start, and the local network port 80 will be accessible, and the service will automatically restart even if the code crashes!

How to Configure Nginx for Internal Access to Raspberry Pi 4 ASP.NET Core 3.0 Website

For more detailed configurations, you can click [Read Original] to refer to the official Microsoft documentation.

How to Configure Nginx for Internal Access to Raspberry Pi 4 ASP.NET Core 3.0 Website

Leave a Comment