- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud
Configuring the NGINX Server - HTTPS Virtual Hosts / Load Balancing / IP Access Restrictions
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.
Lab Info
Table of Contents
-
Challenge
Verify the Self-Signed Certificate for the NGINX Server
First, we'll need to become the
rootuser:sudo su -Let's verify that the X509 certificate was correctly generated using the
openssl verifycommand:openssl verify -CAfile /etc/nginx/ssl/bigstatecollege.edu/ca-cert.pem /etc/nginx/ssl/bigstatecollege.edu/server-cert.pemWe should see the following:
server-cert.pem: OK -
Challenge
Configure the Virtual Host to Use HTTPS
Configure the
bigstatecollege.eduvirtual host to use HTTPS:cd /etc/nginx/sites-availablevi bigstatecollege.edu.confChange the
listenline from port80to443, and addsslafter443:listen 443 ssl;Add the following lines after the
server_nameconfiguration line:ssl_certificate /etc/nginx/ssl/bigstatecollege.edu/server-cert.pem; ssl_certificate_key /etc/nginx/ssl/bigstatecollege.edu/server-key.pem;Save and exit.
-
Challenge
Validate HTTPS
Validate and reload NGINX:
nginx -tsystemctl reload nginxTest the new HTTPS connection. We will need to use the
--insecureswitch in order to accept the self-signed certificate:curl --insecure https://www.bigstatecollege.eduWe should see
Welcome to www.bigstatecollege.edu!.Congratulations! The virtual host for
bigstatecollege.eduis now configured to use HTTPS. -
Challenge
Configure Load Balancing on the Virtual Host
There's already an
upstreamgroup configured in thebigstatecollege.eduvirtual host. Remove thebackupstatus fromapp2andapp3. This will make them live. Edit thebigstatecollege.edu.conffile:vi bigstatecollege.edu.confThe end result should look like the following:
upstream bscapp { server app1.bigstatecollege.edu:8085; server app2.bigstatecollege.edu:8086; server app3.bigstatecollege.edu:8087; }Save and exit.
-
Challenge
Test Load Balancing on the Virtual Host
Validate and reload NGINX:
nginx -tsystemctl reload nginxTest the new configuration for
https://www.bigstatecollege.edu/app:curl --insecure https://www.bigstatecollege.edu/appReload the command several times. We should see
Welcome to app1.bigstatecollege.edu!. -
Challenge
Restrict Access By IP Address
We're going to restrict the
bigstatecollege.eduvirtual host to the 127.0.0.1 interface. Add the following lines after thelisten 443;line in thebigstatecollege.edu.conffile:allow 127.0.0.1; deny all;Save and exit.
-
Challenge
Test IP Address Restriction
Validate and reload NGINX:
nginx -tsystemctl reload nginxTest the new configuration for
https://www.bigstatecollege.edu:curl --insecure https://www.bigstatecollege.eduAccess is forbidden via the private IP address. Try to access the virtual host via
localhost:curl --insecure -H "www.bigstatecollege.edu" https://localhostWe should see
Welcome to www.bigstatecollege.edu!.
About the author
Real skill practice before real-world application
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.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.