Introduction
The cross-platform nature of ASP.NET core makes it possible to host the application on Linux. ASP.NET core also consists of an embedded web server and runtime that runs the application code. This opens door to host your web applications even on a small Linux-based device like Raspberry Pi.
Though an additional server is not needed, typically the embedded server called Kestrel is not designed to be exposed to the open internet. Kestrel requires a reverse proxy configured on top of it to protect the application from possible cyber-attacks. A reverse proxy fully shields the actual web server (Kestrel) from incoming requests. Another application of a reverse proxy is SSL termination where only the reverse proxy requires the SSL certificate. Communication from the proxy to host of applications behind proxy can use plain HTTP. This simplifies the SSL setup.
The two main hosting options for web applications on Raspberry Pi are Nginx and Apache. Nginx is a very popular open-source web server due to the use of asynchronous architecture to process web requests.
To configure Nginx as a reverse proxy to forward requests to your .NET Web API, you need to set up Nginx to listen for incoming HTTP requests and forward them to your .NET Web API application.
Hosting .NET Web API on Raspberry Pi involves two key steps:
Setting up a Nginx web server on the Raspberry Pi and configuring it as a reverse Proxy
Deploying and setting up .NET Web API to receive web requests from Nginx
Here’s a general guide for configuring Nginx to work as a reverse proxy for a .NET Web API. The default distro from Raspberry Pi is Debian, you could install other flavors of Linux.
- Install Nginx on Raspberry Pi:
For Ubuntu/Debian:
bash
sudo apt update
sudo apt install nginx
For CentOS/RedHat:
bash
sudo yum install nginx
- Test the Nginx installation:
After setting up Nginx, try making a request to the Nginx server using the host name or IP address of the Raspberry Pi from a different terminal in LAN. If the Nginx installation is successful, a default page will be displayed. Congrats!! your Raspberry Pi is now a web server.
- Configure Reverse Proxy in Nginx:
The main configuration file for Nginx is located at /etc/nginx/nginx.conf or /etc/nginx/sites-available/default (for Ubuntu/Debian). You will need to modify this file to define the proxy settings.
It would be a good practise to create a backup of this file. Run cp /etc/nginx/sites-enabled/default ~/nginx-default-backup
to backup file to home directory.
In the Nginx configuration file, you need to define a server
block to forward HTTP requests to your .NET Web API.
Open your configuration file (e.g., /etc/nginx/sites-available/default for Ubuntu or /etc/nginx/nginx.conf for CentOS), and add or modify the server block using vi or cat.
Run sudo cat /etc/nginx/sites-enabled/default
to edit the configuration file and replace the server
directive, as shown in the following sample.
Example configuration for Nginx reverse proxy:
server {
listen 80; # Nginx listens on port 80 for HTTP traffic
# Domain or IP address for your Web API (use your actual domain or IP address)
server_name example.com;
# Redirect HTTP requests to HTTPS (optional, if you are using SSL)
# return 301 https://$host$request_uri;
location / {
# The URL of your .NET Web API
proxy_pass http://127.0.0.1:5000; # Change this to the address where your .NET API is hosted, such as http://localhost:5000 or another IP.
# Configure headers to ensure proper forwarding
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection 'keep-alive';
# Optional: Configure timeouts if necessary
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_send_timeout 90;
}
# Optionally, configure SSL if you're using HTTPS
# listen 443 ssl;
# ssl_certificate /etc/nginx/ssl/nginx.crt;
# ssl_certificate_key /etc/nginx/ssl/nginx.key;
}
listen
: indicates port on which web server is listening. Nginx listens on port 80 for HTTP traffic.
proxy_pass
: This is the URL to which Nginx will forward incoming requests. It should point to the address where your .NET Web API is running (e.g., http://localhost:5000 if your .NET Web API runs locally on port 5000).
proxy_set_header
: These headers are essential to forward the correct client information (such as the real IP address of the client, host, and protocol).
- Restart Nginx:
After modifying the Nginx configuration, you need to restart Nginx to apply the changes. You can do this using the following command:
bash
sudo systemctl restart nginx
Or, if you're using an older version of Nginx:
bash
sudo service nginx restart
Now we have completed setting up a Nginx web server on the Raspberry Pi and configuring it as a reverse Proxy.
In the next post we will cover deploying and setting up a .NET application for receiving requests from Nginx.