You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

压测基于Net::Async::FastCGI与Nginx的HelloWorld应用时大量出现502 Bad Gateway(非2xx响应)的问题求助

压测基于Net::Async::FastCGI与Nginx的HelloWorld应用时大量出现502 Bad Gateway(非2xx响应)的问题求助

各位社区的大佬们,我最近在搭一个基于Perl Net::Async::FastCGI 的HelloWorld FastCGI服务,用Nginx做反向代理转发请求。结果用ApacheBench做简单压测时,哪怕并发数只有50,也会出现大量的502 Bad Gateway错误,非2xx响应占比特别高,实在摸不着头脑,想请大家帮忙看看问题出在哪!

环境与配置详情

1. Nginx 配置片段

我用Unix域套接字和FastCGI服务通信,相关location配置如下:

location /helloworld/ {
  proxy_buffering off ;
  gzip off ;
  fastcgi_pass unix:/testFolder/myPath/myUDS.sock ;
  include fastcgi_params ;
}

2. FastCGI HelloWorld 测试脚本

基于IO::Async::Loop事件循环,监听Unix域套接字,收到请求后返回简单的"Hello, World!":

use strict ;
use warnings ;
use IO::Async::Loop ;
use Net::Async::FastCGI ;
# This script will respond to HTTP requests with a simple "Hello, World!" message.

#If using TCP port for communication:
#my $PORT = 9890 ;

#If using Unix domain socket for communication:
my $uds = 'myUDS.sock' ;

# Create an event loop
my $loop = IO::Async::Loop->new() ;

# Define the FastCGI request handler subroutine
sub on_request {#Parms: request(Net::Async::FastCGI::Request); #Return: void;
  my ( $fcgi, $req ) = @_ ;
  # Prepare the HTTP response
  my $response = "Hello, World!\n" ;
  my $respLen  = length( $response ) ;
  # Print HTTP response headers
  $req->print_stdout(
    "Status: 200 OK" . "\n" .
    "Content-type: text/plain" . "\n" .
    "Content-length: " . $respLen . "\n" .
    "\n" .
    $response
  ) ;
  # Finish the request
  $req->finish() ;
}#end sub

# Create a new FastCGI server instance
my $fcgi = Net::Async::FastCGI->new(
  #handle     => \*STDIN ,       # Read FastCGI requests from STDIN
  on_request => \&on_request ,  # Assign the request handler subroutine
) ;

# Add the FastCGI server instance to the event loop
$loop->add($fcgi) ;

$fcgi->listen(
  #service          => $PORT , #if using TCP portnum
  addr => {# if using Unix domain socket
    family   => "unix" ,
    socktype => "stream" ,
    path     => "$uds" ,
  } ,
  #host             => '127.0.0.1' ,
  on_resolve_error => sub { print "Cannot resolve - $_[-1]\n" } ,
  on_listen_error  => sub { print "Cannot listen - $_[-1]\n" } ,
) ;

$SIG{ HUP } = sub {
  system( "rm -f $uds" ) ;
  exit ;
} ;
$SIG{ TERM } = sub {
  system( "rm -f $uds" ) ;
  exit ;
} ;
$SIG{ INT } = sub {
  system( "rm -f $uds" ) ;
  exit ;
} ;

# Run the event loop
$loop->run() ;

3. ApacheBench 压测命令

我使用的压测命令为:

ab -l -v 2 -n 100 -c 50 "http://localhost:9510/helloworld/"

压测关键结果

压测结果里,非2xx响应占比极高,全部都是502 Bad Gateway:

Concurrency Level: 50
Time taken for tests: 0.015 seconds
Complete requests: 100
Failed requests: 0
Non-2xx responses: 85 # 注:所有非2xx响应均为502 Bad Gateway

我的当前状态与疑问

我已经确认:

  • FastCGI服务正常启动,Unix套接字文件存在且权限无异常
  • 单请求访问完全正常,只有并发压测时才会批量出现502
  • Nginx启动无明显报错(可能日志级别未开足够,暂未找到对应错误日志)

想请教社区各位:

  1. 这种场景下,批量502的可能根因是什么?
  2. 有没有大佬可以帮忙复现问题?只需要Perl 5.14+,安装Net::Async::FastCGI,配置好Nginx与测试脚本,启动后执行压测命令即可
  3. 我应该从哪些方向排查?比如调整Nginx的FastCGI相关参数?还是Net::Async::FastCGI的服务配置?

麻烦各位大神指点迷津,万分感谢!

火山引擎 最新活动