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
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
- 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)
- 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:
1 |
ssh -i your-key.pem ubuntu@<public-ip> |
Step 2: Install and Configure Nginx
- Update System Packages
Run the following commands to ensure your packages are up to date:
1 |
sudo apt update && sudo apt upgrade -y |
2. Install Nginx
Install Nginx with the following command:
1 |
sudo apt install nginx -y |
3. Start and Enable Nginx
Ensure Nginx starts automatically after installation:
1 2 |
sudo systemctl start nginx sudo systemctl enable nginx |
Step 3: Configure Nginx to Serve Custom Responses
- 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:
1 |
sudo vi /etc/nginx/sites-available/default |
Replace the content of the server block with the following:
1 2 3 4 5 6 7 8 9 10 11 12 |
server { listen 80; server_name <ip address>; location / { return 200 'Test 1'; add_header Content-Type text/plain; } location /aws { return 200 'Test 2'; add_header Content-Type text/plain; } } |
2. Test the Configuration
Run the following command to test the configuration:
1 |
sudo nginx -t |
If successful, you’ll see a message indicating: test is successful
- Restart Nginx
Apply the changes by restarting Nginx:
1 |
sudo systemctl restart nginx |
Step 4: Verify the Setup
- 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”.
2. Test the Custom Path
- In your browser, navigate to http://<public-ip>/aws.
You should see the response: “Test 2”.
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.
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
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront, Amazon OpenSearch, AWS DMS and many more.
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.
Click to Comment