简体   繁体   English

如何为 CloudFront 行为指定多个路径模式?

[英]How to specify multiple path patterns for a CloudFront Behavior?

I have a CloudFront distribution with an s3 origin and a custom origin.我有一个带有 s3 源和自定义源的 CloudFront 分配。 I would like all traffic on /api/* and /admin/* to go to the custom origin, and all other traffic to go to the s3 origin.我希望/api/*/admin/*上的所有流量到 go 到自定义源,以及到 go 的所有其他流量到 s3 源。 Currently I have it working with only /api/* :目前我只使用/api/*

                cloudfront.SourceConfiguration(
                    custom_origin_source=cloudfront.CustomOriginConfig(
                        domain_name=alb,
                        origin_protocol_policy=cloudfront.OriginProtocolPolicy.MATCH_VIEWER,
                    ),
                    behaviors=[
                        cloudfront.Behavior(
                            allowed_methods=cloudfront.CloudFrontAllowedMethods.ALL,
                            path_pattern="/api/*",
                            forwarded_values={
                                "headers": ["*"],
                                "cookies": {"forward": "all"},
                                "query_string": True,
                            },
                        )
                    ],
                ),

I could probably repeat the behavior with /api/* , but I will eventually have some additional paths to add that will need to be routed to the custom origin (ALB), so I'm wondering if there is a way to do this that is more DRY.我可能会重复使用/api/*的行为,但我最终会添加一些额外的路径,这些路径需要路由到自定义源 (ALB),所以我想知道是否有办法做到这一点更干燥。

Does path_pattern accept /{api,admin,other}/* style patterns? path_pattern是否接受/{api,admin,other}/*样式模式? Or should I refactor the Behaviors section to reuse allowed_methods and forwarded_values and then repeat multiple behaviors with a different path_pattern ?或者我应该重构 Behaviors 部分以重用allowed_methodsforwarded_values ,然后使用不同的path_pattern重复多个行为?

Does path_pattern accept /{api,admin,other}/* style patterns? path_pattern 是否接受/{api,admin,other}/*样式模式?

No, this pattern style is not supported based on the documentation .不,根据文档不支持这种模式样式。

should I refactor?我应该重构吗?

Yes, you can simply save all the path_pattern corresponding to this custom origin into a list, say path_patterns .是的,您可以简单地将与此自定义原点对应的所有path_pattern保存到一个列表中,例如path_patterns Then use a simple handy Python list comprehension,然后使用一个简单好用的 Python 列表推导,

behaviors=[
    cloudfront.Behavior(
        allowed_methods=cloudfront.CloudFrontAllowedMethods.ALL,
        path_pattern=pp,
        forwarded_values={
            "headers": ["*"],
            "cookies": {"forward": "all"},
            "query_string": True,
        },
    ) for pp in path_patterns
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM