AlmaLinuxCentOSDebianLinuxLinux MintManjaroTutorialsUbuntu

How To View Nginx Log Files on Linux

View Nginx Log Files on Linux

In this tutorial, we will show you how to view Nginx log files on Linux. For those of you who didn’t know, When managing Nginx web servers, one of the most frequent tasks you’ll perform is checking the log files. Nginx logging to help you troubleshoot and quickly resolve any problem you may encounter on your Nginx web server. Logging is a very powerful tool that will give you valuable data about all the operations of your server. Nginx writes records of its events in two types of logs: access logs and error logs. Access logs write information about client requests, and error logs write information about the server and application issues.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step view of Nginx web server log files on Linux.

Prerequisites

  • A server running one of the following operating systems: Ubuntu and any other Debian-based distribution or RHEL-based.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

View Nginx Log Files on Linux

Step 1. Nginx Access Logs.

The Nginx logs the activities of all the visitors to your site in the access logs. Here you can find which files are accessed, how NGINX responded to a request, what browser a client is using, the IP address of clients, and many more.

By default on most Linux distributions, such as Ubuntu, AlmaLinux, and Debian-based, access and error logs are located in the /var/log/nginx directory.

  • Configure the Nginx access log.

Access log should be enabled by default. If it’s not, you can enable it manually in the Nginx configuration file using the access_log directive:

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

If you want to customize the output of the access log you can do it using the log_format directive:

log_format custom '$remote_addr - $remote_user [$time_local] '
                           '"$request" $status $body_bytes_sent '
                           '"$http_referer" "$http_user_agent";

Variables used in the example configure above:

  • $remote_addr: IP address of the client
  • $remote_user: Information about the user making the request
  • $time_local: Server’s local date and time
  • $request: Actual request
  • $status: Response code
  • $body_bytes_sent: Size of the response in bytes
  • $http_referer: IP address of the HTTP referer (original source)
  • $http_user_agent: Detailed browser information

After that, reload the Nginx webserver to apply the new settings. To view the access logs for the domain your-domain.com in the file /var/log/nginx/your-domain.com.access.log, use the following tail command in the terminal:

tail -f /var/log/nginx/your-domain.com.access.log

Step 2. Nginx Error Logs.

Whenever Nginx suddenly stopped running or encounters an error, it stores the event data in the error log. This is especially useful when debugging as it saves time and makes troubleshooting easier and more efficient.

  • Configure the Nginx error log.

The error log should be enabled by default. If it’s not, you can enable it manually in the Nginx configuration file using the error_log directive:

error_log /var/log/nginx/error.log;

Below are levels listed by their severity (from low to high):

  • debug : messages used for debugging (LOWEST)
  • info : informational messages
  • notice : notices
  • warn : warnings
  • error : errors while processing the request (doesn’t require immediate action)
  • crit : Critical error that requires prompt action
  • alert : Error that requires immediate action
  • emerg : System is unusable (HIGHEST)

As in the case of the access log, you can log errors into multiple files. Please note that the file has to be manually created before you can log into it:

error_log /var/log/nginx/error_log warn;

For example to view the access logs for the domain your-domain.com in the file /var/log/nginx/your-domain.com.error.log, use the following tail command in the terminal:

tail -f /var/log/nginx/your-domain.com.error.log

Congratulations! You have successfully monitored the Nginx access log and the Nginx error log. For more information about the Apache access log and the Apache error log, please check the official Nginx website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button