NGINX - Customizing Logging to Fit Your Needs

1 hour
  • 7 Learning Objectives

About this Hands-on Lab

Before we can start building our world-changing website or application on LEMP, we have to lay the foundation – the stack. In this hands-on lab, we will walk through customizing NGINX logging on Ubuntu Linux. We will explore configuring custom `access_log` and `error_log` locations, custom log formats and more.

Completing this lab will provide a good understanding of how to customize logging in NGINX on Ubuntu Linux.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Configure the Logging Location for the Virtual Host

Become the root user:

sudo su -

Edit the bigstatecollege.edu.conf file:

cd /etc/nginx/sites-available
vi bigstatecollege.edu.conf

After the server_name line, add the following:

        access_log /var/log/nginx/bigstatecollege.edu_access.log;
        error_log /var/log/nginx/bigstatecollege.edu_error.log;

Save and exit.

Test the New Logging Location for the Virtual Host

Validate / reload NGINX:

nginx -t
systemctl reload nginx

Test the new configuration:

curl --insecure https://www.bigstatecollege.edu
ls -al /var/log/nginx/

We should see the new logs. Take a look at the contents of the new access log:

cat /var/log/nginx/bigstatecollege.edu_access.log

Access the virtual host several more times. Access a file that doesn’t exist, then take a look at the contents of the log again.

Configure the Error Logging Level for the Virtual Host

We’re going to set the error logs to the debug level, the most verbose:

vi bigstatecollege.edu.conf

On the error_log line, add debug:

error_log /var/log/nginx/bigstatecollege.edu_error.log debug;

Save and exit.

Test the Error Logging Level for the Virtual Host

Validate / reload NGINX:

nginx -t
systemctl reload nginx

Test the new configuration:

curl --insecure https://www.bigstatecollege.edu
curl --insecure https://www.bigstatecollege.edu/foo.txt

Let’s see what we generated in the new error log:

cat /var/log/nginx/bigstatecollege.edu_error.log

We’ll see there’s more detail in there.

Configure the Access Log to Use the ‘custom’ Format

Edit the virtual host configuration file:

vi bigstatecollege.edu.conf

On the access_log line, add the following:

access_log /var/log/nginx/bigstatecollege.edu_access.log custom;

Save and exit.

We will need to define the custom logging level in the nginx.conf file:

vi ../nginx.conf
Configure the ‘custom’ Access Log Format in nginx.conf

After the error_log line, add the following:

log_format  custom '$remote_addr - $remote_user [$time_local] '
                             '"$request" $status $body_bytes_sent '
                             '"$http_referer" "$http_user_agent" '
                             '"$http_x_forwarded_for" $request_id '
                             '$geoip_country_name $geoip_country_code '
                             '$geoip_region_name $geoip_city ';

Save and exit.

Test the ‘custom’ Access Log Format

Validate and reload NGINX:

nginx -t
systemctl reload nginx

Let’s test the new configuration:

curl --insecure https://www.bigstatecollege.edu
curl --insecure https://www.bigstatecollege.edu/foo.txt

Now, let’s see what we generated in the new access log:

cat /var/log/nginx/bigstatecollege.edu_access.log

You’ll see the format of the access log has changed.

Additional Resources

Big State College (BSC) is a Large Ten Conference school in a Midwestern state. BSC is looking to deploy a centralized web hosting service using the LEMP stack.

As the engineers tasked with executing this project, we will be configuring custom NGINX logging on an Ubuntu Linux server. Let's begin!

Connecting to the Lab Server

Use the credentials provided on the hands-on lab overview page, and log in using an SSH connection to the Public IP or DNS of the server as cloud_user.

Making a SSH connection using the ssh command. Replace <<public_IP_or_DNS>> with the Public IP or DNS provided on the overview page.

ssh cloud_user@<<public_IP_or_DNS>>

If you can't use ssh or prefer a GUI client, use the credentials provided on the hands-on lab overview page to configure your SSH connection to the lab server.

Lab Git Repository

Feel free to explore the configurations and code from the lab at: GitHub - linuxacademy/content-lemp-deep-dive

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?