Skip to content

Commit 34eb1cb

Browse files
dominik-matichrithikkothari1234
authored andcommitted
update docs
1 parent 29eb449 commit 34eb1cb

File tree

1 file changed

+95
-33
lines changed

1 file changed

+95
-33
lines changed

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

Lines changed: 95 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -70,47 +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.
74-
75-
We recommend that you create a configuration file `/data/web/nginx/http.asset_proxy_cache.conf` defining the cache storage location, structure, size constraints, and cache expiration policies.
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:
7674

7775
```nginx
78-
proxy_cache_path /data/var/nginx-asset-cache levels=1:2 keys_zone=asset_cache:10m max_size=1g inactive=1w;
79-
```
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+
}
80107
81-
Then update your nginx configuration in the following manner.
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+
}
82114
83-
```nginx
84-
location /media {
85-
# ...
86-
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
87-
resolver 8.8.8.8;
88-
set $bucket "<my_bucket_name>";
89-
proxy_pass https://$bucket.s3.amazonaws.com$uri;
90-
proxy_pass_request_body off;
91-
proxy_pass_request_headers off;
92-
proxy_intercept_errors on;
93-
proxy_hide_header "x-amz-id-2";
94-
proxy_hide_header "x-amz-request-id";
95-
proxy_hide_header "x-amz-storage-class";
96-
proxy_hide_header "x-amz-server-side-encryption";
97-
proxy_hide_header "Set-Cookie";
98-
proxy_ignore_headers "Set-Cookie";
99-
100-
# include the following if you defined proxy_cache_path previously
101-
proxy_cache_key "$bucket$uri";
102-
proxy_cache_valid 200 302 7d;
103-
proxy_cache_methods GET HEAD;
104-
proxy_cache_background_update on;
105-
proxy_cache_use_stale updating;
106-
proxy_cache asset_cache;
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;
107169
}
108-
# ...
170+
add_header X-Frame-Options "SAMEORIGIN";
109171
}
110172
```
111173

112-
Keep in mind your bucket URL might be different depending on your AWS region. For example, you might need to change it to `https://s3.amazonaws.com/$bucket$uri` instead.
113-
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:
114176

115177
```json
116178
{

0 commit comments

Comments
 (0)