当前位置: 首页 > 编程文章 >

Go与Nginx性能基准测试:HTTP代理与FastCGI对比

Go与Nginx性能基准测试:HTTP代理与FastCGI对比

作者:许知时间:2026-10-27 11:45:22浏览:532

本文通过wrk工具对Go应用在不同部署模式下的性能进行基准测试,对比了Go独立运行、Nginx HTTP代理以及Nginx FastCGI(TCP和Unix Socket)三种场景。测试结果显示,在单核模式下Nginx开销较小,但在多核并发下差异显著。当需要虚拟主机或负载均衡时,推荐使用HTTP代理而非FastCGI,因为Go的FastCGI实现尚未完全优化。

本文目录展开

测试背景与目标设定

目前存在多种提供Go语言HTTP服务的方法,最佳选择需视具体应用场景而定。尽管市面上有许多优秀的Web服务器,Nginx仍是新项目的首选标准。然而,在Nginx之上托管Go应用的成本究竟如何?是必须依赖Nginx的特性(如虚拟主机、负载均衡、缓存),还是直接由Go提供服务更优?若必须使用Nginx,何种连接机制效率最高?本基准测试旨在探讨这些问题,而非单纯证明Go与Nginx孰快孰慢。

本次测试将对比以下几种不同的部署设置:

  • Go HTTP 独立服务模式(作为对照组)
  • Nginx 反向代理至 Go HTTP
  • Nginx FastCGI 至 Go TCP FastCGI
  • Nginx FastCGI 至 Go Unix Socket FastCGI

测试环境与硬件规格

硬件环境

为确保公平性,所有设置均在同一台廉价硬件上进行测试,这不应构成主要影响因素。

  • Samsung 笔记本 NP550P5C-AD1BR
  • Intel Core i7 3630QM @2.4GHz (四核八线程)
  • CPU缓存: (L1: 256KiB, L2: 1MiB, L3: 6MiB)
  • RAM 8GiB DDR3 1600MHz

软件环境

  • Ubuntu 13.10 amd64 Saucy Salamander (已更新)
  • Nginx 1.4.4 (1.4.4-1~saucy0 amd64)
  • Go 1.2 (linux/amd64)
  • wrk 3.0.4

系统内核与Nginx配置优化

系统设置 内核参数调整

仅需少量调整以提高内核限制。若有更佳建议,欢迎在评论区留言。 复制代码 代码如下:fs.file-max 9999999 fs.nr_open 9999999 net.core.netdev_max_backlog 4096 net.core.rmem_max 16777216 net.core.somaxconn 65535 net.core.wmem_max 16777216 net.ipv4.ip_forward 0 net.ipv4.ip_local_port_range 1025 65535 net.ipv4.tcp_fin_timeout 30 net.ipv4.tcp_keepalive_time 30 net.ipv4.tcp_max_syn_backlog 20480 net.ipv4.tcp_max_tw_buckets 400000 net.ipv4.tcp_no_metrics_save 1 net.ipv4.tcp_syn_retries 2 net.ipv4.tcp_synack_retries 2 net.ipv4.tcp_tw_recycle 1 net.ipv4.tcp_tw_reuse 1 vm.min_free_kbytes 65536 vm.overcommit_memory 1 资源限制

root和www-data用户的最大打开文件数限制配置为200000。 Nginx配置

