# Dynamic Proxy Pass - is there a more elegant solution?

**URL:** <https://www.ruby-forum.com/t/dynamic-proxy-pass-is-there-a-more-elegant-solution/223789>\
**Category:** NGINX\
**Created:** [October 17, 2012, 7:33pm UTC](https://www.ruby-forum.com/t/dynamic-proxy-pass-is-there-a-more-elegant-solution/223789 "2012-10-17T19:33:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [October 17, 2012, 7:33pm UTC](https://www.ruby-forum.com/t/dynamic-proxy-pass-is-there-a-more-elegant-solution/223789/1 "2012-10-17T19:33:53Z")

</div>

I’m X-Accel-Redirecting URLs that look like:

/AWSS3/bucket/key?auth=value

Right now my location that handles these and requests from S3 looks like  
this:

location ~ /AWSS3/(.\*) {

# Store our ETag value.

set $rails\_etag $upstream\_http\_etag;

# Prevent Amazon from overwriting our Headers.

proxy\_hide\_header Content-Type;  
proxy\_hide\_header ETag;

# Hide Amazon Headers

proxy\_hide\_header X-Amz-Id-2;  
proxy\_hide\_header X-Amz-Request-Id;

# Set the HTTP Host header to S3.

proxy\_set\_header Host ‘[s3.amazonaws.com](http://s3.amazonaws.com)’;

# Force Amazon to do the heavy lifting.

proxy\_max\_temp\_file\_size 0;

# Ensure tight timeouts, we’ll retry the requests to a different

backend in

# the event of failures.

proxy\_connect\_timeout 5;  
proxy\_send\_timeout 10;  
proxy\_read\_timeout 10;

# Retry if Amazon freaks out.

proxy\_next\_upstream error timeout http\_500 http\_502 http\_503 http\_504;

# Ensure the requests are always gets.

proxy\_method GET;  
proxy\_set\_header Method ‘GET’;  
proxy\_set\_header Content-Length “”;  
proxy\_set\_header Cookie “”;  
proxy\_set\_header Content-Type “”;

# Clear any CloudFront headers.

proxy\_set\_header X-Amz-Cf-Id “”;

# We use the query string for Authorization, clear headers that the

client

# may have sent.

proxy\_set\_header Authorization “”;

# Resolver, for dynamically proxied requests.

resolver 8.8.8.8;

# Proxy to S3.

set $s3 “[s3-external-2.amazonaws.com](http://s3-external-2.amazonaws.com)”;  
proxy\_pass https://$s3/$1$is\_args$args;

# Add back our own ETag.

add\_header ETag $rails\_etag;

internal;  
}

Is there a way to avoid having to capture the path in the location  
block and reconstruct it with $is\_args$args?

I would like something more like:

location /AWSS3/ {  
…  
proxy\_pass https://$s3/;  
}

which does work when passing to an upstream block ([https://s3/](https://s3/) with  
upstream s3 defined).

I prefer this method as it re-resolves the DNS periodically.
