Skip to main content

actix-web automatically upgrades connections to HTTP/2 if possible.

Negotiation

When either of the rustls or openssl features are enabled, HttpServer provides the bind_rustls() method and bind_openssl() methods, respectively.

[dependencies]
actix-web = { version = "4", features = ["rustls-0_23"] }
rustls = "0.23"
rustls-pemfile = "2"
use actix_web::{web, App, HttpRequest, HttpServer, Responder};

async fn index(_req: HttpRequest) -> impl Responder {
"Hello TLS World!"
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
.unwrap();

let mut certs_file = BufReader::new(File::open("cert.pem").unwrap());
let mut key_file = BufReader::new(File::open("key.pem").unwrap());

// load TLS certs and key
// to create a self-signed temporary cert for testing:
// `openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365 -subj '/CN=localhost'`
let tls_certs = rustls_pemfile::certs(&mut certs_file)
.collect::<Result<Vec<_>, _>>()
.unwrap();
let tls_key = rustls_pemfile::pkcs8_private_keys(&mut key_file)
.next()
.unwrap()
.unwrap();

// set up TLS config options
let tls_config = rustls::ServerConfig::builder()
.with_no_client_auth()
.with_single_cert(tls_certs, rustls::pki_types::PrivateKeyDer::Pkcs8(tls_key))
.unwrap();

HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind_rustls_0_23(("127.0.0.1", 8443), tls_config)?
.run()
.await
}

Upgrades to HTTP/2 described in RFC 7540 §3.2 are not supported. Starting HTTP/2 with prior knowledge is supported for both cleartext and TLS connections (RFC 7540 §3.4) (when using the lower level actix-http service builders).

Check out the TLS examples for concrete example.