Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I echo a request header value as the response body with vanilla Nginx?

Tags:

nginx

Is it possible to configure Nginx to return a response body created from a request header, or a request parameter? It looks like this can be done with the echo module, but if possible I would like to do it with a vanilla install of Nginx.

Basically I want to do the following, but obviously return_body doesn't exist, so what can I use instead?

location ~* ^/echo/(.+) {
  return_body $1;
}

or

location /echo_user_agent {
  return_body $http_user_agent;
}

If I install the echo module I could replace return_body with echo, but if at all possible it would be nice to be able to do this without having to install any extras, it seems to me like something simple like this should be possible to do without.

like image 420
Theo Avatar asked Dec 14 '11 09:12

Theo


2 Answers

You can do this with return 200:

location = /echo_user_agent {
  return 200 $http_user_agent;
}
like image 57
kolbyjack Avatar answered Sep 19 '22 19:09

kolbyjack


Use ngx_lua module.

Like this:

body_filter_by_lua 'ngx.log(ngx.CRIT,ngx.arg[1])';

in the right location.

like image 42
Yuri Avatar answered Sep 19 '22 19:09

Yuri