Hosting on Amazon EC2 with Nginx: Custom Document Roots and Multi-Port Routing.
A hands-on guide to provisioning Nginx on an Amazon Linux EC2 instance, customizing root and index directives, and routing multiple web applications across distinct ports.
Deploying a web server in the cloud involves two synchronized layers: configuring the Linux operating system inside the virtual machine, and configuring network traffic policies in AWS.
Once you know how to install a daemon like Apache or Nginx, the next step is learning how to direct web traffic to custom project directories and expose different applications over distinct TCP ports.
Here is a step-by-step breakdown of configuring Nginx on Amazon EC2, mapping custom document roots, and handling multi-port routing.
---
1. Service Lifecycle Management on Linux
Modern Linux distributions rely on systemd to manage background services. Once Nginx is installed via your package manager, service management follows three essential commands:
• Start the service immediately:
sudo systemctl start nginx
sudo systemctl start nginx
• Ensure the daemon starts automatically if the EC2 instance reboots:
sudo systemctl enable nginx
sudo systemctl enable nginx
• Inspect the running processes and service health:
sudo systemctl status nginx
sudo systemctl status nginx
Checking the status output confirms that systemd has spawned both the Nginx master process and its associated worker processes.
---
2. Customizing the Document Root and Index Directives
By default, Nginx serves static assets from standard locations such as /usr/share/nginx/html or /var/www/html. In production environments, applications rarely sit in the default directory.
To point incoming traffic to a specific project folder:
1. Create a dedicated application directory:
sudo mkdir /usr/share/nginx/html/website
sudo mkdir /usr/share/nginx/html/website
2. Move your project files (HTML, CSS, assets) into that folder.
3. Edit the primary configuration file located at /etc/nginx/nginx.conf. Inside the `server` block, locate and update the `root` and `index` directives:
server {
listen 80;
server_name _;
root /usr/share/nginx/html/website;
index app.html index.html;
...
}
listen 80;
server_name _;
root /usr/share/nginx/html/website;
index app.html index.html;
...
}
• root: Tells Nginx which directory on the filesystem contains the site files.
• index: Tells Nginx which file to serve as the default landing page when a user hits the base URL.
• index: Tells Nginx which file to serve as the default landing page when a user hits the base URL.
After updating the configuration, always reload or restart the service to apply the new directives:
sudo systemctl restart nginx
sudo systemctl restart nginx
---
3. Multi-Port Web Hosting on a Single EC2 Instance
A single EC2 instance can host multiple web applications by binding separate Nginx server blocks to different TCP ports.
For example, you can serve a corporate marketing portal on the standard HTTP port (Port 80) and run a separate user dashboard or clone interface on Port 81.
Inside /etc/nginx/nginx.conf, you can define a secondary server block:
server {
listen 81;
server_name _;
root /usr/share/nginx/html/second-app;
index index.html;
}
listen 81;
server_name _;
root /usr/share/nginx/html/second-app;
index index.html;
}
---
4. Synchronizing AWS Security Groups
A running web server will remain unreachable from the public internet until the cloud firewall permits incoming connections.
To make custom ports reachable:
1. Navigate to the EC2 Console > Instances and select your instance.
2. Under the Security tab, click on the attached Security Group.
3. Edit Inbound Rules and add:
• Type: Custom TCP
• Port Range: 81
• Source: 0.0.0.0/0 (or your specific IP address)
4. Save the rules.
2. Under the Security tab, click on the attached Security Group.
3. Edit Inbound Rules and add:
• Type: Custom TCP
• Port Range: 81
• Source: 0.0.0.0/0 (or your specific IP address)
4. Save the rules.
Once saved, the AWS hypervisor permits traffic to pass through to the instance, allowing users to reach the secondary application directly at:
http://<EC2-Public-IP>:81
http://<EC2-Public-IP>:81
---
Key Takeaways
1. Document roots are flexible: You are not restricted to default web directories; Nginx allows you to define custom paths for each project.
2. Explicit entry points: The `index` directive provides full control over the exact entry file served to clients.
3. Defense in depth: A service running on Linux requires both internal process management (systemd) and cloud perimeter authorization (AWS Security Groups) to be accessible.
2. Explicit entry points: The `index` directive provides full control over the exact entry file served to clients.
3. Defense in depth: A service running on Linux requires both internal process management (systemd) and cloud perimeter authorization (AWS Security Groups) to be accessible.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article