Skip to content

Instantly share code, notes, and snippets.

@joshuar
Created October 30, 2015 03:53
Show Gist options
  • Select an option

  • Save joshuar/38a0196d29a9bc7bd971 to your computer and use it in GitHub Desktop.

Select an option

Save joshuar/38a0196d29a9bc7bd971 to your computer and use it in GitHub Desktop.
Logging Elasticsearch HTTP API Requests with Nginx
worker_processes 1;
error_log /var/log/nginx/error.log;
events {
worker_connections 1024;
}
http {
log_format es '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent'
'"$http_referer" "$http_user_agent" {$request_body}';
access_log /var/log/nginx/access.log es;
upstream elasticsearch {
server 10.250.250.1:9200;
}
server {
listen 8080;
location / {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_connect_timeout 5s;
proxy_read_timeout 10s;
proxy_pass http://elasticsearch;
}
}
}
@joshuar
Copy link
Author

joshuar commented Oct 30, 2015

Log messages in access.log look like the following:

10.250.250.1 - - [30/Oct/2015:03:50:58 +0000] "PUT /twitter/tweet/1 HTTP/1.1" 201 122"-" "curl/7.40.0" {{\x0A    \x22user\x22 : \x22kimchy\x22,\x0A    \x22post_date\x22 : \x222009-11-15T14:12:12\x22,\x0A    \x22message\x22 : \x22trying out Elasticsearch\x22\x0A}}

That's the log produced by the following request:

curl -XPUT http://localhost:8080/twitter/tweet/1 -d '{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}'

@fanslin
Copy link

fanslin commented Apr 12, 2017

Hi, How to print "\x0A" directly, do not line feed?

@lirulei
Copy link

lirulei commented Aug 5, 2023

like this:
log_format log_json escape=json '{"timestamp": "$time_local",'
'"remote_addr": "$remote_addr",'
'"referer": "$http_referer",'
'"request": "$request",'
'"status": "$status",'
'"byte": "$body_bytes_sent",'
'"agent": "$http_user_agent",'
'"x_forwarded_for": "$http_x_forwarded_for",'
'"up_addr": "$upstream_addr",'
'"up_host": "$upstream_http_host",'
'"up_resp_time": "$upstream_response_time",'
'"request_body": "$request_body",'
'"request_time": "$request_time"}';

make sure your nginx version >= 1.11.8
ref: http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment