Skip to main content

Configuring the NGINX Server – HTTPS Virtual Hosts / Load Balancing / IP Access Restrictions

Hands-On Lab

 

Photo of Tom Dean

Tom Dean

Linux Training Architect II

Length

00:45:00

Difficulty

Intermediate

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 configuring NGINX on Ubuntu Linux. We will explore configuring HTTPS (secure) virtual hosts, load balancing, and IP access restrictions.

Completing this lab will provide a good understanding of how to implement these concepts in NGINX on Ubuntu Linux.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Configuring the NGINX Server - HTTPS Virtual Hosts / Load Balancing / IP Access Restrictions

Introduction

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 configuring NGINX on Ubuntu Linux. We will explore configuring HTTPS (secure) virtual hosts, load balancing, and IP access restrictions.

Completing this lab will provide a good understanding of how to implement these concepts in NGINX on Ubuntu Linux.

The Scenario

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 HTTPS virtual hosts, load balancing, and IP address restrictions on an Ubuntu Linux server. Let's begin!

Logging In

Use the credentials provided on the hands-on lab overview page, and log in as cloud_user. We'll want root privileges for the whole lab, so let's run this as soon as we're logged in:

sudo su -

Verify the Self-Signed Certificate for the NGINX Server

First, let's verify that the X509 certificate was correctly generated using the openssl verify command:

openssl verify -CAfile /etc/nginx/ssl/bigstatecollege.edu/ca-cert.pem /etc/nginx/ssl/bigstatecollege.edu/server-cert.pem

We should see the following:
server-cert.pem: OK

Configure the Virtual Host to Use HTTPS

Configure the bigstatecollege.edu virtual host to use HTTPS:

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

Change the listen line from port 80 to 443, and add ssl after 443:

listen 443 ssl;

Add the following lines after the server_name configuration line:

        ssl on;
        ssl_certificate /etc/nginx/ssl/bigstatecollege.edu/server-cert.pem;
        ssl_certificate_key /etc/nginx/ssl/bigstatecollege.edu/server-key.pem;

Save and exit.

Validate HTTPS

Validate the new configuration:

nginx -t

Now we can reload NGINX:

systemctl reload nginx

Test the new HTTPS connection. We will need to use the --insecure switch in order to accept the self-signed certificate:

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

We should see Welcome to www.bigstatecollege.edu!.

Congratulations! The virtual host for bigstatecollege.edu is now configured to use HTTPS.

Configure Load Balancing on the Virtual Host

There's already an upstream group configured in the bigstatecollege.edu virtual host. Remove the backup status from app2 and app3. This will make them live. Edit the bigstatecollege.edu.conf file:

vi bigstatecollege.edu.conf

The end result should look like this:

upstream bscapp  {
   server app1.bigstatecollege.edu:8085;
   server app2.bigstatecollege.edu:8086;
   server app3.bigstatecollege.edu:8087;
}

Save and exit.

Test Load Balancing on the Virtual Host

We'll validate the configuration again, and reload NGINX:

nginx -t
systemctl reload nginx

Test the new configuration for https://www.bigstatecollege.edu/app:

curl --insecure https://www.bigstatecollege.edu/app

Reload the command several times. We should see Welcome to app1.bigstatecollege.edu!.

Restrict Access By IP Address

We're going to restrict the bigstatecollege.edu virtual host to the 127.0.0.1 interface. Add the following lines after the listen 443; line in the bigstatecollege.edu.conf file:

        allow 127.0.0.1;
        deny all;

Save and exit.

Test IP Address Restriction

Validate and reload NGINX again:

nginx -t
systemctl reload nginx

Test the new configuration for https://www.bigstatecollege.edu:

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

Access is forbidden via the private IP address. Try to access the virtual host via localhost:

curl --insecure -H "www.bigstatecollege.edu" https://localhost

We should see Welcome to www.bigstatecollege.edu!.

Conclusion

We configured a virtual host to use HTTPS, set up a load balancing cluster with three nodes, and then we restricted access to it all based on IP address. Everything is tested and working. Congratulations!