M
M
MalikDeveloper20772020-08-05 17:05:35
Django
MalikDeveloper2077, 2020-08-05 17:05:35

Why doesn't Django + AWS & Nginx work in production?

On localhost, my AWS setup works and all media files are stored and downloaded as well as taken from the S3 bucket.

settings.py

AWS_ACCESS_KEY_ID = 'some_key'  
AWS_SECRET_ACCESS_KEY = 'F'  
AWS_STORAGE_BUCKET_NAME = 'my_bucket'  
AWS_S3_SIGNATURE_VERSION = 's3v4'  

AWS_S3_FILE_OVERWRITE = False  
AWS_DEFAULT_ACL = None  
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'


It all works and the image link looks like this:
CAUgk.png

Next, I deployed the project on the Nginx server
And after that the link to the image looks like this:
5vUiY.png

That is, here the image is not taken from Amazon, but from the local media folder (it is not there and, accordingly, the image is not shown) :
K8VUb.png

Nginx config:
server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;

        server_name _;

        /* HERE THE PATH TO MY AWS BUCKET
        ================================*/
        location /media  {
                proxy_pass https://diasmart-media-static.s3.amazonaws.com/;  // diasmart-media-static это bucket name
        }
        /* ============END============= */

        location /static {
                alias /root/DiaStore/src/assets;
        }

        location / {
                proxy_pass http://127.0.0.1:8001;
                proxy_set_header X-Forwarded-Host $server_name;
                proxy_set_header X-Real-IP $remote_addr;
                add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
                add_header Access-Control-Allow-Origin *;
        }
}


I don't know what's wrong, please help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim, 2020-08-05
@Viji

Hello, is NGINX running on EC2? If so, then create a role in Amazon IAM for EC2 instance with read permission on this bucket. After that, when you access the nginx server via ssh, you must execute the curl command and the picture should be downloaded without any AWS_ACCESS_KEY.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question