Skip to content

Commit 195997f

Browse files
Merge pull request #326 from ByteInternet/update-magento2-s3-obj-storage-doc
update magento2 s3 obj storage doc
2 parents 16a8ef4 + 2f21818 commit 195997f

File tree

2 files changed

+98
-18
lines changed

2 files changed

+98
-18
lines changed

.github/workflows/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:
11-
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
11+
python-version: ['3.8', '3.9', '3.10', '3.11']
1212

1313
steps:
1414
- uses: actions/checkout@v3

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

Lines changed: 97 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,29 +70,109 @@ Magento's S3 implementation creates a test file called `storage.flag`, which is
7070

7171
## Serving assets from your S3 bucket
7272

73-
To start serving media assets from your S3 bucket, you need to make some adjustments to your nginx configuration.
73+
To start serving media assets from your S3 bucket, you need to make some adjustments to your nginx configuration. Create the following file at `/data/web/nginx/example.com/server.assets.conf` for each relevant vhost:
7474

7575
```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";
76+
set $backend "haproxy";
77+
78+
location @object_storage_fallback {
79+
# Proxy to object storage
80+
set $bucket "my_bucket_name";
81+
proxy_cache_key "$bucket$uri";
82+
proxy_cache_valid 200 302 7d;
83+
proxy_cache_methods GET HEAD;
84+
proxy_cache_background_update on;
85+
proxy_cache_use_stale updating;
86+
proxy_cache asset_cache;
87+
resolver 8.8.8.8;
88+
proxy_pass https://$bucket.s3.amazonaws.com$uri;
89+
proxy_pass_request_body off;
90+
proxy_pass_request_headers off;
91+
proxy_intercept_errors on;
92+
proxy_hide_header "x-amz-id-2";
93+
proxy_hide_header "x-amz-request-id";
94+
proxy_hide_header "x-amz-storage-class";
95+
proxy_hide_header "x-amz-server-side-encryption";
96+
proxy_hide_header "Set-Cookie";
97+
proxy_ignore_headers "Set-Cookie";
98+
add_header Cache-Control "public";
99+
add_header X-Frame-Options "SAMEORIGIN";
100+
add_header X-Cache-Status $upstream_cache_status;
101+
expires +1y;
102+
103+
# If object storage fails, fallback to PHP handler
104+
error_page 404 = @asset_fallback;
105+
error_page 403 = @asset_fallback;
106+
}
107+
108+
location @php_asset_fallback {
109+
# Handle with phpfpm
110+
rewrite ^/media /get.php?$args last;
111+
rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
112+
echo_exec @phpfpm;
113+
}
114+
115+
location @haproxy {
116+
# Handle with haproxy
117+
include /etc/nginx/proxy_to_haproxy.conf;
118+
proxy_pass http://127.0.0.1:8080;
119+
}
120+
121+
location @asset_fallback {
122+
try_files "" $asset_fallback_handler;
123+
}
124+
125+
location ~ ^/static/ {
126+
expires max;
127+
128+
# Remove signature of the static files that is used to overcome the browser cache
129+
location ~ ^/static/version\d*/ {
130+
rewrite ^/static/version\d*/(.*)$ /static/$1 last;
131+
}
132+
133+
location ~* \.(ico|jpg|jpeg|png|gif|svg|svgz|webp|avif|avifs|js|css|eot|ttf|otf|woff|woff2|html|json|webmanifest)$ {
134+
add_header Cache-Control "public";
135+
add_header X-Frame-Options "SAMEORIGIN";
136+
expires +1y;
137+
138+
try_files $uri $uri/ @asset_fallback;
139+
}
140+
location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
141+
add_header Cache-Control "no-store";
142+
add_header X-Frame-Options "SAMEORIGIN";
143+
expires off;
144+
145+
try_files $uri $uri/ @asset_fallback;
146+
}
147+
try_files $uri $uri/ @asset_fallback;
148+
add_header X-Frame-Options "SAMEORIGIN";
149+
}
150+
151+
location /media/ {
152+
try_files $uri $uri/ @asset_fallback;
153+
154+
location ~ ^/media/theme_customization/.*\.xml {
155+
deny all;
156+
}
157+
158+
location ~* \.(ico|jpg|jpeg|png|gif|svg|svgz|webp|avif|avifs|js|css|swf|eot|ttf|otf|woff|woff2)$ {
159+
add_header Cache-Control "public";
160+
add_header X-Frame-Options "SAMEORIGIN";
161+
expires +1y;
162+
try_files $uri $uri/ @object_storage_fallback;
163+
}
164+
location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
165+
add_header Cache-Control "no-store";
166+
add_header X-Frame-Options "SAMEORIGIN";
167+
expires off;
168+
try_files $uri $uri/ @object_storage_fallback;
90169
}
91-
# ...
170+
add_header X-Frame-Options "SAMEORIGIN";
92171
}
93172
```
94173

95-
Also make sure your S3 bucket policies are configured correctly, so that only `/media` is publicly readable. For example:
174+
Make sure to change the string `my_bucket_name` to the name of your bucket and keep in mind that your bucket URL might be different depending on your AWS region. For example, you might need to change it from `https://$bucket.s3.amazonaws.com$uri` to `https://s3.amazonaws.com/$bucket$uri` instead.
175+
Furthermore, ensure that your S3 bucket policies are configured correctly, so that only `/media` is publicly readable. For example:
96176

97177
```json
98178
{

0 commit comments

Comments
 (0)