Search code examples
nginxamazon-s3proxypass

Nginx Proxy pass to s3 for js files returns 400 Bad request


Below is my nginx configuration.

    server_name  myserver.com;

    location ~ /(in|sr)/(en|tm)/(.+\.(css|js)) {
        proxy_set_header Host http://mybucketabc.s3.amazonaws.com;

        proxy_http_version     1.1;
        proxy_set_header       Connection "";
        proxy_set_header       Authorization '';
        proxy_hide_header      x-amz-id-2;
        proxy_hide_header      x-amz-request-id;
        proxy_hide_header      x-amz-meta-server-side-encryption;
        proxy_hide_header      x-amz-server-side-encryption;
        proxy_hide_header      Set-Cookie;
        proxy_ignore_headers   Set-Cookie;
        proxy_intercept_errors on;
        add_header             Cache-Control max-age=31536000;

        rewrite ^/(in|sr)/(en|tm)/(.*) /$1/$2/$3 break;
        proxy_pass    http://mybucketabc.s3.amazonaws.com;
    }

When I try to access http://myserver.com/in/en/main.9bd9926d.js, I'm getting "400 Bad Request"

But directly accessing http://mybucketabc.s3.amazonaws.com/in/en/main.9bd9926d.js works fine.

I've similar proxy pass rules for html and svg files, both works fine but requests to .js files always throws "400 Bad Request".

Any help is really appreciated.


Solution

  • This got resolved after correcting the Host.

    Wrong Host proxy_set_header Host http://mybucketabc.s3.amazonaws.com;

    Correct Host (removed "http://") proxy_set_header Host mybucketabc.s3.amazonaws.com;