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"';

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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
Your answer is semi correct. I used $http_build_id but I needed to fix some configuration. Please update your answer. I used 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, thanks, updated. I will do a re-test on my end with that. Thanks for digging in more about this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.