3

I'm trying to log data from custom header. In response:

Cache-Control:no-cache
Connection:keep-alive
Content-Type:application/json
Date:Mon, 09 Nov 2015 16:09:09 GMT
Server:nginx/1.9.4
Transfer-Encoding:chunked
X-Extended-Info:{"c":70}
X-Powered-By:PHP/5.6.12

In php script (Symfony2):

$response->headers->set('X-Extended-Info', json_encode($info))

I wanna write in log data from "X-Extended-Info".

Nginx config:

log_format main_log '$extended_info';
server {
    set $extended_info '-';
    if ($sent_http_x_extended_info != '') {
        set $extended_info $sent_http_x_extended_info;
    }
   ...
}

And in log I see only '-'. I read nginx - read custom header from upstream server, but this solution doesn't work in my case (I tried to use $upstream_http_, $http_).

Is possible to read response from phpfpm? Thank you.

Community
  • 1
  • 1
Niomin
  • 68
  • 1
  • 6
  • When I use $sent_http_connection, it works. When I use the most other headers, $sent_http_ is empty. – Niomin Nov 09 '15 at 16:19

2 Answers2

2

if directive works before your request send to backend, so at that time there is no $sent_http_... variable.

You could use map directive.

log_format main_log '$extended_info';

map $sent_http_x_extended_info $extended_info {
    default $sent_http_x_extended_info;
    "" "-";
}
Alexey Ten
  • 13,794
  • 6
  • 44
  • 54
0

I had the same issue that Niomin had , where the expected header just showed up as "-". I was able to get this working by simply changing my fastcgi_pass directive to an upstream value, before i was just hardcoding it to a unix socket like the below shows:

#fastcgi_pass unix:/home/phillip/php.sock; fastcgi_pass php_backend;

Creating an entry for upstream servers i believe is necessary to log the cool $upstream_http_* response headers...below is what i created to replace my hardcoded unix socket...

upstream php_backend { server unix:/home/phillip/php.sock; }

Then i edited the log_format to include that upstream_http_x_forwarded_by

log_format main '$host - $remote_addr - [$time_local] "$request" ' '$status "X-Powered-By $upstream_http_x_powered_by "$http_referer" ''"$http_user_agent"';