HTTP redirected to HTTPS in nginx.conf

I have a nginx.conf in which I am running an application on localhost. I need to redirect the application from HTTP to HTTPS. In the nginx.conf, I have configuration as below:-

http {
  error_log /etc/nginx/error/error.log warn; #./nginx/error.log warn;
  client_max_body_size 20m;

  proxy_cache_path /etc/nginx/cache keys_zone=one:500m max_size=1000m;

  server {
    listen 80;
    server_name localhost; 
    return 301 https://$server_name$request_uri;

  server {
    listen 443 ssl http2;

    server_name localhost; 

    ssl_session_cache shared:SSL:50m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

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

    ssl_protocols TLSv1.2;
    ssl_prefer_server_ciphers on;

    keepalive_timeout 70;

    location / {
      proxy_pass http://localhost:80; 
      proxy_ssl_certificate /etc/nginx/ssl.crt; 
      proxy_ssl_certificate_key /etc/nginx/ssl.key; 
      proxy_ssl_verify    off;
      allow all;
      proxy_redirect          off;
      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-Host $server_name;
      proxy_set_header   X-Forwarded-Proto https;
      #access_log      /var/log/nginx/access.log;
      #error_log       /var/log/nginx/error.log;
      client_max_body_size    0;
      client_body_buffer_size 128k;
      proxy_connect_timeout   1200s;
      proxy_send_timeout      1200s;
      proxy_read_timeout      1200s;
      proxy_buffers           32 4k;


And in the docker-compose.yml, I do have APP_URL=http://localhost:8080 env variable. Does anybody have an idea, what needs to be changed to redirect from HTTP to HTTPS?

Thanks in advance.

Source: Docker Questions