静的コンテンツ、例えば画像をS3に配置します。その画像のパスをDBに保存するときにドメインを保存しなくて、URIだけ保存しつつ表示させる方法です。
- Nginx(www.exampl.net) -> CloudFront -> S3(s3.hoge.com)
もくじ
Nginx
$ sudo vi /etc/nginx/sites-enabled/homestead.test server { listen 80; listen 443 ssl http2; server_name example.net www.example.net; root "/home/vagrant/code/public"; index index.html index.htm index.php; charset utf-8; client_header_buffer_size 64k; large_client_header_buffers 4 64k; location / { try_files $uri $uri/ /index.php?$query_string; } ・・・ ●これを追加 location /images { try_files $uri @s3; } ・・・ location @s3 { return 301 https://s3.hoge.com$request_uri; } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } access_log /var/log/nginx/homestead.test.log; error_log /var/log/nginx/homestead.test-error.log error; sendfile off; client_max_body_size 200M; location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors off; fastcgi_buffer_size 16k; fastcgi_buffers 4 16k; fastcgi_connect_timeout 3000; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_param HOSTNAME $client; } location ~ /\.ht { deny all; } location /phpmyadmin { root /usr/share; index index.php; location ~ ^/phpmyadmin.+\.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } } ssl_certificate /etc/nginx/ssl/homestead.test.crt; ssl_certificate_key /etc/nginx/ssl/homestead.test.key; }
CloudFront
Behaviorで
- Path Pattern
/images/* - Origin or Origin Group
S3のオリジンを指定
他はデフォルトですが🐱 …
- Viewer Protocol Policy
HTTP and HTTPS - Allowed HTTP Methods
GET, HEAD - Cached and origin request settings
Use legacy cache setting - Cache Based Selected Request Headers
None(improves Caching) - Object Caching
Use Origin Cache Headers - Minimum TTL
0 - Maximum TTL
31536000 - Default TTL
86400 - Forward Cookies
None(improves Caching) - Query String Forwarding and Caching
None(improves Caching) - Smooth Streaming
No - Restrict Viewer Access
No - Compress Objects Automatically
No
S3
s3.hoge.comのバケットにimagesバケットを作ってください