How to use nginx to make http jump to https? in secondary subdomain name

main domain baidu.com
subdomain b.baidu.com
you can Http to Https
such as http://baidu.com-> https://baidu.com
http://b.baidu.com-> https://b.baidu.com

how do I configure it?
existing configuration:

server {
        listen 80;
        server_name baidu.com;
        rewrite ^(.*)$ https://$server_name$1 permanent;

}
server {
        listen 80;
        server_name b.baidu.com;
        rewrite ^(.*)$ https://$server_name$1 permanent;

}

in this way, the configuration subdomain name cannot be redirected

Mar.07,2021

server {
    listen 80;
    server_name baidu.com;
    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    }
}
server {
    listen 80;
    server_name b.baidu.com;
    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    }
}
Menu