Nginx config file for grafana

Hi,
I created an nginx file in the Docker environment using docker-compose. I am trying to achieve two things:

  1. When the user uses an HTTPS link , it should display the Grafana page login.
  2. When the user uses an HTTP link I would like to redirect to https , then it should display the Grafana page login.

I have successfully addressed points 1 . However, I’m struggling with the implementation of the HTTP-to-HTTPS redirect. When I enter the URL with ‘http,’ it doesn’t redirect, but without ‘http,’ the URL successfully redirects. How can I resolve this issue? (adding the nginx file)

Thanks in advance.

events {
    # Essential configuration for the "events" section
}

http {
    server {
        listen 80;
        server_name localhost;

        location /grafana/ {
            proxy_pass http://localhost:3000/;
            #include /etc/nginx/proxy_params; # Include any additional proxy parameters here

            # WebSocket settings
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_read_timeout 86400;
            proxy_redirect http://localhost:3000/ https://localhost/grafana;
        }

        # Add any additional secure headers here if needed
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
        add_header X-Content-Type-Options nosniff;
        add_header X-Frame-Options SAMEORIGIN;

        # Error log configuration
        error_log /var/log/nginx/error.log;
    }

    server {
        listen 443 ssl;
        server_name localhost;

        ssl_certificate /etc/nginx/ssl/certnew.crt;
        ssl_certificate_key /etc/nginx/ssl/certk.key;

        location /grafana/ {
            proxy_pass http://grafana:3000/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;

            # WebSocket settings
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
}