Skip to content

Instantly share code, notes, and snippets.

@benjaminbarbe
Forked from raucao/nginx-lua-s3.nginxconf
Last active September 18, 2023 17:29
Show Gist options
  • Save benjaminbarbe/1961db5ffbaad57eff12 to your computer and use it in GitHub Desktop.
Save benjaminbarbe/1961db5ffbaad57eff12 to your computer and use it in GitHub Desktop.

Revisions

  1. benjaminbarbe revised this gist Aug 18, 2015. 2 changed files with 14 additions and 5 deletions.
    5 changes: 5 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    [Proxy cache passes GET instead of HEAD to upstream](http://forum.nginx.org/read.php?2,247577,247577)... so we have a 403.

    This version include these fixes.
    - Remove $request_method from $string_to_sign and pass static string "GET" instead
    - Remove proxy_buffering directive for allowing the nginx cache
    14 changes: 9 additions & 5 deletions nginx-lua-s3.nginxconf
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,26 @@

    location ~* ^/s3/(.*) {
    proxy_cache static;
    proxy_cache_valid 200 24h;
    proxy_hide_header x-amz-id-2;
    proxy_hide_header x-amz-request-id;
    proxy_hide_header Set-Cookie;
    proxy_ignore_headers Set-Cookie;

    set $bucket '<REPLACE WITH YOUR S3 BUCKET NAME>';
    set $aws_access '<REPLACE WITH YOUR AWS ACCESS KEY>';
    set $aws_secret '<REPLACE WITH YOUR AWS SECRET KEY>';
    set $url_full "$1";
    set_by_lua $now "return ngx.cookie_time(ngx.time())";
    set $string_to_sign "$request_method\n\n\n\nx-amz-date:${now}\n/$bucket/$url_full";
    set $string_to_sign "GET\n\n\n\nx-amz-date:${now}\n/$bucket/$url_full";
    set_hmac_sha1 $aws_signature $aws_secret $string_to_sign;
    set_encode_base64 $aws_signature $aws_signature;

    resolver 172.31.0.2 valid=300s;
    resolver_timeout 10s;

    proxy_http_version 1.1;
    proxy_set_header Host $bucket.s3.amazonaws.com;
    proxy_set_header x-amz-date $now;
    proxy_set_header Authorization "AWS $aws_access:$aws_signature";
    proxy_buffering off;
    # proxy_buffering off;
    proxy_intercept_errors on;

    rewrite .* /$url_full break;
  2. Sebastian Kippe renamed this gist Nov 6, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. Sebastian Kippe created this gist Nov 6, 2014.
    25 changes: 25 additions & 0 deletions gistfile1.nginxconf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@

    location ~* ^/s3/(.*) {
    set $bucket '<REPLACE WITH YOUR S3 BUCKET NAME>';
    set $aws_access '<REPLACE WITH YOUR AWS ACCESS KEY>';
    set $aws_secret '<REPLACE WITH YOUR AWS SECRET KEY>';
    set $url_full "$1";
    set_by_lua $now "return ngx.cookie_time(ngx.time())";
    set $string_to_sign "$request_method\n\n\n\nx-amz-date:${now}\n/$bucket/$url_full";
    set_hmac_sha1 $aws_signature $aws_secret $string_to_sign;
    set_encode_base64 $aws_signature $aws_signature;

    resolver 172.31.0.2 valid=300s;
    resolver_timeout 10s;

    proxy_http_version 1.1;
    proxy_set_header Host $bucket.s3.amazonaws.com;
    proxy_set_header x-amz-date $now;
    proxy_set_header Authorization "AWS $aws_access:$aws_signature";
    proxy_buffering off;
    proxy_intercept_errors on;

    rewrite .* /$url_full break;

    proxy_pass http://s3.amazonaws.com;
    }