I'm trying to create a JSON/JSONP proxy for an external API. Here's my config so far:
server {
listen 80;
server_name api.example.com;
location /api/ {
if ( $arg_callback ) {
echo_before_body '$arg_callback(';
echo_after_body ');';
}
if ($args ~ (.*)callback=[^&]*(.*)) {
set $args $1$2;
}
proxy_pass http://api.external.com;
rewrite ^/api/(.*)$ /external/api/$1?api_key=MY_KEY$is_args$args break;
}
}
The proxy works just fine with no callback
parameter, returning valid JSON. However, when I include the callback
, it still returns only JSON without the callback wrapped around it. I have to strip the callback
parameter before I call the external API, because it returns an error if the request contains unrecognized options. How can I strip the callback parameter out of the request (if it exists) before passing on the request but still use it to create the JSONP?