Nestjs 服务器没有响应
Nestjs server doesn't respond
我遇到一个问题,即 nestjs 服务器在处理数据后不响应或不向客户端发送响应。我创建了一个端点,它需要时间来处理数据并将其插入数据库。我知道这不是一个好的做法,但这几天是必需的。 如果请求在 4 分钟 18 秒内完成,服务器会响应,但再过几秒,它就不会响应。不会引发超时或任何类型的错误。我尝试从 postman 发送请求,但服务器和 postman 都没有超时。如果我遗漏了任何东西,我也尝试了 try-catch。我正在通过 pm2 部署 nest 服务器。所以我尝试了没有 pm2 的 运行 服务器,但结果相同。但是,如果它是本地主机,服务器会在我的本地环境和服务器上响应。
我在这里对错误和任何问题一无所知。以下是我 运行:
的设置
- Azure VM - B4ms(16GB RAM,4 个 vCPU)
- Ubuntu 20.04
- Node.js (14.18.2)
- Nestjs (7.0)
- pm2 (5.1.2)
下面是我的main.ts文件
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { urlencoded, json } from 'express';
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: ['log', 'error', 'verbose']
});
// app.setGlobalPrefix('api');
app.use(json({ limit: '500mb' }));
app.use(urlencoded({ extended: true, limit: '500mb' }));
await app.listen(3000);
}
bootstrap();
下面是端点控制器所属的我的入职模块:
import { Module } from '@nestjs/common';
import { BatchService } from 'src/batch/batch.service';
import { USERS, OFFICES, USER_BATCH, BATCH, SCHEME, USER_OFFICE, DISTRICTS, ONBOARDED_BATCH_DETAILS } from '../core/constants/index';
import { UtilsService } from 'src/core/utils/utils.service';
import scheme from '../models/scheme';
import { SchemesService } from '../schemes/schemes.service';
import batch from '../models/batch';
import offices from '../models/offices';
import users from '../models/user.entity';
import user_batch from '../models/user_batch';
import { OfficeService } from '../office/office.service';
import { UserBatchService } from '../user-batch/user-batch.service';
import { UsersService } from '../users/users.service';
import { OnboardingController } from './onboarding.controller';
import { OnboardingService } from './onboarding.service';
import { UserOfficeService } from '../user-office/user-office.service';
import user_office from '../models/user_office';
import { HttpModule } from '@nestjs/axios';
import { DistrictService } from '../district/district.service';
import district from '../models/district';
import { OnboardedBatchDetailsService } from '../onboarded-batch-details/onboarded-batch-details.service';
import { onboarded_batch_details } from '../models/onboarded_batch_details';
@Module({
imports: [
HttpModule
],
controllers: [OnboardingController],
providers: [
OnboardingService,
UsersService,
{ provide: USERS, useValue: users },
OfficeService,
{ provide: OFFICES, useValue: offices },
UtilsService,
UserBatchService,
{ provide: USER_BATCH, useValue: user_batch },
BatchService,
{ provide: BATCH, useValue: batch },
SchemesService,
{ provide: SCHEME, useValue: scheme},
UserOfficeService,
{ provide: USER_OFFICE, useValue: user_office },
DistrictService,
{ provide: DISTRICTS, useValue: district },
OnboardedBatchDetailsService,
{ provide: ONBOARDED_BATCH_DETAILS, useValue: onboarded_batch_details }
]
})
export class OnboardingModule {}
下面是我的端点代码。
@Post('/test-batch-onboard')
@HttpCode(200)
async testBatchOnbard(
@Headers('apiKey') apiKey,
@Body() data: Array<BatchData>,
){
return new Promise((resolve, reject) => {
let t = 259000; // 4m 19s -- no response
let d = this.addBatchStudentData(apiKey, data);
try{
setTimeout(function () {
resolve("I'm done here after \n" + t/60000 + " mins.");
}, t);
}
catch(e){
console.log(e);
reject(new InternalServerErrorException());
}
})
}
这是我的 app.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config/dist/config.module';
import { ScheduleModule } from '@nestjs/schedule';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AttendanceController } from './attendance/attendance.controller';
import { DatabaseModule } from './core/database/database.module';
import { AttendanceService } from './attendance/attendance.service';
import { AttendanceModule } from './attendance/attendance.module';
import { UsersModule } from './users/users.module';
import UserAttendance from './models/user_attendance';
import { BATCH, DISTRICTS, OFFICES, ONBOARDED_BATCH_DETAILS, ORGANIZATIONS, USERS, USER_ATTENDANCE, USER_BATCH } from './core/constants';
import { SchedulerModule } from './scheduler/scheduler.module';
import { ReportsModule } from './reports/reports.module';
import { OfficeModule } from './office/office.module';
import { OrganizationModule } from './organization/organization.module';
import { DailyAttendanceTasksService } from './jobs/dailyAttendanceReport.job';
import { UsersService } from './users/users.service';
import { OfficeService } from './office/office.service';
import { OrganizationService } from './organization/organization.service';
import { BatchService } from './batch/batch.service';
import { UtilsService } from './core/utils/utils.service';
import { MailgunService } from './core/utils/mailgun.service';
import { UserBatchModule } from './user-batch/user-batch.module';
import { BatchModule } from './batch/batch.module';
import { OnboardingModule } from './onboarding/onboarding.module';
import { SchemesModule } from './schemes/schemes.module';
import { UserOfficeModule } from './user-office/user-office.module';
import { DistrictService } from './district/district.service';
import { OnboardedBatchDetailsService } from './onboarded-batch-details/onboarded-batch-details.service';
import users from './models/user.entity';
import offices from './models/offices';
import organizations from './models/organizations';
import user_attendance from './models/user_attendance';
import batch from './models/batch';
import district from './models/district';
import { onboarded_batch_details } from './models/onboarded_batch_details';
import { SendBatchDataEmailJobService } from './scheduler/send-batch-data-email-job.service';
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
ScheduleModule.forRoot(),
UsersModule,
AttendanceModule,
DatabaseModule,
SchedulerModule,
ReportsModule,
OfficeModule,
OrganizationModule,
UserBatchModule,
BatchModule,
OnboardingModule,
SchemesModule,
UserOfficeModule,
],
controllers: [AppController],
providers: [
AppService,
UsersService,
{ provide: USERS, useValue: users},
OfficeService,
{ provide: OFFICES, useValue: offices },
OrganizationService,
{ provide: ORGANIZATIONS, useValue: organizations },
AttendanceService,
{ provide: USER_ATTENDANCE, useValue: user_attendance },
UsersService,
{ provide: USER_BATCH, useValue: USER_BATCH },
BatchService,
{ provide: BATCH, useValue: batch },
DistrictService,
{ provide: DISTRICTS, useValue: district },
OnboardedBatchDetailsService,
{ provide: ONBOARDED_BATCH_DETAILS, useValue: onboarded_batch_details },
DailyAttendanceTasksService,
UtilsService,
MailgunService,
DistrictService,
OnboardedBatchDetailsService
],
})
export class AppModule { }
以上代码在我的本地环境和服务器的本地主机中都有效。如果您尝试在服务器本地发送 curl post 请求,它会 returns 响应,但对于任何其他请求,它根本不响应。我尝试使用 this 增加 nestjs 超时,但没有效果。自 2 天以来,我一直在寻找此问题的解决方案,但找不到。非常感谢任何帮助。谢谢。
编辑
以上代码在 AWS EC2 实例上正常运行并且 returns 响应。
阅读问题陈述,Azure 似乎正在中断连接以获得更大的负载和冗长的请求。
调整 Azure VM 的 MTU(最大传输单元)和 LSO(大发送卸载)参数可以帮助暂时规避此问题,但最终需要修复设计。
我通过增加 VM 的默认超时值解决了这个问题。可以通过 Azure Powershell 访问更改或增加超时所需的设置。遗憾的是,此设置在 Azure 门户中不可用。我使用了Azure powershell version 7.2
,更改超时的命令如下:
$publicIP = Get-AzPublicIpAddress -Name '<name_of_the_ip_address_associated_with_your_VM>'
$publicIP.IdleTimeoutInMinutes = "<your_time_in_mins>"
Set-AzPublicIpAddress -PublicIpAddress $publicIP
** 请检查命令的语法是否正确,因为不同版本的 powershell 有不同的语法。
我遇到一个问题,即 nestjs 服务器在处理数据后不响应或不向客户端发送响应。我创建了一个端点,它需要时间来处理数据并将其插入数据库。我知道这不是一个好的做法,但这几天是必需的。 如果请求在 4 分钟 18 秒内完成,服务器会响应,但再过几秒,它就不会响应。不会引发超时或任何类型的错误。我尝试从 postman 发送请求,但服务器和 postman 都没有超时。如果我遗漏了任何东西,我也尝试了 try-catch。我正在通过 pm2 部署 nest 服务器。所以我尝试了没有 pm2 的 运行 服务器,但结果相同。但是,如果它是本地主机,服务器会在我的本地环境和服务器上响应。
我在这里对错误和任何问题一无所知。以下是我 运行:
的设置- Azure VM - B4ms(16GB RAM,4 个 vCPU)
- Ubuntu 20.04
- Node.js (14.18.2)
- Nestjs (7.0)
- pm2 (5.1.2)
下面是我的main.ts文件
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { urlencoded, json } from 'express';
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: ['log', 'error', 'verbose']
});
// app.setGlobalPrefix('api');
app.use(json({ limit: '500mb' }));
app.use(urlencoded({ extended: true, limit: '500mb' }));
await app.listen(3000);
}
bootstrap();
下面是端点控制器所属的我的入职模块:
import { Module } from '@nestjs/common';
import { BatchService } from 'src/batch/batch.service';
import { USERS, OFFICES, USER_BATCH, BATCH, SCHEME, USER_OFFICE, DISTRICTS, ONBOARDED_BATCH_DETAILS } from '../core/constants/index';
import { UtilsService } from 'src/core/utils/utils.service';
import scheme from '../models/scheme';
import { SchemesService } from '../schemes/schemes.service';
import batch from '../models/batch';
import offices from '../models/offices';
import users from '../models/user.entity';
import user_batch from '../models/user_batch';
import { OfficeService } from '../office/office.service';
import { UserBatchService } from '../user-batch/user-batch.service';
import { UsersService } from '../users/users.service';
import { OnboardingController } from './onboarding.controller';
import { OnboardingService } from './onboarding.service';
import { UserOfficeService } from '../user-office/user-office.service';
import user_office from '../models/user_office';
import { HttpModule } from '@nestjs/axios';
import { DistrictService } from '../district/district.service';
import district from '../models/district';
import { OnboardedBatchDetailsService } from '../onboarded-batch-details/onboarded-batch-details.service';
import { onboarded_batch_details } from '../models/onboarded_batch_details';
@Module({
imports: [
HttpModule
],
controllers: [OnboardingController],
providers: [
OnboardingService,
UsersService,
{ provide: USERS, useValue: users },
OfficeService,
{ provide: OFFICES, useValue: offices },
UtilsService,
UserBatchService,
{ provide: USER_BATCH, useValue: user_batch },
BatchService,
{ provide: BATCH, useValue: batch },
SchemesService,
{ provide: SCHEME, useValue: scheme},
UserOfficeService,
{ provide: USER_OFFICE, useValue: user_office },
DistrictService,
{ provide: DISTRICTS, useValue: district },
OnboardedBatchDetailsService,
{ provide: ONBOARDED_BATCH_DETAILS, useValue: onboarded_batch_details }
]
})
export class OnboardingModule {}
下面是我的端点代码。
@Post('/test-batch-onboard')
@HttpCode(200)
async testBatchOnbard(
@Headers('apiKey') apiKey,
@Body() data: Array<BatchData>,
){
return new Promise((resolve, reject) => {
let t = 259000; // 4m 19s -- no response
let d = this.addBatchStudentData(apiKey, data);
try{
setTimeout(function () {
resolve("I'm done here after \n" + t/60000 + " mins.");
}, t);
}
catch(e){
console.log(e);
reject(new InternalServerErrorException());
}
})
}
这是我的 app.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config/dist/config.module';
import { ScheduleModule } from '@nestjs/schedule';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AttendanceController } from './attendance/attendance.controller';
import { DatabaseModule } from './core/database/database.module';
import { AttendanceService } from './attendance/attendance.service';
import { AttendanceModule } from './attendance/attendance.module';
import { UsersModule } from './users/users.module';
import UserAttendance from './models/user_attendance';
import { BATCH, DISTRICTS, OFFICES, ONBOARDED_BATCH_DETAILS, ORGANIZATIONS, USERS, USER_ATTENDANCE, USER_BATCH } from './core/constants';
import { SchedulerModule } from './scheduler/scheduler.module';
import { ReportsModule } from './reports/reports.module';
import { OfficeModule } from './office/office.module';
import { OrganizationModule } from './organization/organization.module';
import { DailyAttendanceTasksService } from './jobs/dailyAttendanceReport.job';
import { UsersService } from './users/users.service';
import { OfficeService } from './office/office.service';
import { OrganizationService } from './organization/organization.service';
import { BatchService } from './batch/batch.service';
import { UtilsService } from './core/utils/utils.service';
import { MailgunService } from './core/utils/mailgun.service';
import { UserBatchModule } from './user-batch/user-batch.module';
import { BatchModule } from './batch/batch.module';
import { OnboardingModule } from './onboarding/onboarding.module';
import { SchemesModule } from './schemes/schemes.module';
import { UserOfficeModule } from './user-office/user-office.module';
import { DistrictService } from './district/district.service';
import { OnboardedBatchDetailsService } from './onboarded-batch-details/onboarded-batch-details.service';
import users from './models/user.entity';
import offices from './models/offices';
import organizations from './models/organizations';
import user_attendance from './models/user_attendance';
import batch from './models/batch';
import district from './models/district';
import { onboarded_batch_details } from './models/onboarded_batch_details';
import { SendBatchDataEmailJobService } from './scheduler/send-batch-data-email-job.service';
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
ScheduleModule.forRoot(),
UsersModule,
AttendanceModule,
DatabaseModule,
SchedulerModule,
ReportsModule,
OfficeModule,
OrganizationModule,
UserBatchModule,
BatchModule,
OnboardingModule,
SchemesModule,
UserOfficeModule,
],
controllers: [AppController],
providers: [
AppService,
UsersService,
{ provide: USERS, useValue: users},
OfficeService,
{ provide: OFFICES, useValue: offices },
OrganizationService,
{ provide: ORGANIZATIONS, useValue: organizations },
AttendanceService,
{ provide: USER_ATTENDANCE, useValue: user_attendance },
UsersService,
{ provide: USER_BATCH, useValue: USER_BATCH },
BatchService,
{ provide: BATCH, useValue: batch },
DistrictService,
{ provide: DISTRICTS, useValue: district },
OnboardedBatchDetailsService,
{ provide: ONBOARDED_BATCH_DETAILS, useValue: onboarded_batch_details },
DailyAttendanceTasksService,
UtilsService,
MailgunService,
DistrictService,
OnboardedBatchDetailsService
],
})
export class AppModule { }
以上代码在我的本地环境和服务器的本地主机中都有效。如果您尝试在服务器本地发送 curl post 请求,它会 returns 响应,但对于任何其他请求,它根本不响应。我尝试使用 this 增加 nestjs 超时,但没有效果。自 2 天以来,我一直在寻找此问题的解决方案,但找不到。非常感谢任何帮助。谢谢。
编辑
以上代码在 AWS EC2 实例上正常运行并且 returns 响应。
阅读问题陈述,Azure 似乎正在中断连接以获得更大的负载和冗长的请求。 调整 Azure VM 的 MTU(最大传输单元)和 LSO(大发送卸载)参数可以帮助暂时规避此问题,但最终需要修复设计。
我通过增加 VM 的默认超时值解决了这个问题。可以通过 Azure Powershell 访问更改或增加超时所需的设置。遗憾的是,此设置在 Azure 门户中不可用。我使用了Azure powershell version 7.2
,更改超时的命令如下:
$publicIP = Get-AzPublicIpAddress -Name '<name_of_the_ip_address_associated_with_your_VM>'
$publicIP.IdleTimeoutInMinutes = "<your_time_in_mins>"
Set-AzPublicIpAddress -PublicIpAddress $publicIP
** 请检查命令的语法是否正确,因为不同版本的 powershell 有不同的语法。