0

environment setup

I have nginx webserver and simple backend application. this backend app has custom response header in certain condition (for example: X-Real-Status) and it's running behind nginx as an upstream (reverse proxy). now i have this custom header value with $upstream_http_x_real_status in nginx configuration.

my question is: I have two log format as below:

log_format req_format '$uri $request_method $status $request_time $http_host $upstream_response_time';

log_format req_format_v2 '$uri $request_method $upstream_http_x_real_status $request_time $http_host $upstream_response_time';

I wanna merge these two log format to one, because my custom header (upstream_http_x_real_status) not set for some situations (hosting incident, runtime error and so on) and req_format_v2 print empty rather than status (which is status of response is 504 Bad Gateway for example). I wanna have something like this in nginx configuration:

# DANGER: obviously syntax error
# ------------------------------
$real_status = $upstream_http_x_real_status | $status
 
log_format new_req_format '$uri $request_method $real_status $request_time $http_host $upstream_response_time';

how to achieve this purpose?

2
  • try log_format req_format_v3 '$uri $request_method $status $upstream_http_x_real_status $request_time $http_host $upstream_response_time'; since all has been added, a | letter is invalid in the configuration of nginx.
    – djdomi
    Commented Feb 24 at 14:44
  • Just log them both.
    – symcbean
    Commented Feb 24 at 17:14

1 Answer 1

0

That's should be as simple as one map:

map $upstream_http_x_real_status $real_status {
  # use $upstream... by default
  default "$upstream_http_x_real_status";
  # but if it's empty, just use $status
  "" "$status";
}

log_format new_req_format '$uri $request_method $real_status $request_time $http_host $upstream_response_time';
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .