admin管理员组

文章数量:1279042

I'm running nginx version 1.14.0 on a GCP VM running a container OS (cos-stable-89-16108-470-1). I'm trying to get the nginx access logs to GCP cloud logging, in json format.

Firstly, logs appear on GCP cloud logging only after adding a sym link as follows:

RUN ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log

The logs are of the default 'combined' format:

ip1.ip2.ip3.ip4 - - [24/Feb/2025:11:56:09 +0000] "GET /api/health HTTP/2.0" 200 40 "-" "curl/8.7.1"

We've hit a roadblock trying to change the format to json. This is what the log_format setting looks like in the nginx.conf file:

  http {
  log_format json_format escape=json '{'
        '"msec": "$msec", '
        '"connection": "$connection", '
        '"connection_requests": "$connection_requests", '
        '"pid": "$pid", '
        '"request_id": "$request_id", '
        '"request_length": "$request_length", '
        '"remote_addr": "$remote_addr", '
        '"remote_user": "$remote_user", '
        '"remote_port": "$remote_port", '
        '"time_local": "$time_local", '
        '"time_iso8601": "$time_iso8601", '
        '"request": "$request", '
        '"request_uri": "$request_uri", '
        '}';

  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;

  access_log  /var/log/nginx/access.log json_format;

This does not have any effect on the logs that appear on GCP logging. Its still the same default nginx format.

This json format works fine on a docker container running on my local system. The only difference being, on the local container, there isn't a sym link.

Logs from my local container:

{"msec": "1740393829.310", "connection": "82", "connection_requests": "1", "pid": "11", "request_id": "cdbfe5d1304e04ac78e762560863701d", "request_length": "89", "remote_addr": "ip1.ip2.ip3.ip4", "remote_user": "", "remote_port": "57708", "time_local": "24/Feb/2025:10:43:49 +0000", "time_iso8601": "2025-02-24T10:43:49+00:00", "request": "GET / HTTP/1.1", "request_uri": "/", }
{"msec": "1740393831.360", "connection": "83", "connection_requests": "1", "pid": "11", "request_id": "0024b54995759551faa356855cc350e5", "request_length": "89", "remote_addr": "ip1.ip2.ip3.ip4", "remote_user": "", "remote_port": "57712", "time_local": "24/Feb/2025:10:43:51 +0000", "time_iso8601": "2025-02-24T10:43:51+00:00", "request": "GET / HTTP/1.1", "request_uri": "/", }

Any help on what I can try next? Any pointers would be much appreciated! Thank you.

I'm running nginx version 1.14.0 on a GCP VM running a container OS (cos-stable-89-16108-470-1). I'm trying to get the nginx access logs to GCP cloud logging, in json format.

Firstly, logs appear on GCP cloud logging only after adding a sym link as follows:

RUN ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log

The logs are of the default 'combined' format:

ip1.ip2.ip3.ip4 - - [24/Feb/2025:11:56:09 +0000] "GET /api/health HTTP/2.0" 200 40 "-" "curl/8.7.1"

We've hit a roadblock trying to change the format to json. This is what the log_format setting looks like in the nginx.conf file:

  http {
  log_format json_format escape=json '{'
        '"msec": "$msec", '
        '"connection": "$connection", '
        '"connection_requests": "$connection_requests", '
        '"pid": "$pid", '
        '"request_id": "$request_id", '
        '"request_length": "$request_length", '
        '"remote_addr": "$remote_addr", '
        '"remote_user": "$remote_user", '
        '"remote_port": "$remote_port", '
        '"time_local": "$time_local", '
        '"time_iso8601": "$time_iso8601", '
        '"request": "$request", '
        '"request_uri": "$request_uri", '
        '}';

  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;

  access_log  /var/log/nginx/access.log json_format;

This does not have any effect on the logs that appear on GCP logging. Its still the same default nginx format.

This json format works fine on a docker container running on my local system. The only difference being, on the local container, there isn't a sym link.

Logs from my local container:

{"msec": "1740393829.310", "connection": "82", "connection_requests": "1", "pid": "11", "request_id": "cdbfe5d1304e04ac78e762560863701d", "request_length": "89", "remote_addr": "ip1.ip2.ip3.ip4", "remote_user": "", "remote_port": "57708", "time_local": "24/Feb/2025:10:43:49 +0000", "time_iso8601": "2025-02-24T10:43:49+00:00", "request": "GET / HTTP/1.1", "request_uri": "/", }
{"msec": "1740393831.360", "connection": "83", "connection_requests": "1", "pid": "11", "request_id": "0024b54995759551faa356855cc350e5", "request_length": "89", "remote_addr": "ip1.ip2.ip3.ip4", "remote_user": "", "remote_port": "57712", "time_local": "24/Feb/2025:10:43:51 +0000", "time_iso8601": "2025-02-24T10:43:51+00:00", "request": "GET / HTTP/1.1", "request_uri": "/", }

Any help on what I can try next? Any pointers would be much appreciated! Thank you.

Share Improve this question asked Feb 24 at 12:32 ssk_seekssk_seek 193 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Modifying the conf file in the sites-enabled directory, worked for me:

server {
    # only listen to https here
    listen                          *:443 ssl http2;
    server_name                     thisisanexample

    access_log                      /var/log/nginx/access.log json_format;

Log output now:

"{"msec": "1740400753.196", "connection": "49", "connection_requests": "1", "pid": "13", "request_id": "dskfsdkfjksdfsd", "request_length": "46", "remote_addr": "ip1.ip2.ip3.ip4", "remote_user": "", "remote_port": "52672", "time_local": "24/Feb/2025:12:39:13 +0000", "time_iso8601": "2025-02-24T12:39:13+00:00", "request": "GET /api/health HTTP/2.0", "request_uri": "/api/health", }
"
}

Hope someone finds this useful.

本文标签: google cloud platformnginx access logs on GCPStack Overflow