You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Before using this package, you need to configure AWS CloudFront with signed cookies. This guide assumes you already have an S3 bucket with your assets and a domain name hosted on the internet.
**Important**: Store the `cloudfront-private.key` file in your Laravel application's `storage` directory. This is the default location the package looks for the private key.
173
+
174
+
### 2. Create a CloudFront Key Group
175
+
176
+
1. Go to the AWS CloudFront console
177
+
2. Navigate to **Key management** > **Public keys**
178
+
3. Click **Create public key**
179
+
4. Give it a name (e.g., "My App Public Key")
180
+
5. Paste the content of `cloudfront-public.key`
181
+
6. Save and note down the **Key ID** (you'll need this for `CLOUDFRONT_KEY_PAIR_ID`)
182
+
7. Navigate to **Key groups** and create a new key group
183
+
8. Add the public key you just created to this key group
184
+
185
+
### 3. Create a CloudFront Distribution
186
+
187
+
**Important**: Your CloudFront distribution must use the same root domain as your application. For example, if your application is at `example.com`, your CloudFront domain should be something like `assets.example.com` or `cdn.example.com`. This is necessary because cookies can only be set for domains you own.
188
+
189
+
1. Go to the CloudFront console and click **Create distribution**
190
+
2. Under **Origin domain**, select your S3 bucket with the assets
191
+
3. Under **Default cache behavior**:
192
+
- Set **Restrict viewer access** to **Yes**
193
+
- Select the key group you created earlier
194
+
4. Under **Settings**:
195
+
- Add your custom SSL certificate (e.g., `*.example.com`)
196
+
- Under **Alternate domain name (CNAME)**, add your CloudFront domain (e.g., `cdn.example.com`)
197
+
5. Create the distribution (this may take 10-15 minutes to deploy)
198
+
6. Note down the **Distribution domain name** (e.g., `d1234abcd.cloudfront.net`) and the full CloudFront URL (e.g., `https://d1234abcd.cloudfront.net/*`)
199
+
200
+
### 4. Configure Route 53 DNS
201
+
202
+
1. Go to Route 53 and select your hosted zone
203
+
2. Click **Create record**
204
+
3. Set the record name to match your CloudFront CNAME (e.g., `cdn`)
205
+
4. Enable the **Alias** toggle
206
+
5. Select **CloudFront distribution** as the alias target
207
+
6. Select your distribution from the dropdown
208
+
7. Create the record
209
+
210
+
If you don't see your distribution in the dropdown, you can use a CNAME record type instead and use the CloudFront domain name as the value.
211
+
212
+
### 5. Update S3 Bucket CORS Policy
213
+
214
+
1. Go to your S3 bucket
215
+
2. Navigate to the **Permissions** tab
216
+
3. Scroll to the **Cross-origin resource sharing (CORS)** section
217
+
4. Update the policy (replace `example.com` and `cdn.example.com` with your domains):
218
+
219
+
```json
220
+
[
221
+
{
222
+
"AllowedHeaders": ["*"],
223
+
"AllowedMethods": ["GET", "HEAD"],
224
+
"AllowedOrigins": [
225
+
"https://example.com",
226
+
"https://cdn.example.com"
227
+
],
228
+
"ExposeHeaders": ["ETag"]
229
+
}
230
+
]
231
+
```
232
+
160
233
## Usage
161
234
162
235
Add the `SignCloudfrontCookies` middleware to your routes or route groups:
0 commit comments