AWS, Cloud Computing

3 Mins Read

Deploying a Setup to Serve Different Responses Using an Amazon EC2 Instance

Voiced by Amazon Polly

Overview

This blog provides a comprehensive guide to configuring an Ubuntu 22.04 instance hosted on an Amazon EC2 instance to deliver dynamic responses based on URL path routing. By utilizing Nginx, a highly performant and lightweight web server, we will implement a basic server block configuration to serve “Test 1” for the root URL (<IP>) and “Test 2” for a specific path (<IP>/aws).

The tutorial includes detailed, step-by-step instructions, covering tasks such as setting up the Nginx virtual host, editing configuration files, and verifying HTTP responses using tools like curl or a web browser. Along the way, you will deepen your understanding of Amazon EC2 instance management, Nginx server directives, and efficient web server response handling.

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

Introduction

In this blog, we will leverage Nginx’s ability to handle routing and serve custom responses, demonstrating how it can be used for lightweight use cases like simple API endpoints or monitoring services. We will focus on deploying an Amazon EC2 instance, a scalable and flexible virtual machine provided by Amazon Web Services. We will install and configure Nginx on Ubuntu 22.04, set up custom server blocks to handle specific URL paths, and ensure our server is accessible via the public IP of the instance. This practical example will give you a deeper understanding of Nginx’s configuration and AWS infrastructure.

Step-by-Step Guide

Step 1: Set Up Your Amazon EC2 Instance

  1. Launch an Amazon EC2 Instance
  • Log in to your AWS Management Console.
  • Navigate to the Amazon EC2 Dashboard and launch a new instance.
  • Choose Ubuntu 22.04 as your AMI (Amazon Machine Image).
  • Select an instance type (e.g., t2.micro for the free tier).
  • Configure security groups to allow HTTP (port 80) and SSH (port 22)
  1. Connect to Your Instance
  • Use an SSH client (e.g., Terminal on macOS/Linux or PuTTY on Windows) to connect to your Amazon EC2 instance:

Step 2: Install and Configure Nginx

  1. Update System Packages

Run the following commands to ensure your packages are up to date:

2. Install Nginx

Install Nginx with the following command:

3. Start and Enable Nginx

Ensure Nginx starts automatically after installation:

Step 3: Configure Nginx to Serve Custom Responses

  1. Modify the Nginx Configuration
  • Nginx stores its configuration files in the /etc/nginx directory. We will edit the default configuration file to create two custom routes:

Open the default configuration file in a text editor:

Replace the content of the server block with the following:

2. Test the Configuration

Run the following command to test the configuration:

If successful, you’ll see a message indicating: test is successful

  1. Restart Nginx

Apply the changes by restarting Nginx:

Step 4: Verify the Setup

  1. Access the Amazon EC2 Public IP
  • Open your browser and enter the public IP address of your Amazon EC2 instance http://<public-ip>.
  • You should see the response: “Test 1”.

step4

2. Test the Custom Path

  • In your browser, navigate to http://<public-ip>/aws.

You should see the response: “Test 2”.

step4b

Conclusion

Following this tutorial, you successfully set up an Ubuntu 22.04 instance on Amazon EC2 and configured Nginx to serve custom responses based on URL paths.

This blog demonstrates the flexibility of Nginx in handling different request routes, providing a foundational understanding of its configuration capabilities. Beyond this, it can be extended to more complex use cases, such as hosting web applications, implementing reverse proxies, or building scalable infrastructure for production environments.

With Amazon EC2 and Nginx, you now have a powerful combination to experiment, innovate, and deploy tailored solutions for diverse web service requirements.

Drop a query if you have any questions regarding Amazon EC2 or Docker and we will get back to you quickly.

Making IT Networks Enterprise-ready – Cloud Management Services

  • Accelerated cloud migration
  • End-to-end view of the cloud environment
Get Started

About CloudThat

CloudThat is an award-winning company and the first in India to offer cloud training and consulting services worldwide. As a Microsoft Solutions Partner, AWS Advanced Tier Training Partner, and Google Cloud Platform Partner, CloudThat has empowered over 850,000 professionals through 600+ cloud certifications winning global recognition for its training excellence including 20 MCT Trainers in Microsoft’s Global Top 100 and an impressive 12 awards in the last 8 years. CloudThat specializes in Cloud Migration, Data Platforms, DevOps, IoT, and cutting-edge technologies like Gen AI & AI/ML. It has delivered over 500 consulting projects for 250+ organizations in 30+ countries as it continues to empower professionals and enterprises to thrive in the digital-first world.

FAQs

1. Why is Nginx returning a “403 Forbidden” error?

ANS: – This error typically occurs when the default Nginx directory permissions are misconfigured. Ensure that the /var/www/html directory or custom paths have the correct permissions. In this case, permissions are not required since we serve direct responses.

2. What if I can’t access the Amazon EC2 instance from my browser?

ANS: – Ensure your Amazon EC2 instance’s security group allows inbound traffic on port 80. Go to the AWS Console > Amazon EC2 Dashboard > Security Groups and verify the rules for your instance.

WRITTEN BY Vaishali Bhawsar

Vaishali is working as a Research Associate in CloudThat Technologies. She has good knowledge of Networking, Linux systems & C language, and currently working on various AWS projects along with, Terraform, Docker, and Ansible. She enjoys painting and cooking during her free time.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!