Facebook Pixel

Installing and Configuring Nginx on Ubuntu for Node.js Deployment

Learn how to install Nginx on Ubuntu, understand the directory structure, create server blocks, and configure it as a reverse proxy for your Node.js application.

Installing and Configuring Nginx on Ubuntu

This guide covers installing Nginx on Ubuntu, understanding its directory structure, and configuring it as a reverse proxy for Node.js.

Step 1: Install Nginx

sudo apt update sudo apt install -y nginx

Verify:

nginx -v # nginx version: nginx/1.18.0 (Ubuntu)

Step 2: Start and Enable Nginx

sudo systemctl start nginx sudo systemctl enable nginx sudo systemctl status nginx

You should see active (running).

Step 3: Test Nginx

Visit http://your-ec2-public-ip in a browser. You should see the "Welcome to nginx!" page.

Step 4: Nginx Directory Structure

/etc/nginx/
├── nginx.conf          # Main config file
├── sites-available/    # Available site configs
│   └── default
├── sites-enabled/      # Enabled sites (symlinks to sites-available)
│   └── default -> ../sites-available/default
├── conf.d/             # Additional configuration files
└── mime.types          # MIME type definitions

Key files:

  • nginx.conf Global settings (worker processes, logging, etc.)
  • sites-available/ All site configurations (create files here)
  • sites-enabled/ Symlinks to enabled sites (Nginx serves these)

Step 5: Create a Server Block

sudo nano /etc/nginx/sites-available/devtinder

Add this configuration:

server { listen 80; server_name yourdomain.com; # or EC2 public IP location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; 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; proxy_cache_bypass $http_upgrade; } }

Step 6: Enable the Site

# Create symlink to sites-enabled sudo ln -s /etc/nginx/sites-available/devtinder /etc/nginx/sites-enabled/ # Remove default site sudo rm /etc/nginx/sites-enabled/default # Test configuration sudo nginx -t # nginx: configuration file /etc/nginx/nginx.conf test is successful # Restart Nginx sudo systemctl restart nginx

Step 7: Verify the Proxy

Make sure your Node.js app is running on port 3000:

pm2 status # devtinder-api should be online

Test from your local machine:

curl http://your-ec2-public-ip/api/health

You should get your API response. Nginx is now proxying traffic to Node.js.

Understanding the Configuration

server block Like a virtual host. Defines how Nginx handles requests for a specific domain.

listen 80 Listen on port 80 (HTTP).

server_name The domain name or IP that this server block handles.

location / How to handle all request paths.

proxy_pass Where to forward requests (the Node.js app on port 3000).

proxy_set_header Passes information from the original request to Node.js (host, IP, protocol).

Common Nginx Commands

# Test config sudo nginx -t # Reload config (no downtime) sudo systemctl reload nginx # Restart (brief downtime) sudo systemctl restart nginx # Stop sudo systemctl stop nginx # Check status sudo systemctl status nginx # View Nginx error log sudo tail -f /var/log/nginx/error.log

The Takeaway

Installing Nginx on Ubuntu is straightforward with apt. The key is understanding the directory structure: create configs in sites-available, symlink to sites-enabled, test with nginx -t, and restart. The server block configuration with proxy_pass, proxy_set_header, and WebSocket headers is the foundation for deploying Node.js with Nginx.

Run sudo apt update and sudo apt install -y nginx. Start with sudo systemctl start nginx, enable with sudo systemctl enable nginx. Verify by visiting http://your-ip in a browser to see the welcome page.

nginx.conf is the main config. sites-available/ contains all site configs. sites-enabled/ contains symlinks to enabled sites. conf.d/ has additional configs. Create configs in sites-available, symlink to sites-enabled, remove the default, test, and restart.

Create a server block with listen 80, server_name your domain, and a location / block with proxy_pass http://localhost:3000. Add proxy_set_header directives for Host, X-Real-IP, X-Forwarded-For, X-Forwarded-Proto, and Upgrade/Connection headers for WebSocket support.

Create the config in /etc/nginx/sites-available/, create a symlink to /etc/nginx/sites-enabled/ with ln -s, remove the default site, test with sudo nginx -t, and restart with sudo systemctl restart nginx.

Reload (sudo systemctl reload nginx) applies new config without dropping connections (no downtime). Restart (sudo systemctl restart nginx) stops and starts Nginx (brief downtime). Use reload for config changes in production.

Ready to master Node.js completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.