mst
mst

Reputation: 466

how to implement nginx conf in aws cloudfront?

i have some static files, which is served from nginx.

And i have nginx conf that direct

http//xx.yy.com/style.css => /web/style/xx.css

Now i want to use AWS Cloudfront to server this static css/js files.

How can in do this in Cloudfront?

At the end of day, i want to be able to dynamically direct request to different files or folders according to subdomain.

for example :

http//xx.yy.com/style.css => /web/style/xx.css

http//zz.yy.com/style.css => /web/style/zz.css

http//xx.yy.com/api.js => /web/api/xx.js

http//zz.yy.com/api.js => /web/api/zz.js

Upvotes: 0

Views: 1571

Answers (1)

alexjs
alexjs

Reputation: 573

CloudFront 'origin' decisions are largely based on two things - URL and path.

Within a single CloudFront distribution you can have multiple 'behaviour' rules. Each rule can have its own origin, so you could say:

For request path: /foo/ Use origin: http://foo.origin.com/

Each distribution can have multiple 'alternate domain names', but you cannot say, within a single distribution, 'for this hostname, use this origin', that can only be specified on a path basis.

At the end of day, i want to be able to dynamically direct request to different files or folders according to subdomain.

http//xx.yy.com/style.css => /web/style/xx.css

http//zz.yy.com/style.css => /web/style/zz.css

Nonetheless, you do have an option here. CloudFront can be configured to whitelist headers, including the Host header. Whitelisted headers are used as part of the cache key variant. Subsequently if you configure:

  • Configure 1 Cloudfront Distribution
  • Set 2 Alternative Domain Names (xx.yy.com and zz.yy.com)
  • Whitelist 'Host' as header to be sent to origin
  • On the origin side, configure name based virtual hosting with the appropriate rewrites (e.g. if xx.yy.com/style.css, then serve xx.css)

This should result in a working configuration for what you've described, although does involve more logic in the Nginx layer than would be preferable.

Please note that if you wanted xx.yy.com and zz.yy.com to go to different origin (HTTP servers) for the same directory, you'd need to use different CloudFront distributions. Furthermore, CloudFront doesn't allow you rewrite file parts of URLS (e.g. /foo.css -> origin/bar.css), so doing it on a specific file basis would be tricky.

Upvotes: 1

Related Questions