Redirecting http to https #2406
-
Is there someway to listen to both http and https i.e at port 80 and 443? I just want to handle both http and https request. I made it to work for https by going through one of your |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
I guess i got a part of my answer i.e binding to both port 80 and 443. Only redirecting is left, after getting a req that is not https from port 80. Can you help me in that? |
Beta Was this translation helpful? Give feedback.
-
Use use actix_web::{App, HttpRequest, HttpResponse, HttpServer, get};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().service(handler))
.bind("localhost:8080")?
.bind("localhost:8081")?
.run()
.await
}
#[get("/")]
async fn handler(req: HttpRequest) -> HttpResponse {
match req.app_config().local_addr().port() {
8081 => HttpResponse::Ok().body("Hello World from 8081"),
_ => HttpResponse::PermanentRedirect().append_header(("location", "http://localhost:8081")).finish(),
}
} |
Beta Was this translation helpful? Give feedback.
-
Thank you soo much for your response. I still have one more question, i hope you wont mind. What if i want the request to be handled by other services(created in the App instance) if its a https (i.e on port 443). And when its a http req ( i.e on port 80), well for that should i place a middleware at the very first in the App instance using And my question is if its possible to see the request schema and sending a response by changing just the incoming request's LOCATION header with scheme changed to https and status code as value of PERMANENT REDIRECT in the middleware? |
Beta Was this translation helpful? Give feedback.
-
I guess i found my answer in a library code here : https://github.com/petertrotman/actix-web-middleware-redirect-https/blob/master/src/lib.rs |
Beta Was this translation helpful? Give feedback.
-
This isn't updated for 4.0. Where could I find something like this? |
Beta Was this translation helpful? Give feedback.
I guess i found my answer in a library code here :
https://github.com/petertrotman/actix-web-middleware-redirect-https/blob/master/src/lib.rs