# Actix Web

**Actix Web** 是 [Rust](https://www.edgepedia.org/zh/rust-programming-language) 编程语言的一个 Web 框架，提供 HTTP 服务器、请求路由、提取器和中间件，用于构建 Web 服务与应用。其官方文档自称是一个强大而务实的框架，实质上是一个带点花样的微框架；项目方则宣传，根据 TechEmpower 框架基准测试（TechEmpower Framework Benchmarks），它是现有最快的 Web 框架之一。<sup>[1](https://github.com/actix/actix-web)</sup><sup> • </sup><sup>[2](https://actix.rs/docs/whatis/)</sup>

| 关键事实 | 详情 |
|---|---|
| 语言与运行时 | Rust；运行于稳定版 Rust，在 actix-web 4.13.0 中最低支持版本提升至 1.88<sup>[1](https://github.com/actix/actix-web)</sup><sup> • </sup><sup>[3](https://github.com/actix/actix-web/blob/main/actix-web/CHANGES.md)</sup> |
| 协议 | HTTP/1.x 与 HTTP/2、流式传输与流水线、keep-alive、客户端/服务器 WebSocket<sup>[1](https://github.com/actix/actix-web)</sup> |
| TLS | 使用 OpenSSL 或 Rustls 提供 SSL 支持<sup>[1](https://github.com/actix/actix-web)</sup> |
| 并发模型 | 多线程工作线程，默认每个物理 CPU 一个，各自运行应用的一份独立副本<sup>[4](https://actix.rs/docs/server/)</sup> |
| 当前主版本 | 4.x；4.0.0 版于 2022 年 2 月 25 日发布<sup>[5](https://docs.rs/crate/actix-web/latest)</sup> |
| 与 actor 框架的关系 | 不再由 `actix` actor crate 驱动；actor crate 仅在 WebSocket 端点中需要<sup>[2](https://actix.rs/docs/whatis/)</sup><sup> • </sup><sup>[6](https://rustmagazine.org/issue-3/robjtede/)</sup> |
| 许可证 | 以 Apache License 2.0 与 MIT 双重许可<sup>[1](https://github.com/actix/actix-web)</sup> |

## 提供的能力

Actix Web 提供一个包含在原生可执行文件中的 HTTP 服务器。服务可以置于 nginx 之类的反向代理之后，也可以直接对外提供；即便前面没有另一个 HTTP 服务器，框架也提供 HTTP/1 与 HTTP/2 支持以及 TLS，因此可用于小型生产服务。<sup>[2](https://actix.rs/docs/whatis/)</sup>

功能集包括 HTTP/1.x 与 HTTP/2、流式传输与流水线、带可选宏的请求路由、完全兼容 Tokio、keep-alive 与慢请求处理、客户端与服务器 [WebSocket](https://www.edgepedia.org/zh/websocket) 支持、透明的内容压缩与解压（brotli、gzip、deflate 与 zstd）、multipart 流、静态资源、经由 OpenSSL 或 Rustls 的 SSL，以及 Logger、Session、CORS 等内置中间件。它与 `awc` HTTP 客户端集成。<sup>[1](https://github.com/actix/actix-web)</sup>

一个最小应用将 `HttpServer` 绑定到套接字，由工厂闭包构建 `App`，并注册处理函数；处理函数可以用 `#[get("/hello/{name}")]` 之类的属性宏来编写。<sup>[1](https://github.com/actix/actix-web)</sup>

## 架构与并发

**工作线程与状态。** `HttpServer` 接受一个应用工厂，并启动若干 HTTP 工作线程，默认数量等于物理 CPU 数。每个工作线程获得一个独立的应用实例，因此应用状态不在线程间共享，处理函数可以操作自己那份副本，无需担心并发问题。必须跨工作线程共享的状态通过 `Arc` 和 `web::Data` 提取器传递；文档警告说，为修改而锁定共享状态会带来性能开销。<sup>[4](https://actix.rs/docs/server/)</sup>

由于每个工作线程按顺序处理自己的请求，阻塞线程的处理函数会使该工作线程无法接收新请求；I/O 和数据库调用这类耗时长、不受 CPU 限制的操作应写成异步函数，好让工作线程在它们运行期间处理其他请求。<sup>[4](https://actix.rs/docs/server/)</sup>

**连接处理。** 服务器开始监听后，由两个主循环处理传入连接：接受循环和工作线程循环，二者主要实现在 `actix-server` crate 中，请求循环则实现在 `actix-web` 与 `actix-http` crate 中。<sup>[7](https://actix.rs/docs/conn_lifecycle/)</sup>

**关闭。** 服务器支持优雅关闭：收到停止信号后，工作线程有一段设定时间来完成正在服务的请求，超时后仍存活的一律强制丢弃。默认关闭超时为 30 秒。在 Unix 上，SIGTERM 触发优雅关闭，而 SIGINT 与 SIGQUIT 则强制工作线程关闭。<sup>[4](https://actix.rs/docs/server/)</sup>

## 处理函数、提取器与中间件

处理函数的参数由提取器提供；提取器是实现 `FromRequest`、从请求中取数据的类型。除了路径、查询、表单和 JSON 提取器之外，框架还提供用于应用状态的 `Data`、用于请求本身的 `HttpRequest`，以及用于请求体的 `String`、`Bytes` 和 `Payload`。通过 `web::Data` 访问的应用状态是只读的；可变状态必须显式实现，例如使用跨工作线程共享的原子类型。<sup>[8](https://actix.rs/docs/extractors/)</sup>

中间件系统在请求与响应处理的前后添加行为。中间件可以预处理请求、后处理响应、修改应用状态，并访问 Redis、日志和会话等外部服务。中间件按 `App`、`scope` 或 `Resource` 注册，执行顺序与注册顺序相反。内置中间件包括 `Logger`（使用标准 `log` crate，默认采用 Apache 风格格式）、用于响应头的 `DefaultHeaders`，以及通过 `actix-session` 实现的会话管理；其默认 cookie 后端每个会话存储少于 4000 字节，因为负载必须装进单个 cookie。<sup>[9](https://actix.rs/docs/middleware/)</sup>

## 历史

**Actor 模型的起源。** Actix 项目始于 2017 年，最初是一个 actor 框架，以相互隔离、传递消息的单元作为并发基础，Actix Web 后来构建于其上。<sup>[10](https://dokument.wissen-ahrensburg.de/entwicklung/webentwicklung/evolution-digitaler-rust-webframeworks/)</sup> 2019 年的 Actix Web 2.0 放弃了最初的 actor 模型，改用纯粹的 async/await，以更简单的心智模型保持高性能。<sup>[10](https://dokument.wissen-ahrensburg.de/entwicklung/webentwicklung/evolution-digitaler-rust-webframeworks/)</sup> 框架的核心维护者 Rob Ede（robjtede）解释说，Actix Web 早在 1.0 版之前就不再使用 `actix` actor crate 来驱动其核心 HTTP 服务器——1.0 之前的最后一个版本发布于 2019 年 1 月；这个名字留存下来，成了一个常见的误解。如今 `actix` crate 仍在维护，但仅在 WebSocket 端点中需要。<sup>[6](https://rustmagazine.org/issue-3/robjtede/)</sup><sup> • </sup><sup>[2](https://actix.rs/docs/whatis/)</sup>

**2020 年的 unsafe 争议。** 2018 年 6 月，一个追踪议题指出 actix-web 代码中包含 100 多处 `unsafe` 的使用——`unsafe` 是 Rust 中暂停编译器内存安全保证的关键字——并主张，一个面向公共互联网的 Web 服务器应尽量减少此类代码。<sup>[11](https://github.com/actix/actix-web/issues/289#issuecomment-3)</sup> 2020 年 1 月，在第三方审计发现误用 unsafe 代码可能导致严重漏洞、且社区补丁被拒之后，维护者删除了官方 GitHub 仓库，将代码转移到个人账户，并在此过程中删除了许多议题。<sup>[12](https://deavid.wordpress.com/2020/01/18/actix-web-is-dead-about-unsafe-rust/)</sup> 仓库在数日内恢复，维护者卸任；Actix 继续得到维护。<sup>[12](https://deavid.wordpress.com/2020/01/18/actix-web-is-dead-about-unsafe-rust/)</sup> 原维护者（网名 fafhrd91）退出后，Ede 后来接手了该项目。<sup>[6](https://rustmagazine.org/issue-3/robjtede/)</sup>

**第 4 版。** Actix Web 4.0.0 于 2022 年 2 月 25 日发布，是一次社区驱动的努力，57 位贡献者提交了 600 多次提交，将 actix 各 crate 更新为基于 Tokio 1 的版本，并重做了 `Responder` trait，使其同步返回响应。<sup>[5](https://docs.rs/crate/actix-web/latest)</sup><sup> • </sup><sup>[3](https://github.com/actix/actix-web/blob/main/actix-web/CHANGES.md)</sup> 4.x 系列持续开发：4.13.0 版将最低支持的 Rust 版本提升至 1.88，并通过增大默认流控窗口大小提升了 HTTP/2 上传吞吐量；4.14.0 版增加了原始 cookie 访问器以及 Windows 上的双栈 IPv6 套接字。<sup>[3](https://github.com/actix/actix-web/blob/main/actix-web/CHANGES.md)</sup> 部分功能标记为实验性，带有 `experimental` 前缀，可能在任意版本中变化；当前的例子是 `experimental-introspection`，它提供路由与方法报告辅助功能，用于诊断和工具开发。<sup>[1](https://github.com/actix/actix-web)</sup>

## 性能与基准测试

Actix Web 在 TechEmpower 框架基准测试中一直表现突出；该项目在受控硬件上，对 Web 应用框架进行纯文本、JSON 序列化、数据库及其他类型的测试。<sup>[1](https://github.com/actix/actix-web)</sup><sup> • </sup><sup>[13](https://github.com/andrew-james-dev/techempower-benchmarks)</sup> 在 TechEmpower 第 21 轮（2022 年）中，actix 出现在公布结果的前列；该轮的一份社区分析统计出，前十名最快的框架中有五个是用 Rust 编写的。<sup>[14](https://gist.github.com/hasindusithmin/d05e2c455999e2279d963e35a440acbd)</sup> 一篇回顾该基准测试系列的文章指出，Rust 用 Actix 的成绩来吸引人们使用这门语言，而基准测试的公开赛道为框架作者优化性能创造了强烈动机。<sup>[15](https://send-now.techempower.com/posts/techempower-web-framework-benchmarks.html)</sup>

不过，解读基准测试结果也需要注意一些限制。对 Actix Web 与 Axum 的独立比较通常发现的是因场景而异的小差异，而非决定性的赢家。<sup>[17](https://stackwise.info/compare/actix-vs-axum)</sup>

## 生态与用户

Actix 生态包括若干配套 crate，如用于 HTTP 协议实现的 `actix-http`、用于服务器运行时的 `actix-server`、用于会话的 `actix-session`，以及 `awc` HTTP 客户端。<sup>[3](https://github.com/actix/actix-web/blob/main/actix-web/CHANGES.md)</sup><sup> • </sup><sup>[1](https://github.com/actix/actix-web)</sup> 一个精选示例仓库收集了社区项目，包括 Lemmy——一个联邦化的 Reddit 替代品；MeiliSearch——一个容忍拼写错误的搜索引擎；Mozilla 为 Firefox Suggest 提供的 Merino Web 服务，以及为 Mozilla Tile Service 提供的 Contile 后端；还有 Hyperswitch——一个开源支付交换系统。<sup>[18](https://github.com/actix/examples)</sup> 该仓库还列出了入门项目、面向 MySQL、Postgres、SQLite 和 MongoDB 的数据库集成示例，以及 Luca Palmieri 所著的《Zero to Production in Rust》一书，该书使用 Actix Web 和 SQLx 讲授后端开发。<sup>[18](https://github.com/actix/examples)</sup>

主 actix-web 仓库在 GitHub 上约有 2.49 万星标和 1.9 千次 fork，示例仓库约有 4 千星标。<sup>[1](https://github.com/actix/actix-web)</sup><sup> • </sup><sup>[18](https://github.com/actix/examples)</sup>

## References

1. [actix/actix-web repository README, GitHub](https://github.com/actix/actix-web)
2. [What is Actix Web, Actix Web documentation](https://actix.rs/docs/whatis/)
3. [actix-web CHANGES.md, GitHub](https://github.com/actix/actix-web/blob/main/actix-web/CHANGES.md)
4. [The HTTP Server, Actix Web documentation](https://actix.rs/docs/server/)
5. [actix-web 4.16.0, Docs.rs](https://docs.rs/crate/actix-web/latest)
6. [Interview with @robjtede, Rust Magazine](https://rustmagazine.org/issue-3/robjtede/)
7. [Connection Lifecycle, Actix Web documentation](https://actix.rs/docs/conn_lifecycle/)
8. [Extractors, Actix Web documentation](https://actix.rs/docs/extractors/)
9. [Middleware, Actix Web documentation](https://actix.rs/docs/middleware/)
10. [Evolution und Architekturen digitaler Rust-Webframeworks, Wissen Ahrensburg](https://dokument.wissen-ahrensburg.de/entwicklung/webentwicklung/evolution-digitaler-rust-webframeworks/)
11. [Unsound uses of Unsafe in API, actix-web issue #289, GitHub](https://github.com/actix/actix-web/issues/289#issuecomment-3)
12. [Actix-web is dead (about unsafe Rust), DeavidSedice's blog](https://deavid.wordpress.com/2020/01/18/actix-web-is-dead-about-unsafe-rust/)
13. [TechEmpower Framework Benchmarks, GitHub](https://github.com/andrew-james-dev/techempower-benchmarks)
14. [TechEmpower framework benchmarks round 21 results, GitHub Gist](https://gist.github.com/hasindusithmin/d05e2c455999e2279d963e35a440acbd)
15. [TechEmpower Web Framework Benchmarks: What That Era Still Teaches](https://send-now.techempower.com/posts/techempower-web-framework-benchmarks.html)
16. [Framework benchmarks with context, Better Web](https://frameworkbenchmarks.better-web.org/)
17. [Actix Web vs Axum (2026), Stackwise](https://stackwise.info/compare/actix-vs-axum)
18. [actix/examples repository, GitHub](https://github.com/actix/examples)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Frameworks, libraries, and APIs › Web frameworks and libraries

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/actix-web

English original: https://www.edgepedia.org/actix-web
