Host asp net core grpc service on linux with nginx

Introduction

ASP.NET is a popular programming language used for web applications. In this article, we will explore how to host an ASP.NET Core gRPC service on with Nginx.

Prerequisites

Before we begin, make sure you have the following:

  • A Linux machine with ASP.NET Core SDK installed
  • Nginx installed on the Linux machine

Step 1: Create a new ASP.NET Core gRPC service

First, let's create a new ASP.NET Core gRPC service. Open a terminal and run the following :

dotnet new grpc -n MyGrpcService

This will create a new directory named “MyGrpcService” with the necessary files for an ASP.NET Core gRPC service.

Step 2: and run the gRPC service

Next, to the directory by running:

cd MyGrpcService

Build the project by running:

dotnet build

Once the build is successful, run the gRPC service by executing:

dotnet run

This will start the gRPC service on the default port 5001.

Step 3: Configure Nginx

Now, let's configure Nginx to act as a reverse proxy for our gRPC service. Open the Nginx configuration file by running:

sudo nano /etc/nginx/nginx.conf

Inside the “http” , add the following block:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:5001;
        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;
    }
}

Replace “example.com” with your or IP address.

Step 4: Restart Nginx

Save the changes to the Nginx configuration file and exit the editor. Restart Nginx to apply the changes by running:

sudo systemctl restart nginx

Step 5: Test the gRPC service

Finally, test the gRPC service by sending a request to the Nginx server. You can use tools like grpcurl or grpc_cli to send gRPC requests.

For example, to send a request to the gRPC service using grpcurl, run:

grpcurl -plaintext -d '{"message": "Hello"}' example.com:80 MyGrpcService.Greeter/SayHello

You should a response from the gRPC service.

Conclusion

In this article, we have learned how to host an ASP.NET Core gRPC service on Linux with Nginx. By following the steps outlined above, you can easily deploy your gRPC service and use Nginx as a reverse proxy to handle incoming requests.

Rate this post

Leave a Reply

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

Table of Contents