5

I'm trying to set up Nginx as a proxy for a static html/js webapp hosted on S3. I read a lots of tutorials and posts and I could make it work if my bucket is set as public.

The reason I need Nginx as a proxy is that I don't want my bucket to be public.

Following this guide, I added the set-misc-nginx-module from this GitHub repo. The extra module, by providing AWS key and AWS secret, builds the authenticated S3 requests for each object of the bucket.

I recompiled Nginx and I managed to allow it to access to the protected bucket. The problem is that I cannot render the html, and basically I'm being served with the xml content of the bucket I'm trying to proxy.

This is the configuration file of Nginx

server {
    listen 80;
    server_name [MY_DNS];

location * {
    set $bucket           '[MY_BUCKET]';
    set $aws_access       '[MY_AWS_KEY]';
    set $aws_secret       '[MY_AWS_SECRET]';
    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;
}

auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;

}

And this is the result if I try to access to my website

<ListBucketResult>
    <Name>[MY_BUCKET]</Name>
    <Prefix />
    <Marker />
    <MaxKeys>1000</MaxKeys>
    <IsTruncated>false</IsTruncated>
    <Contents>
        <Key>index.html</Key>
        <LastModified>[LAST_MODIFIED]</LastModified>
        <ETag>[ETAG]</ETag>
        <Size>22</Size>
        <Owner>
            <ID>[OWNER_ID]
            </ID>
            <DisplayName>[NAME]</DisplayName>
        </Owner>
        <StorageClass>STANDARD</StorageClass>
    </Contents>
</ListBucketResult>

**EDIT:**The index.html does have the content type set as text/html.

Probably I'm missing something in the Nginx configuration.

All works fine if I set the entire bucket as public and if I use the "simple" proxy approach.

4

You've more than likely already resolved this, but you can try adding proxy_hide_header Content-Type; inside your location directive

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.