Before we dive into the setup, let’s quickly understand what a reverse proxy is and why it’s useful.
What is a Reverse Proxy ?
A reverse proxy is a server that sits in front of web servers and forwards client requests to those web servers. It acts as a middleman between clients and your application servers.
Benefits
- Improved security
- Load balancing
- Caching
- SSL encryption
“A reverse proxy is like a smart receptionist for your web servers, directing traffic and providing additional services.”
Pre-requisites
Before we start, make sure you have:
- An AWS account
- An EC2 instance running Amazon Linux 2
- Basic knowledge of Linux commands
- SSH access to your EC2 instance
Step 1: Update Your System
First things first, let’s make sure our system is up to date.
- Connect to your EC2 instance via SSH
- Run the following commands:
sudo yum update -y
sudo yum install -y httpd
This updates your system and installs Apache HTTP Server.
Step 2: Install Nginx
Nginx is a popular choice for a reverse proxy. Let’s install it:
- Add the Nginx repository:
sudo amazon-linux-extras install nginx1
- Install Nginx:
sudo yum install -y nginx
Step 3: Configure Nginx as a Reverse Proxy
Now, let’s set up Nginx to act as a reverse proxy:
- Open the Nginx configuration file:
sudo nano /etc/nginx/nginx.conf
- Replace the contents with the following:
http {
upstream backend {
server localhost:8080;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
events {}
- Save and exit the file (Ctrl+X, then Y, then Enter)
upstream backend
: Defines a group of servers. In this case, we’re forwarding to a local server on port 8080.server
: Defines how Nginx should handle requests.proxy_pass
: Tells Nginx where to forward requests.proxy_set_header
: Sets headers for the proxied request.
Step 4: Start and Enable Nginx
Let’s get Nginx up and running:
- Start Nginx:
sudo systemctl start nginx
- Enable Nginx to start on boot:
sudo systemctl enable nginx
Step 5: Configure Your Application Server
For this example, we’ll use a simple Python HTTP server:
- Create a simple HTML file:
echo "Hello from the backend!" > index.html
- Start a Python HTTP server on port 8080:
python3 -m http.server 8080
Step 6: Test Your Setup
Now, let’s make sure everything is working:
- Open your web browser
- Navigate to your EC2 instance’s public IP address
If everything is set up correctly, you should see “Hello from the backend!“
Troubleshooting
If you’re not seeing the expected result, try these steps:
- Check Nginx status:
sudo systemctl status nginx
- Look at Nginx error logs:
sudo tail -f /var/log/nginx/error.log
- Ensure your EC2 security group allows inbound traffic on port 80
Congratulations! You’ve successfully set up a reverse proxy on AWS Linux. This setup can be the foundation for more complex configurations, including load balancing multiple backend servers or adding SSL encryption.
Remember, a reverse proxy is a powerful tool that can significantly enhance your web infrastructure’s performance and security. As you become more comfortable with this setup, explore additional features and optimizations to further improve your system.