【问题标题】:Restricting bandwidth with Nginx using Amazon S3使用 Amazon S3 限制 Nginx 的带宽
【发布时间】:2016-11-02 14:46:13
【问题描述】:

我在 Amazon S3 上托管了大型下载文件(有些大于 5 GB)。我的主服务器是 Nginx。 Amazon S3 没有公共访问权限。文件通过签名 URL 提供。

在使用 Amazon S3 时有没有办法限制带宽?我知道 Amazon S3 上没有选项,但我们可以使用 Nginx 作为代理并从那里制作它吗?

我正在尝试使用该链接中的示例:

https://coderwall.com/p/rlguog/nginx-as-proxy-for-amazon-s3-public-private-files

这个代码块:

location ~* ^/proxy_private_file/(.*) {
  set $s3_bucket        'your_bucket.s3.amazonaws.com';
  set $aws_access_key   'AWSAccessKeyId=YOUR_ONLY_ACCESS_KEY';
  set $url_expires      'Expires=$arg_e';
  set $url_signature    'Signature=$arg_st';
  set $url_full         '$1$aws_access_key&$url_expires&$url_signature';

  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               172.16.0.23 valid=300s;
  resolver_timeout       10s;

  proxy_pass             http://$s3_bucket$url_full;  

}

我不明白的是如何将创建的签名 URL 从 PHP 传递到 Nginx 配置?所以我可以告诉 Nginx 去那个签名的 URL 作为代理。

【问题讨论】:

  • 似乎可行——也许是proxy_limit_rate?您真的要限制“带宽”,还是每个客户端每秒的请求数,还是其他?
  • 我的问题是;我正在使用一个插件为 Amazon S3 创建签名 URL。该插件使用 PHP 中的类似内容将用户重定向到签名 URL:header('Location: ' . $signedURL); 在这种情况下我可以在哪里放置 Nginx?
  • @Michael-sqlbot 我对服务器不是很好,所以请多多包涵。用户请求 example.com/files/file1.txt PHP,生成签名 URL。像 amazon/bucket/file1.txt 然后使用 PHP 的标头提供服务。我需要在 Nginx conf 中获取 amazon/bucket/file.txt URL 并在那里发出代理请求。我该怎么做?
  • 简而言之:PHP 创建一个 URL,我想将其用作 Nginx 的代理地址。

标签: nginx amazon-s3 bandwidth


【解决方案1】:

我找到了解决方案。这里是:

首先在你的 nginx 配置中打开你的 http 块。我们将创建限制每个 IP 连接所需的区域。

limit_conn_zone $binary_remote_addr zone=addr:10m;

现在在 /etc/nginx/conf.d/sitename.conf 或您定义的任何地方打开您的 服务器块。创建内部位置。我们会将 PHP 请求重定向到这里:

location ~* ^/internal_redirect/(.*?)/(.*) {
# Do not allow people to mess with this location directly
# Only internal redirects are allowed
internal;

# Location-specific logging, so we can clearly see which requests
# passing through proxy and what is happening there
access_log /var/log/nginx/internal_redirect.access.log main;
error_log /var/log/nginx/internal_redirect.error.log warn;

# Extract download url from the request
set $download_uri $2;
set $download_host $1;

# Extract the arguments from request.
# That is the Signed URL part that you require to get the file from S3 servers
if ($download_uri ~* "([^/]*$)" ) {
    set  $filename  $1;
}

# Compose download url
set $download_url $download_host/$download_uri?$args;

# Set download request headers
proxy_http_version      1.1;
proxy_set_header        Connection "";
proxy_hide_header       x-amz-id-2;
proxy_hide_header       x-amz-request-id;
proxy_hide_header       Set-Cookie;
proxy_ignore_headers    "Set-Cookie";

# Activate the proxy buffering, without it limiting bandwidth speed in proxy will not work!
proxy_buffering on;

# Buffer 512 KB data
proxy_buffers 32 16k;

proxy_intercept_errors  on;
resolver        8.8.8.8 valid=300s;
resolver_timeout        10s;

# The next two lines could be used if your storage
# backend does not support Content-Disposition
# headers used to specify file name browsers use
# when save content to the disk
proxy_hide_header Content-Disposition;
add_header Content-Disposition 'attachment; filename="$filename"';

# Do not touch local disks when proxying
# content to clients
proxy_max_temp_file_size 0;

# Limit the connection to one per IP address
limit_conn addr 1;

# Limit the bandwidth to 300 kilobytes
proxy_limit_rate 300k;

# Set logging level to info so we can see everything.
# All levels you can set: info | notice | warn | error
limit_conn_log_level info;   

# Finally download the file and send it to client
# Beware that you can shouldn't include "htttp://" or "https://"
# in proxy. Doing that will cause an "invalid port in upstream" error.
proxy_pass $download_url;
}

在 PHP 中画龙点睛,并将您签名的 URL 发送到 Nginx:

header( 'X-Accel-Redirect: ' . '/internal_redirect/' . $YOUR_SIGNED_URL );

【讨论】:

    猜你喜欢
    • 2011-05-21
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    相关资源
    最近更新 更多