0

Similar to this question and this question, I have a client (bazel) that is sending requests to nginx.

The client is adding a custom header called build_id as HTTP_BUILD_ID to each request.

I just want to log this header from nginx.

I wrote a small python flask application and printed all the headers and it showed that HTTP_BUILD_ID is indeed part of the request.

Unfortunately, nginx can't find it. This is what my log_format looks like with the addition of $http_build_id.

log_format main '$remote_addr - $upstream_cache_status [$time_local]
                '"$request" $status $body_bytes_sent '
                '[$http_build_id]'
                '"$http_referer" "$http_user_agent"';
GGhe
  • 663
  • 5
  • 17

1 Answers1

1

If you are sending it as HTTP_BUILD_ID, then you should use "$http_http_build_id" in your log_format

Also, do check your virtual host (server) definition to make sure it's using the main format. Some default installs look like: access_log FILE combined, where combined is a predefined format. Read more about that here

As pointed by the OP, installing the nginx-echo-headers package pointed out to some invalid headers. Adding underscores_in_headers on; fixed the issue.

Raul
  • 579
  • 1
  • 4
  • 13
  • The header HTTP_USER_AGENT is shown as $http_user_agent. While HTTP_REFERER is seen as $http_referer. I assumed that HTTP_BUILD_ID would be seen as $http_build_id. – GGhe Oct 11 '19 at 07:03
  • Look at `x-forwarded-for`. To log it, it's `http_x_forwarded_for`. User-Agent, Referrer, these are not prefixed by HTTP when sent by the browser. In nginx, to get a header, you do `http_HEADER`. And in your case, because your header is `HTTP_BUILD_ID`, you have the double http – Raul Oct 11 '19 at 07:10
  • Your answer is semi correct. I used `$http_build_id` but I needed to fix some configuration. Please update your answer. I used https://github.com/brndnmtthws/nginx-echo-headers to echo out what my requests were and then realized that it was saying that my header was invalid. I then added `underscores_in_headers on;` to fix the issue. – GGhe Oct 11 '19 at 17:32
  • @GGhe, thanks, updated. I will do a re-test on my end with that. Thanks for digging in more about this! – Raul Oct 11 '19 at 17:40