Infrastructure

Nginx Setup for Running Frontend and Backend on AWS EC2

Editor | February 26, 2026 | 4 min read

When you deploy both frontend (FE) and backend (BE) on one EC2 instance, Nginx is the standard way to route traffic cleanly. It can serve static frontend files, proxy API requests to backend services, and terminate HTTPS in one place.

This keeps your architecture simple while still production-friendly.

Typical Architecture
  • frontend app served from /var/www/frontend (or proxied to FE server)
  • backend app running on internal port (for example 127.0.0.1:8000)
  • Nginx listening on ports 80 and 443

Users access one domain, and Nginx handles request routing.

Example Nginx Configuration
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;

    # Frontend (static build)
    root /var/www/frontend;
    index index.html;

    location / {
        try_files $uri /index.html;
    }

    # Backend API
    location /api/ {
        proxy_pass http://127.0.0.1:8000/;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

If your frontend is also a Node process (for example Next.js SSR), proxy / to that FE process instead of using root.

Required Setup Checklist on EC2
  1. Install Nginx and enable it at boot.
  2. Keep FE/BE services running with systemd or a process manager.
  3. Open security group ports 80 and 443.
  4. Restrict backend port access from public internet.
  5. Set DNS A record to EC2 public IP.

Without firewall and security group hygiene, reverse proxy setup alone is not enough.

HTTPS and Production Hardening
  • Use Let's Encrypt (certbot) for TLS certificates.
  • Redirect all HTTP traffic to HTTPS.
  • Add request size limits and basic rate limiting.
  • Configure logs for upstream error visibility.

These steps make debugging and operations much easier in production.

Final Take

Nginx is the simplest reliable pattern for hosting FE and BE together on EC2. With clear path-based routing, TLS, and proper process management, one instance can serve production workloads efficiently.