How to Fix Nginx Out of Memory (OOM) on AWS EC2


  1. The Root Cause Nginx Out of Memory (OOM) on AWS EC2 frequently occurs due to the selection of smaller instance types (e.g., t2/t3.micro, small) which possess limited physical RAM. This is exacerbated when Nginx, especially as a reverse proxy or serving many static files, allocates too much memory per worker process or runs an excessive number of worker processes, exceeding the instance’s available memory and swap.

  2. Quick Fix (CLI) Immediately alleviate memory pressure by restarting Nginx or adding swap space if the instance is critically low on RAM.

    # 1. Check current memory usage (optional, but good for diagnosis)
    top -bn1 | head -n 5
    # or install htop for a more visual overview
    # sudo yum install htop  # for Amazon Linux / RHEL
    # sudo apt-get install htop # for Ubuntu / Debian
    # htop
    
    # 2. Restart Nginx to release resources
    sudo systemctl restart nginx
    # OR for older systems:
    # sudo service nginx restart
    
    # 3. (Optional but recommended for low-memory EC2 instances) Add temporary swap space
    # Create a 4GB swap file (adjust count based on your needs)
    sudo fallocate -l 4G /swapfile
    sudo chmod 600 /swapfile
    sudo mkswap /swapfile
    sudo swapon /swapfile
    # Make swap persistent across reboots
    echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
  3. Configuration Check Edit your Nginx primary configuration file, typically /etc/nginx/nginx.conf, or a specific site configuration file within /etc/nginx/conf.d/. Adjust the worker_processes directive to reduce the total memory footprint.

    # Open the Nginx main configuration file
    sudo vi /etc/nginx/nginx.conf
    
    # Locate and modify the 'worker_processes' directive.
    # Reduce 'worker_processes' if it's set too high (e.g., from 'auto' to 1 or 2 for small instances).
    # Each worker process consumes memory; fewer workers mean less overall memory usage.
    # A general guideline is 1 worker per CPU core, but for OOM on small EC2, reducing it is key.
    worker_processes 1; # Adjust to 1 or 2 for small EC2 instances if you're hitting OOM
    
    # (Optional, for reverse proxy setups) Adjust proxy buffer sizes.
    # These settings control how much memory Nginx allocates for buffering responses from upstream servers.
    # If not explicitly set, Nginx uses defaults. High traffic or large responses with large buffers can lead to OOM.
    # Place these inside http, server, or location blocks as appropriate.
    # proxy_buffer_size   4k;
    # proxy_buffers       4 8k;
    # proxy_busy_buffers_size 8k;
    # Resetting to lower values or Nginx defaults for very small instances can help.
    
    # After changes, save the file and exit the editor.

    Apply the new Nginx configuration:

    sudo nginx -t
    sudo systemctl reload nginx
    # OR for older systems:
    # sudo service nginx reload
  4. Verification Confirm Nginx is running with the new configuration and monitor memory usage.

    # Check Nginx service status
    sudo systemctl status nginx
    
    # Monitor Nginx process memory usage
    ps aux | grep nginx | grep -v grep
    
    # Observe overall memory usage (especially after applying load)
    top
    # Or htop (if installed)
    # htop
    
    # Check Nginx error logs for any new issues
    sudo tail -f /var/log/nginx/error.log