Redirect http to https in nginx

How to redirect http traffic to https in nginx configuration.

server {
    listen  80;
    server_name domain.name;
    return 301 https://$server_name$request_uri;
}

server {
    listen   443 ssl;
    ssl_certificate  domain.name.crt;
    ssl_certificate_key  domain.name.key;
    server_name  domain.name;
    location / {
        ...
    }
}

References

nginx