我一直在寻找不同的帖子,但到目前为止没有运气。让我来描述一下我想要实现的全部流程。作为输入,有一个ec2实例和两个s3桶。我需要在这个ec2实例上配置nginx代理,以使用户能够与angularjs客户端所在的s3存储进行通信。例如,请求如下:
GET: http://my-ec2-instance.compute.amazonaws.com/first-app/index.html
我希望nginx将它传递给托管在第一个存储桶中的应用程序,并检索响应,就像我直接向
请求一样http://first-bucket.s3.amazonaws.com
我的存储桶政策如下:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AddPerm",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::first-bucket/*"
}
]
}
启用静态网络托管。我甚至可以直接访问我的索引页面,例如:
http://first-bucket.s3-website-us-west-2.amazonaws.com/
此外,我制作了包含html,js和css public的index.html和客户端文件夹。 s3存储中的文件夹层次结构为:
first-app\
|- index.html
|- app\
|- app.js
|- css
|- partials
|- vendor
这是我的nginx.conf中的服务器配置部分:
server {
location /first-app/ {
try_files $uri @s3-first-bucket;
}
location /second-app/ {
try_files $uri @s3-second-bucket;
}
location @s3-first-bucket {
set $s3_bucket 'first-bucket.s3.amazonaws.com';
set $url_full '$1';
proxy_http_version 1.1;
proxy_set_header Host $s3_bucket;
proxy_set_header Authorization '';
proxy_hide_header x-amz-id-2;
proxy_hide_header x-amz-request-id;
proxy_hide_header Set-Cookie;
proxy_ignore_headers "Set-Cookie";
proxy_buffering off;
proxy_intercept_errors on;
resolver 8.8.4.4 8.8.8.8 valid=300s;
resolver_timeout 10s;
proxy_pass http://$s3_bucket/$url_full;
}
location @s3-second-bucket {
set $s3_bucket 'second-bucket.s3.amazonaws.com';
set $url_full '$1';
proxy_http_version 1.1;
proxy_set_header Host $s3_bucket;
proxy_set_header Authorization '';
proxy_hide_header x-amz-id-2;
proxy_hide_header x-amz-request-id;
proxy_hide_header Set-Cookie;
proxy_ignore_headers "Set-Cookie";
proxy_buffering off;
proxy_intercept_errors on;
resolver 8.8.4.4 8.8.8.8 valid=300s;
resolver_timeout 10s;
proxy_pass http://$s3_bucket/$url_full;
}
}