{"id":617,"date":"2020-11-12T17:28:03","date_gmt":"2020-11-12T17:28:03","guid":{"rendered":"http:\/\/localhost\/cit\/?p=617"},"modified":"2020-11-12T19:03:00","modified_gmt":"2020-11-12T19:03:00","slug":"nginx-log-files-a-closer-look","status":"publish","type":"post","link":"http:\/\/localhost\/cit\/nginx-log-files-a-closer-look\/","title":{"rendered":"Nginx Log Files: A Closer Look"},"content":{"rendered":"\n

In this article we’ll take a little look into the Nginx Log files, and their different configurations. <\/p>\n\n\n\n

\"nginx<\/figure>\n\n\n\n

Nginx (pronounced Engine X) has become one of the most popular web servers on the internet. This is due to its fast speeds and lightweight deployment. After all as developers; all we want is to use tools that don\u2019t leave much of a footprint on our infrastructure. Furthermore at the same time give us a high degree of performance. <\/p>\n\n\n\n

Its ability to scale and handle large traffic volumes has led to it gaining market share. But a key component of the Nginx system is its\u2019 error handling and logging functions. <\/p>\n\n\n\n

It provides flexible logging features to capture valuable details and help you understand the behavior of your web server. Nginx offers two different files for logging web server data these are namely the error_log and access_log. <\/p>\n\n\n\n

Starting access_log<\/em><\/strong> is used for storing information about web client requests, while error_log<\/em><\/strong> stores other unexpected or informative messages.<\/p>\n\n\n\n

Nginx Log Files: access_log<\/em><\/strong><\/h2>\n\n\n\n

Let\u2019s take a deeper dive into the access_log files, it gathers all client requests immediately after the request is processed, which enables developers to log which pages users are requesting from the webserver. There is also the added flexibility to specify where you want your logs should be using the command:<\/p>\n\n\n\n

access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];\naccess_log off;\n<\/code><\/pre>\n\n\n\n

You end up with something like this:<\/p>\n\n\n\n

http {\n     \t ...\n     \t ...\n     \t access_log  \/var\/log\/nginx\/access.log;\n    \t  ...\n   \t   \u2026\n}\n<\/code><\/pre>\n\n\n\n

Note<\/strong>:<\/p>\n\n\n\n

It is always better to segregate the access logs of all the virtual hosts by recording them in a separate file.<\/p>\n\n\n\n

server {\n    listen 80;\n    server_name domain1.com\n    access_log  \/var\/log\/nginx\/domain1.access.log;\n    ...\n    ...\n    }\n    \ntail -f \/var\/log\/nginx\/domain1.access.log <\/code><\/pre>\n\n\n\n

Allows you to view your logs for each domain<\/p>\n\n\n\n

Format\u201d allows you to use custom format in your logs by using variables such as number of bytes sent to the client ($bytes_sent) or the request length ($request_length).<\/p>\n\n\n\n

Whereas, the \u201cif=condition\u201d parameter provides a powerful way to perform conditional logging, so it only stores log access log messages if a certain condition holds true.<\/p>\n\n\n\n

Nginx Log Files: error_log<\/em><\/strong><\/h2>\n\n\n\n

In Nginx, the error_log file captures all log messages at the error severity level. Which means it\u2019s primarily used for understanding fatal or critical messages to help with troubleshooting. The default location for error_log is logs\/error.log. The way Nginx stores error messages is flexible and\u2014along with allowing you to write messages to a file\u2014it also supports sending error_log messages to stderr or the Syslog daemon. If you\u2019re running NGINX open source 1.5.2 or newer, you can also send error_log messages to more than one place at a time by specifying multiple error_log directives on the same configuration level.<\/p>\n\n\n\n

The Syntax for error_log forllows the convention:<\/p>\n\n\n\n

error_log log_file log_level;<\/code><\/pre>\n\n\n\n

Where the log_file gives the path the log is to be stored and the log_level indicates the severity of the logs to be kept.<\/p>\n\n\n\n

http {\n        ...\n        error_log  \/var\/log\/nginx\/error_log  crit;\n        ...\n}<\/code><\/pre>\n\n\n\n

The same convention applies to error_log as it di to access_log where you can specify each to the individual virtual host.<\/p>\n\n\n\n

http {\n       ...\n       ...\n       error_log  \/var\/log\/nginx\/error_log;\n       server {\n\t        \tlisten 80;\n\t\t        server_name domain1.com;\n       \t\t        error_log  \/var\/log\/nginx\/domain1.error_log  warn;\n                        ...\n\t   }\n       server {\n\t        \tlisten 80;\n\t\t        server_name domain2.com;\n      \t\t        error_log  \/var\/log\/nginx\/domain2.error_log  debug;\n                        ...\n\t   }\n}\n<\/code><\/pre>\n\n\n\n

You can also send the error_log directly to Syslog using:<\/p>\n\n\n\n

error_log syslog:server=192.168.10.11 debug;<\/code><\/pre>\n\n\n\n

Note<\/strong>:<\/p>\n\n\n\n

There are varying types of log events that hold corresponding levels of priority and you can configure your webserver to show only the ones that meet a certain level. These levels include:<\/p>\n\n\n\n

\n
<\/div>\n\n\n\n
\n