How do I configure nginx to serve static files from an AWS S3 bucket?

Question:

I’m currently deploying a simple flask web application with nginx as my reverse proxy sitting between my WSGI and users. However, I don’t want to store static files(specifically images) on my VPS because storage space will start to get expensive as the number of users go up. I’ve been able to use the AWS S3 boto3 package to upload images to the bucket but I’m not entirely sure how to configure the nginx web server(shown below) to serve the images from my S3 bucket.

server {                                                                        
        listen 80;                                                              
        server_name $IP_ADDR;                                              
                                                                                
        location /static {                                                      
                alias /home/a_mankavil11/flaskBlog/flaskBlog/static;            
        }                                                                       
                                                                                
        location / {                                                            
                proxy_pass http://localhost:8000;                               
                include /etc/nginx/proxy_params;                                
                proxy_redirect off;                                             
        }                                                                       
                                                                                
                                                                                
} 

Bear in mind my experience with server configuration is quite rudimentary.

Asked By: Abe Mankavil

||

Answers:

This works on my server. This one makes my server use images from my S3 bucket, but you can do other static files, just change the remote S3 folder and local location block:

 location /images/ {
proxy_pass https://s3.us-west.amazonaws.com/mybucket/images/;
default_type "image/gif";
   }

In your S3 console, navigate to the object (image or static file) inside the bucket you want to use. Click the object properties and it will give you the Object URL where you can get the path (note I create an images directory in my S3 bucket, it doesn’t have to be called images):

AWS S3 Object URL inside bucket

Answered By: Bman70
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.