Skip to content

Commit 6834703

Browse files
committed
docs/magento2: Add serving assets example
1 parent 2bddc86 commit 6834703

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

docs/ecommerce-applications/magento-2/how-to-configure-remote-storage-for-magento-2-x.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ This can be useful for many reasons, such as:
2121
## Configuring the application
2222

2323
Configuring Magento 2 to start storing files in your bucket is done using a single command.
24-
If you're using a different provider than AWS S3, you need to specify the `--remote-storage-endpoint` option.
2524

2625
**AWS S3**
2726

@@ -36,11 +35,13 @@ bin/magento setup:config:set \
3635

3736
**Other S3 compatible providers**
3837

38+
If you're using a different provider than AWS S3, you need to specify the `--remote-storage-endpoint` option.
39+
3940
```bash
4041
bin/magento setup:config:set \
4142
--remote-storage-driver="aws-s3" \
4243
--remote-storage-bucket="my_bucket_name" \
43-
--remote-storage-region="my-aws-region" \
44+
--remote-storage-region="provider-region" \
4445
--remote-storage-key="abcd1234" \
4546
--remote-storage-secret="abcd1234" \
4647
--remote-storage-endpoint="https://my-s3-compatible.endpoint.com"
@@ -67,6 +68,47 @@ This is much faster than Magento's built-in sync, because `aws s3 sync` uploads
6768

6869
Magento's S3 implementation creates a test file called `storage.flag`, which is basically created to test if the connection works. So this is not a magic file to mark anything ([source](https://github.com/magento/magento2/blob/6f4805f82bb7511f72935daa493d48ebda3d9039/app/code/Magento/AwsS3/Driver/AwsS3.php#L104)).
6970

71+
## Serving assets from your S3 bucket
72+
73+
To start serving media assets from your S3 bucket, you need to make some adjustments to your nginx configuration.
74+
75+
```nginx
76+
location /media {
77+
# ...
78+
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
79+
resolver 8.8.8.8;
80+
set $bucket "<my_bucket_name>";
81+
proxy_pass https://s3.amazonaws.com/$bucket$uri;
82+
proxy_pass_request_body off;
83+
proxy_pass_request_headers off;
84+
proxy_intercept_errors on;
85+
proxy_hide_header "x-amz-id-2";
86+
proxy_hide_header "x-amz-request-id";
87+
proxy_hide_header "x-amz-storage-class";
88+
proxy_hide_header "Set-Cookie";
89+
proxy_ignore_headers "Set-Cookie";
90+
}
91+
# ...
92+
}
93+
```
94+
95+
Also make sure your S3 bucket policies are configured correctly, so that only `/media` is publicly readable. For example:
96+
97+
```json
98+
{
99+
"Version": "2012-10-17",
100+
"Statement": [
101+
{
102+
"Sid": "AllowPublicReadOnlyForMedia",
103+
"Effect": "Allow",
104+
"Principal": "*",
105+
"Action": "s3:GetObject",
106+
"Resource": "arn:aws:s3:::<your-bucket-name>/media/*"
107+
}
108+
]
109+
}
110+
```
111+
70112
## Magento remote storage documentation
71113

72114
- [Configure Remote Storage](https://experienceleague.adobe.com/en/docs/commerce-operations/configuration-guide/storage/remote-storage/remote-storage)

0 commit comments

Comments
 (0)