actix-web支持http2,可以将http连接升级到HTTP/2。
1.添加依赖
actix-web = { version = "4.4", features = ["openssl"] }
openssl = { version = "0.10", features = ["v110"] }
2.生成https自签名证书
使用openssl genrsa -out key.pem 2048和openssl req -new -x509 -key key.pem -out cert.pem -days 365在项目根目录生成证书
(base) lvwei@bogon actix-web-app % openssl genrsa -out key.pem 2048
(base) lvwei@bogon actix-web-app % openssl req -new -x509 -key key.pem -out cert.pem -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:BEIJING
Locality Name (eg, city) []:BEIJING
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Personal
Organizational Unit Name (eg, section) []:Personal
Common Name (e.g. server FQDN or YOUR name) []:actix-web
Email Address []:ksnowlv@163.com
3.http server开启http2
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
let http_server = HttpServer::new(|| {
let spec = swagger_ui::swagger_spec_file!("actix_swagger/openapi.json");
let config = swagger_ui::Config::default();
App::new().service(scope("/api/v1/swagger")
.configure(swagger(spec, config)))
.wrap(middleware::Logger::default())
.wrap(logging_middleware::Logging)
.wrap(Compress::default())
.configure(user_routes).route("/{value}", web::get().to(index))
});
// 创建 SSL 加密器
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
builder.set_private_key_file("key.pem", SslFiletype::PEM).unwrap();
builder.set_certificate_chain_file("cert.pem").unwrap();
http_server//.bind("127.0.0.1:8080")?
.bind_openssl("127.0.0.1:8080", builder)?
.run()
.await
4.效果
重启actix-web服务,打开https://127.0.0.1:8080/123验证。
原文始发于微信公众号(ksnowlv):Actix Web开启http2
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/254580.html