How to configure Nginx to serve statics from Amazon S3

Recently I needed to configure Nginx to serve statics from Amazon S3 and this is how I did it.

Configuring Nginx to serve statics from Amazon S3

Edit the Nginx configuration file and add a server block (ie in your nginx.conf)

server { 
  listen 80; 
  server_name static.yourdomain.com; # Edit your domain and subdomain   

  location / { 
    proxy_set_header Host 'bucket-name.s3.amazonaws.com'; 
    proxy_set_header Authorization ''; 
    proxy_hide_header x-amz-id-2; 
    proxy_hide_header x-amz-request-id; 
    proxy_hide_header Set-Cookie; 
    proxy_ignore_headers "Set-Cookie"; 
    proxy_intercept_errors on; 
    proxy_pass https://bucket-name.s3.amazonaws.com/; # Edit your Amazon S3 Bucket name 
    expires 1y; 
    log_not_found off; 
  } 
}

Note the expiration parameter. In this case it was 1y (1 year), but you can set whatever you need, probably less than that.