How to use 2 ports (1 HTTPS, 1 gRPC) in Azure Web Service with Nest.js

Issue: I am currently working on a Nest.js application hosted on Azure Web Service and encountering difficulties configuring two ports—one for HTTPS and another for gRPC. The application logs indicate that the HTTP server is listening at http://0.0.0.0:3001, and the gRPC server is listening at http://0.0.0.0:4001. However, I am seeking guidance on how to enable HTTPS on port 3001 and establish a secure gRPC connection on port 4001. It's worth mentioning that gRPC is configured to connect to three servers within the App Service plan, using HTTP for client communication. While HTTP on port 3001 is functioning correctly, gRPC on port 4001 is not working as expected.

[Nest] 21371 - 11/26/2023, 12:04:03 AM LOG [HTTP] 🚀 Listening HTTP at http://0.0.0.0:3001
[Nest] 21371 - 11/26/2023, 12:04:03 AM LOG [GRPC] 🚀 Listening GRPC at http://0.0.0.0:4001
No errors found.

Additional Information:

Configuration details and attempts made so far. The specific challenge with gRPC not functioning as expected on port 4001. The need for simultaneous connections to three servers within the App Service plan. Question:

How can I correctly configure Azure Web Service and Nest.js to enable HTTPS on port 3001? What steps are necessary to establish a secure gRPC connection on port 4001, considering the configuration details mentioned above? Any insights or code examples would be greatly appreciated. Thank you!

Configuration Attempts: I have attempted to follow the configurations outlined in the Azure documentation here. Despite successfully configuring HTTP (port 3001), the three servers are unable to connect simultaneously. I have set the HTTP20_ONLY_PORT to 4001, and the URL for connecting to the three servers is the Azure URL using HTTP.

Upvotes: 0

Views: 524

Answers (1)

Suresh Chikkam
Suresh Chikkam

Reputation: 3415

Firstly, Azure App Service primarily supported HTTP and HTTPS protocols for web applications. You can upload your SSL certificate or use a certificate provided by Azure.

  • Here, I have certificate.pem and private-key.pem to work in local and while running the application in local it is running on https url only.

main.ts:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as fs from 'fs';
import * as https from 'https';

async function bootstrap() {
  const httpsOptions = {
    key: fs.readFileSync('path/to/private-key.pem'),
    cert: fs.readFileSync('path/to/certificate.pem'),
  };

  const app = await NestFactory.create(AppModule, { httpsOptions });
  await app.listen(3001);
}
bootstrap();

Local: enter image description here

enter image description here

Configure gRPC with HTTPS:

  • gRPC uses HTTP/2, which is secure by default. This setting will configure your site to be forwarded HTTP/2 requests. configure in your app settings link this. enter image description here

I had followed this MS doc link to configure gRPC to my webapp.

enter image description here

Here, I Ignored the Client certification mode because as I said azure app service will have HTTPS protocol inbuilt.

Upvotes: 0

Related Questions