Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

NGINX - Customizing Logging to Fit Your Needs

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.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 0m
Published
Clock icon Jan 10, 2020

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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.

  2. Challenge

    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.

  3. Challenge

    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.

  4. Challenge

    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.

  5. Challenge

    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
    
  6. Challenge

    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.

  7. Challenge

    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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans