Skip to content

Instantly share code, notes, and snippets.

@chids
Created March 17, 2011 11:54
Show Gist options
  • Save chids/874201 to your computer and use it in GitHub Desktop.
Save chids/874201 to your computer and use it in GitHub Desktop.
How to set the HTTP accept header based on the file extension in Nginx
# Copy the accept header to a variable
set $acceptHeader $http_accept;
# Does the request end in xml or json?
if ($uri ~ ^(.*)\.(xml|json)$) {
# Create a new accept header...
set $acceptHeader "application/$2";
# ...and remove the extension from the path
rewrite ^(.*)\.(xml|json)$ $1 last;
}
# Set the Accept header for the proxied resource
proxy_set_header Accept $acceptHeader;
# ...continue with normal proxy conf, host and path mapping etc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment