How to add request headers to Apigee NGnix access_log

In Apigee Edge Private Cloud, you can configure Apigee nginx router to customize your access log to log request and response headers.

Request headers would take the nginx variable format of

$http_requestheadername

Response headers would take the nginx variable format of

$upstream_http_responseheadername

A few syntax notes about these nginx variables:

1. The actually HTTP request header can have different cases, but the variable that you configure will be the name of the request header in all lower case.

2. If the HTTP request header contains a hyphen in the name, replace the hyphen with an underscore in the responseheadername variable format.

Using the following example request, lets say you want to log the value of the request headers "X-Language" and "X-CountryCode":

GET /v1/example HTTP/1.1
Host: api.example.com
X-Language: en-usX-CountryCode: us

The variable syntax for these headers would be:

$http_x_language
$http_x_countrycode

Now to log these variables, configure your nginx router with these steps:

1. Edit your router.properties, create it if it does not exist, here:

/opt/apigee/customer/application/router.properties 

2. Add the following two lines inside the router.properties:

conf_load_balancing_load.balancing.driver.nginx.global.http.parameters.template.extra=log_format router_new    '$time_iso8601\\\\t$hostname\\\\t$remote_addr:$remote_port\\\\t'\\\

\n\\n                     '$upstream_addr\\\\t$request_time\\\\t-\\\\t-\\\\t'\\\

\n\\n                     '$status\\\\t$upstream_status\\\\t$request_length\\\\t'\\\

\n\\n                     '$body_bytes_sent\\\\t'\\\

\n\\n                     '${conf_load_balancing_load.balancing.driver.nginx.log.format.router.request}\\\\t$upstream_http_x_apigee_message_id\\\\t'\\\

\n\\n                     '$http_user_agent\\\\t$host\\\\t$hostname-$pid-$connection-$connection_requests\\\\t$my_nginx_var_xff\\\\t'\\\

\n\\n                     '$upstream_http_x_apigee_fault_flag\\\\t$upstream_http_x_apigee_fault_source\\\\t$upstream_http_x_apigee_fault_code\\\\t'\\\

\n\\n                     '$upstream_http_x_apigee_fault_policy\\\\t$upstream_http_x_apigee_fault_flow\\\\t$upstream_http_x_apigee_fault_revision\\\\t'\\\

\n\\n                     '$upstream_http_x_apigee_dp_color\\\\t$my_x_apigee_target_latency\\\\t'\\\

\n\\n                     '$upstream_http_x_apigee_proxy\\\\t$upstream_http_x_apigee_proxy_basepath\\\\t'\\\

\n\\n                     '$self_region\\\\t$self_pod\\\\t$self_color\\\\t'\\\
\n\\n                     '$http_x_language\\\\t$http_x_countrycode';\\\

\n\\n
conf_load_balancing_load.balancing.driver.nginx.access.log=${conf_load_balancing_load.balancing.driver.nginx.log.dir}/${org}~${env}.${port}_access_log router_new

3. Restart the router:

apigee-service edge-router restart 

4. repeat on all routers

Router logs are located in the following directory:

/opt/apigee/var/log/edge-router/nginx

Logs are named in the format of org~env.port_access_log.

Make your requests and tail the access_log to see the new headers get logged.

Comments
anusmitadey
Bronze 2
Bronze 2

Hi @jghunt , 

I am trying to log a custom request header "X-ASGW-ID" which I am setting using Javascript which is added to Pre-proxy Flow Hook. 

 

context.setVariable("request.header.X-ASGW-ID",<<value>>);

 

However, the value comes blank when I try to log this in the nginx access log. 

I have tried using $http_x_asgw_id as well as $upstream_http_x_asgw_id, but both gives me blank value. Can you please suggest how we can log this?

Thanks in advance!

 

Version history
Last update:
‎02-27-2018 06:21 PM
Updated by: