Skip to main content

HttpServer

Configuring the network layer.

Actix 4.9+ 5 min read

# HttpServer Configuration

The HttpServer type is responsible for serving HTTP requests. It acts as the TCP listener, accepting connections, and dispatching them to the worker threads which run your App logic.

##Worker Threads

By default, HttpServer starts a number of worker threads equal to the number of logical CPUs on the system. This provides optimal performance for most workloads because it maximizes CPU utilization without overwhelming the OS scheduler.

You can override this using the .workers() method:

code_blocksrust
HttpServer::new(|| App::new())
    .workers(4) // Specifically use 4 workers
    .bind("127.0.0.1:8080")?
    .run()
    .await

Reducing the number of workers is useful in resource-constrained environments (like small Docker containers) or when your application relies heavily on external services and is fully async, thus not needing a lot of CPU.

##Connection Backlog and Keep-Alive

Actix Web allows you to tweak low-level networking parameters:

  • Keep-Alive: By default, Actix enables HTTP keep-alive. You can configure the duration or disable it via .keep_alive().
  • Client Timeout: You can configure how long the server should wait for a client to send a full request via .client_request_timeout().
  • Max Connections: Prevent DDoS attacks by limiting concurrent connections using .max_connections().
code_blocksrust
use std::time::Duration;
use actix_web::http::KeepAlive;

HttpServer::new(|| App::new())
    .keep_alive(KeepAlive::Timeout(Duration::from_secs(75)))
    .client_request_timeout(Duration::from_secs(5))
    .bind("127.0.0.1:8080")?
    .run()
    .await

##TLS / HTTPS

While it is common to deploy Actix Web behind a reverse proxy (like Nginx, HAProxy, or Traefik) that handles SSL termination, you can run Actix Web directly with TLS using libraries like rustls or openssl.

Using rustls:

code_blocksrust
// Note: Requires the 'rustls' feature enabled on actix-web
// .bind_rustls("127.0.0.1:8443", rustls_config)

The HttpServer is highly configurable, ensuring that Actix Web can be tuned for almost any deployment scenario.

Was this page helpful?

Reference Implementation

The following code snippet demonstrates the authoritative pattern for this documentation.

src/main.rs
1use actix_web::{App, HttpServer, Responder, get};
2
3#[get("/")]
4async fn index() -> impl Responder {
5 "Server optimized!"
6}
7
8#[actix_web::main]
9async fn main() -> std::io::Result<()> {
10 HttpServer::new(|| App::new().service(index))
11 .workers(2)
12 .max_connections(10_000)
13 .bind("127.0.0.1:8080")?
14 .run()
15 .await
16}
Was this page helpful?