我试图从亚马逊 S3 存储桶提供静态和媒体文件,但 nginx 无法连接到它
这是我得到的错误
<Error>
<Code>AccessDenied</Code>
<Message>
AWS authentication requires a valid Date or x-amz-date header
</Message>
<RequestId></RequestId>
<HostId></HostId>
</Error>
Run Code Online (Sandbox Code Playgroud)
和我的 Nginx 配置
server {
listen 80;
server_name my_elastic_ip;
location = /favicon.ico { access_log off; log_not_found off; }
location / {
try_files $uri @s3;
}
location @s3 {
set $s3_bucket 'my_bucket.s3.amazonaws.com';
set $url_full '$1';
set $aws_access_key 'my_access_key';
set $aws_secret_key 'my_secret_key';
proxy_http_version 1.1;
proxy_set_header Host $s3_bucket;
proxy_set_header x-amz-date $date_gmt;
proxy_set_header Authorization 'AWS $aws_access_key:$aws_secret_key';
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;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑1:
我已经解决了替换 x-amz-date 的问题:
set_by_lua $now "return ngx.cookie_time(ngx.time())";
proxy_set_header x-amz-date $now;
Run Code Online (Sandbox Code Playgroud)
您需要额外的 nginx 软件包,请使用以下命令安装它:
sudo apt-get install nginx-extras
Run Code Online (Sandbox Code Playgroud)
现在我收到这个错误:
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you provided. Check your key and signing method.
</Message>
Run Code Online (Sandbox Code Playgroud)
编辑2:
要创建签名,我已将 set-misc-nginx-module ( https://github.com/openresty/set-misc-nginx-module#installation ) 添加到 nginx (安装 nginx 可选模块)
然后将我的 nginx 配置更新为:
server {
listen 80;
server_name my_ip;
location = /favicon.ico { access_log off; log_not_found off; }
location / {
try_files $uri @s3;
}
location @s3 {
set $s3_bucket 'my_bucket';
set $key 'my_file';
set $aws_access_key 'my_access_key';
set $aws_secret_key 'my_secret_key';
set_by_lua $now "return ngx.cookie_time(ngx.time())";
set $aws_signature '';
set $string_to_sign "$request_method\n\n\n\nx-amz-date:$now\n/$s3_bucket/$key";
set_hmac_sha1 $aws_signature $aws_secret_key $string_to_sign;
set_encode_base64 $aws_signature $aws_signature;
proxy_http_version 1.1;
proxy_set_header x-amz-date $now;
proxy_set_header Authorization 'AWS $aws_access_key:$aws_signature';
proxy_set_header Host $s3_bucket.s3.amazonaws.com;
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.amazonaws.com;
}
Run Code Online (Sandbox Code Playgroud)
}
收到此错误:
状态:HTTP/1.1 403 禁止
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
Run Code Online (Sandbox Code Playgroud)
请考虑暂时将proxy_pass后端服务设置为本地服务或 HTTP 回显服务,以便您可以查看发送到 Amazon 的完整 HTTP 请求。(如果您使用 HTTP echo Web 服务,请首先从请求中删除敏感位!)。
然后你直接调试亚马逊请求有什么问题。一旦弄清楚了这一点,您就可以对 Nginx 进行适当的更改,以便它为您发送有效的请求标头。
| 归档时间: |
|
| 查看次数: |
4154 次 |
| 最近记录: |