从amazom S3存储检索静态文件静态、文件、amazom

2023-09-11 09:14:02 作者:*`深湖觅鲸

我想我的配置nginx的以这样的方式,每当有一些不好的网关回应,我试着从S3存储读取静态的HTML内容。

I am trying to configure my nginx in such a way that whenever there is some bad gateway response, I try to fetch static html contents from the s3 bucket.

请求的URL结构some_bucket /文件夹1 / FOLDER2 /文

The url structure of the request is some_bucket/folder1/folder2/text

和数据存储在S3存储桶与目录结构s3.amazonaws.com/some_bucket/folder1/folder2/folder1_folder2.html

And the data is stored in s3 bucket with directory structure as s3.amazonaws.com/some_bucket/folder1/folder2/folder1_folder2.html

我不能够确定的值,文件夹1和FOLDER2,这样我可以做 html文件动态和使用proxy_pass。 另外,我想try_files,但我认为这并不URL的工作。

I am not able to determine the values for folder1 and folder2 so that I can make the html file dynamically and use proxy_pass. Also, I tried try_files but I think that does not work for urls.

不知道如何解决这个问题。

Any idea how to tackle this problem.

感谢。

推荐答案

这是我做过什么的人(可能是新手),谁可能会遇到这个问题。

This is what I did for someone(probably newbie) who may encounter this problem.

 location ~* ^/some_bucket/(.*)/(.*)/.* {
      proxy_pass http://s3.amazonaws.com/some_bucket/$1/$2/$1_$2.html;
 }

〜*表示不区分大小写的正则表达式匹配 ^意味着任何事情之前 ()用于捕捉参数。

~* means case insensitive regex match ^ means anything before () for catching parameters.

例如, 用户进入www.example.com/some_bucket/folder1/folder2/text~~V

For example, User enters www.example.com/some_bucket/folder1/folder2/text

然后,将其处理为,

〜*确保不区分大小写(为区分大小写跳过*(指只是把〜))

~* ensures case insensitive search(for case sensitive skip *(means just put ~))

^匹配www.example.com。

^ matches www.example.com.

/ some_bucket /匹配的话,

/some_bucket/ is matched then,

。*表示任何数量的任意字符(任何数值,更换[0-9] *)

.* means any number of any character(for any numeric, replace with [0-9]*)

()保证匹配的值被逮住

() ensures that matched values gets catched

所以,$ 1捕获FOLDER1

So, $1 catches folder1

$ 2捕获FOLDER2

$2 catches folder2

然后

。*没有括号匹配任何系统字符,但没有捕获匹配的值

.* without parenthesis matches any charater but does not catch the matched value

现在的逮住值可用于通过找到亚马逊桶文件

Now the catched values can be used to find the file in amazon bucket using

proxy_pass http://s3.amazonaws.com/some_bucket/ $ 1 / $ 2 / $ 1_ $ 2.HTML

proxy_pass http://s3.amazonaws.com/some_bucket/$1/$2/$1_$2.html

https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms可以帮助