需要进行若干必要的Nginx调整。为保证测试公平性,已禁用gzip压缩。以下是/etc/nginx/nginx.conf配置文件内容: 复制代码 代码如下: user www-data; worker_processes auto; worker_rlimit_nofile 200000; pid /var/run/nginx.pid; events { worker_connections 10000; use epoll; multi_accept on; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 300; keepalive_requests 10000; types_hash_max_size 2048; open_file_cache max=200000 inactive=300s; open_file_cache_valid 300s; open_file_cache_min_uses 2; open_file_cache_errors on; server_tokens off; dav_methods off; include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log combined; error_log /var/log/nginx/error.log warn; gzip off; gzip_vary off; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*.conf; } Nginx虚拟主机配置 upstream go_http { server 127.0.0.1:8080; keepalive 300; } server { listen 80; server_name go.http; access_log off; error_log /dev/null crit; location / { proxy_pass http://go_http; proxy_http_version 1.1; proxy_set_header Connection ""; } } upstream go_fcgi_tcp { server 127.0.0.1:9001; keepalive 300; } server { listen 80; server_name go.fcgi.tcp; access_log off; error_log /dev/null crit; location / { include fastcgi_params; fastcgi_keep_conn on; fastcgi_pass go_fcgi_tcp; } } upstream go_fcgi_unix { server unix:/tmp/go.sock; keepalive 300; } server { listen 80; server_name go.fcgi.unix; access_log off; error_log /dev/null crit; location / { include fastcgi_params; fastcgi_keep_conn on; fastcgi_pass go_fcgi_unix; } }

Go服务端代码实现细节

Go源代码 复制代码 代码如下: package main import ( "fmt" "log" "net" "net/http" "net/http/fcgi" "os" "os/signal" "syscall" ) var ( abort bool ) const ( SOCK = "/tmp/go.sock" ) type Server struct { } func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { body := "Hello World\n" // Try to keep the same amount of headers w.Header().Set("Server", "gophr") w.Header().Set("Connection", "keep-alive") w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Length", fmt.Sprint(len(body))) fmt.Fprint(w, body) } func main() { sigchan := make(chan os.Signal, 1) signal.Notify(sigchan, os.Interrupt) signal.Notify(sigchan, syscall.SIGTERM) server := Server{} go func() { http.Handle("/", server) if err := http.ListenAndServe(":8080", nil); err != nil { log.Fatal(err) } }() go func() { tcp, err := net.Listen("tcp", ":9001") if err != nil { log.Fatal(err) } fcgi.Serve(tcp, server) }() go func() { unix, err := net.Listen("unix", SOCK) if err != nil { log.Fatal(err) } fcgi.Serve(unix, server) }() <-sigchan if err := os.Remove(SOCK); err != nil { log.Fatal(err) } }

检查HTTP响应头

为确保测试公平,所有请求的大小必须保持一致。

复制代码 代码如下:$ curl -sI http://127.0.0.1:8080/ HTTP/1.1 200 OK Connection: keep-alive Content-Length: 12 Content-Type: text/plain Server: gophr Date: Sun, 15 Dec 2013 14:59:14 GMT $ curl -sI http://127.0.0.1:8080/ | wc -c 141 $ curl -sI http://go.http/ HTTP/1.1 200 OK Server: nginx Date: Sun, 15 Dec 2013 14:59:31 GMT Content-Type: text/plain Content-Length: 12 Connection: keep-alive $ curl -sI http://go.http/ | wc -c 141 $ curl -sI http://go.fcgi.tcp/ HTTP/1.1 200 OK Content-Type: text/plain Content-Length: 12 Connection: keep-alive Date: Sun, 15 Dec 2013 14:59:40 GMT Server: gophr $ curl -sI http://go.fcgi.tcp/ | wc -c 141 $ curl -sI http://go.fcgi.unix/ HTTP/1.1 200 OK Content-Type: text/plain Content-Length: 12 Connection: keep-alive Date: Sun, 15 Dec 2013 15:00:15 GMT Server: gophr $ curl -sI http://go.fcgi.unix/ | wc -c 141 启动测试引擎

  • 使用sysctl配置内核参数
  • 配置Nginx主配置文件
  • 配置Nginx虚拟主机
  • 以www-data用户身份启动服务
  • 执行基准测试

不同并发模式下的性能基准数据

基准测试结果

GOMAXPROCS = 1 Go独立服务模式 复制代码 代码如下: # wrk -t100 -c5000 -d30s http://127.0.0.1:8080/ Running 30s test @ http://127.0.0.1:8080/ 100 threads and 5000 connections Thread Stats Avg Stdev Max +/- Stdev Latency 116.96ms 17.76ms 173.96ms 85.31% Req/Sec 429.16 49.20 589.00 69.44% 1281567 requests in 29.98s, 215.11MB read Requests/sec: 42745.15 Transfer/sec: 7.17MB Nginx + Go via HTTP

# wrk -t100 -c5000 -d30s http://go.http/ Running 30s test @ http://go.http/ 100 threads and 5000 connections Thread Stats Avg Stdev Max +/- Stdev Latency 124.57ms 18.26ms 209.70ms 80.17% Req/Sec 406.29 56.94 0.87k 89.41% 1198450 requests in 29.97s, 201.16MB read Requests/sec: 39991.57 Transfer/sec: 6.71MB

Nginx + Go via FastCGI TCP 复制代码 代码如下: # wrk -t100 -c5000 -d30s http://go.fcgi.tcp/ Running 30s test @ http://go.fcgi.tcp/ 100 threads and 5000 connections Thread Stats Avg Stdev Max +/- Stdev Latency 514.57ms 119.80ms 1.21s 71.85% Req/Sec 97.18 22.56 263.00 79.59% 287416 requests in 30.00s, 48.24MB read Socket errors: connect 0, read 0, write 0, timeout 661 Requests/sec: 9580.75 Transfer/sec: 1.61MB Nginx + Go via FastCGI Unix Socket

# wrk -t100 -c5000 -d30s http://go.fcgi.unix/ Running 30s test @ http://go.fcgi.unix/ 100 threads and 5000 connections Thread Stats Avg Stdev Max +/- Stdev Latency 425.64ms 80.53ms 925.03ms 76.88% Req/Sec 117.03 22.13 255.00 81.30% 350162 requests in 30.00s, 58.77MB read Socket errors: connect 0, read 0, write 0, timeout 210 Requests/sec: 11670.72 Transfer/sec: 1.96MB

GOMAXPROCS = 8 Go独立服务模式 复制代码 代码如下: # wrk -t100 -c5000 -d30s http://127.0.0.1:8080/ Running 30s test @ http://127.0.0.1:8080/ 100 threads and 5000 connections Thread Stats Avg Stdev Max +/- Stdev Latency 39.25ms 8.49ms 86.45ms 81.39% Req/Sec 1.29k 129.27 1.79k 69.23% 3837995 requests in 29.89s, 644.19MB read Requests/sec: 128402.88 Transfer/sec: 21.55MB

Nginx + Go via HTTP 复制代码 代码如下:# wrk -t100 -c5000 -d30s http://go.http/ Running 30s test @ http://go.http/ 100 threads and 5000 connections Thread Stats Avg Stdev Max +/- Stdev Latency 336.77ms 297.88ms 632.52ms 60.16% Req/Sec 2.36k 2.99k 19.11k 84.83% 2232068 requests in 29.98s, 374.64MB read Requests/sec: 74442.91 Transfer/sec: 12.49MB

Nginx + Go via FastCGI TCP 复制代码 代码如下: # wrk -t100 -c5000 -d30s http://go.fcgi.tcp/ Running 30s test @ http://go.fcgi.tcp/ 100 threads and 5000 connections Thread Stats Avg Stdev Max +/- Stdev Latency 217.69ms 121.22ms 1.80s 75.14% Req/Sec 263.09 102.78 629.00 62.54% 721027 requests in 30.01s, 121.02MB read Socket errors: connect 0, read 0, write 176, timeout 1343 Requests/sec: 24026.50 Transfer/sec: 4.03MB

Nginx + Go via FastCGI Unix Socket 复制代码 代码如下: # wrk -t100 -c5000 -d30s http://go.fcgi.unix/ Running 30s test @ http://go.fcgi.unix/ 100 threads and 5000 connections Thread Stats Avg Stdev Max +/- Stdev Latency 694.32ms 332.27ms 1.79s 62.13% Req/Sec 646.86 669.65 6.11k 87.80% 909836 requests in 30.00s, 152.71MB read Requests/sec: 30324.77 Transfer/sec: 5.09MB 结论分析

测试结果分析与部署建议

在第一组基准测试中,部分Nginx设置尚未优化(如启用gzip,且Go后端未使用keep-alive连接)。在使用wrk工具并按建议优化Nginx后,结果出现了较大差异。

当GOMAXPROCS=1时,Nginx带来的开销并不明显;但当GOMAXPROCS=8时,性能差异变得巨大。未来可能会尝试其他配置。如果需要使用Nginx的虚拟主机、负载均衡或缓存等功能,建议使用HTTP代理方式,避免使用FastCGI。有观点认为Go的FastCGI支持尚未得到充分优化,这可能是导致测试结果出现巨大差异的原因。

上一篇
Smarty配置与高级缓存技术详解

Smarty通过将动态生成的HTML转换为静态文件,显著降低服务器负载。开启缓存后,在设定存活期内直接读取静态页。针对多页面需求可使用URL哈希设置缓存ID,并通过isCached判断避免重复数据库查询。对于需实时更新的模块,可采用insert法、动态block或插件block等局部缓存技术,确保特定内容不被缓存而每次重新执行。

下一篇
Shell脚本实战:Nginx部署脚本的迭代优化

本文通过四个版本的演进,展示如何从零开始编写并优化一键部署Nginx的Shell脚本。从最初的命令堆砌,到引入常量简化代码,再到增加错误判断与目录创建逻辑,最终实现支持参数传入、帮助信息及编译结果验证的健壮脚本。这种循序渐进的优化思路,体现了自动化脚本开发中循环演化的核心原则。

相关文章

更多
Windows下Nginx与PHP环境配置指南

本文介绍在Windows系统中搭建Nginx与PHP运行环境的完整流程。重点解析PHP版本选择中Thread Safe与非线程安全的区别,以及ISAPI与FastCGI执行模式的差异。详细说明了php.ini关键参数如cgi.fix_pathinfo和fastcgi.impersonate的配置方法,并给出Nginx中location块处理PHP请求的标准代码片段及SCRIPT_FILENAME路径修正注意事项,最后提供通过RunHiddenConsole启动php-cgi服务的命令示例。

2026.03.26 956
Nginx与PHP-FPM架构原理及配置详解

本文解析Nginx调用PHP的运行机制,重点阐述php-fpm作为FastCGI进程管理器监听127.0.0.1:9000端口的原理。通过对比fastcgi.conf与fastcgi_params的差异,说明SCRIPT_FILENAME参数的必要性,并提供包含location匹配、root指定及fastcgi_pass转发的最简vhost配置示例,帮助理解从网络请求到脚本执行的完整流程。

2026.04.14 651
详解Nginx与PHP-FPM协作执行请求的工作原理

本文解析Nginx与PHP配合处理HTTP请求的底层机制。通过梳理CGI、FastCGI及PHP-FPM的关系,说明Master-Worker进程模型如何优化资源加载。结合Nginx配置文件中的location匹配规则,展示请求从DNS解析到最终返回结果的完整流转路径,帮助理解动态内容生成的核心流程。

2026.07.08 582
解决Nginx配置FastCGI路径导致PHP文件无法访问的问题

在Linux环境下使用Nginx和PHP7时,若访问PHP页面出现File not found错误且日志提示Primary script unknown,通常是因为fastcgi_param中SCRIPT_FILENAME的路径前缀配置错误。将配置文件中的硬编码路径(如/scripts)修改为$document_root变量,即可正确解析脚本路径并恢复服务。

2026.03.17 373
优化IIS FastCGI设置提升PHP高并发处理能力

在 IIS 中通过调整 FastCGI 的实例数、请求队列长度和超时参数,可以显著提升 PHP 在高并发场景下的处理能力。

2025.05.29 998
解决php-cgi.exe FastCGI进程请求超时错误

在 IIS 环境下运行 PHP 时,若出现 HTTP 500.0 错误提示“FastCGI 进程超过了配置的请求超时时限”,通常是因为脚本执行时间超出了默认限制。该问题可通过修改 applicationHost.config 中的 activityTimeout、使用 appcmd 命令行调整配置、在 IIS 管理器中更改全局或站点级活动超时值,以及编辑 fcgiext.ini 增加 RequestTimeout 和 ActivityTimeout 参数来解决。

2025.07.07 978

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。