Skip to main content

Command Palette

Search for a command to run...

Nginx: From Setup to Hosting Websites

Published
4 min read
Nginx: From Setup to Hosting Websites
K
👋 Hi, I'm Khushi Dubey, a passionate final year BCA student diving deep into the world of Cloud & DevOps. My journey is focused on the intersection of cloud technologies and cutting-edge DevOps methodologies to drive innovation and transformation. 🌍 Here’s a snapshot of who I am: 🚀 Aspiring Cloud/DevOps Professional: Passionate about streamlining processes, automating workflows, and optimizing cloud infrastructures. 💻 Tech Enthusiast: Constantly exploring new tools, trends, and techniques in cloud computing and DevOps engineering. 🎯 Problem-Solver: Dedicated to finding practical and scalable solutions to real-world challenges. ✍️ Lifelong Learner: Continuously improving my skill set to keep pace with evolving technologies and industry demands. 🌐 Collaborator: Believer in the power of teamwork and open knowledge sharing to build a thriving tech community. 📚 Follow my journey as I share insights, tutorials, and hands-on experiences from my Cloud and DevOps

What is Nginx?

Nginx is a powerful web server used to serve websites and applications. It’s fast, lightweight, and can handle a lot of traffic at once. It’s also used as a reverse proxy, load balancer, and more.


Step 1: Setting Up Nginx on AWS EC2

Creating an AWS EC2 Instance

  1. Sign in to AWS: Go to the AWS Management Console.

  2. Launch an EC2 Instance:

    • Click on "EC2" under the "Services" menu.

    • Click "Launch Instance."

    • Choose an Amazon Machine Image (AMI) like Ubuntu or Amazon Linux.

    • Select an instance type (e.g., t2.micro for free tier).

    • Configure instance details (default settings are fine for beginners).

    • Add storage (default is 8GB, which is enough for starters).

    • Add tags (optional) and configure security groups (allow HTTP, HTTPS, and SSH).

    • Review and launch the instance.

  3. Connect to Your Instance:

    • Use SSH to connect to your EC2 instance. For example:

      bash

      Copy

        ssh -i your-key.pem ubuntu@your-ec2-public-ip
      

Installing Nginx

Once you’re connected to your EC2 instance, install Nginx:

bash

Copy

sudo apt update
sudo apt install nginx

Start Nginx and enable it to run on boot:

bash

Copy

sudo systemctl start nginx
sudo systemctl enable nginx

Check if Nginx is running by visiting your server’s public IP in a browser. You should see the default Nginx welcome page.


Step 2: Nginx Configuration Overview

Nginx uses configuration files to control how it works. The main configuration file is located at /etc/nginx/nginx.conf. However, for hosting websites, we use server blocks (similar to virtual hosts in Apache).


Step 3: Hosting a Custom Static Website

  1. Create a Website Directory:

    bash

    Copy

     sudo mkdir -p /var/www/mywebsite/html
    
  2. Add Your Website Files:
    Upload your HTML, CSS, and JavaScript files to the /var/www/mywebsite/html directory.

  3. Create a Server Block:
    Create a new configuration file for your website:

    bash

    Copy

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

    Add the following:

    nginx

    Copy

     server {
         listen 80;
         server_name your-domain-or-ip;
    
         root /var/www/mywebsite/html;
         index index.html;
    
         location / {
             try_files $uri $uri/ =404;
         }
     }
    
  4. Enable the Configuration:
    Create a symbolic link to enable the site:

    bash

    Copy

     sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/
    
  5. Test and Reload Nginx:
    Test the configuration for errors:

    bash

    Copy

     sudo nginx -t
    

    Reload Nginx:

    bash

    Copy

     sudo systemctl reload nginx
    

    Now, your website should be live!


Step 4: Changing the Default HTTP Port

By default, Nginx listens on port 80 for HTTP traffic. To change it:

  1. Edit the Nginx configuration file:

    bash

    Copy

     sudo nano /etc/nginx/sites-available/mywebsite
    
  2. Change the listen directive:

    nginx

    Copy

     listen 8080;
    
  3. Reload Nginx:

    bash

    Copy

     sudo systemctl reload nginx
    

Now, your website will be accessible on port 8080.


Step 5: Nginx Logging

Nginx logs access and error messages:

  • Access Logs: Located at /var/log/nginx/access.log. Tracks all requests to your server.

  • Error Logs: Located at /var/log/nginx/error.log. Records errors encountered by Nginx.

You can customize logging in your server block:

nginx

Copy

access_log /var/log/nginx/mywebsite_access.log;
error_log /var/log/nginx/mywebsite_error.log;

Step 6: Hosting Multiple Websites with Nginx

To host multiple websites, create separate server blocks for each site. For example:

nginx

Copy

server {
    listen 80;
    server_name website1.com;
    root /var/www/website1/html;
    ...
}

server {
    listen 80;
    server_name website2.com;
    root /var/www/website2/html;
    ...
}

Enable each configuration and reload Nginx.


Step 7: Understanding MIME Types

MIME types tell browsers how to handle different file types (e.g., HTML, CSS, images). Nginx automatically detects MIME types, but you can customize them:

nginx

Copy

types {
    text/html html;
    text/css css;
    image/png png;
}

Step 8: Using a Custom Domain with Nginx

  1. Buy a Domain: Purchase a domain from a registrar like GoDaddy or Namecheap.

  2. Point Domain to Your Server: Update the DNS settings to point to your EC2 instance’s public IP.

  3. Update Nginx Configuration:
    Replace server_name your-domain-or-ip; with your custom domain:

    nginx

    Copy

     server_name yourdomain.com;
    

Step 9: Configuring Error Pages and Logs

To create custom error pages:

nginx

Copy

error_page 404 /404.html;
location = /404.html {
    root /var/www/mywebsite/html;
}

Step 10: Nginx HTTPS and SSL

Secure your website with HTTPS using Let’s Encrypt:

  1. Install Certbot:

    bash

    Copy

     sudo apt install certbot python3-certbot-nginx
    
  2. Obtain an SSL Certificate:

    bash

    Copy

     sudo certbot --nginx -d yourdomain.com
    
  3. Certbot will automatically update your Nginx configuration to use HTTPS.


Step 11: Nginx Reverse Proxy

A reverse proxy forwards client requests to backend servers. Example:

nginx

Copy

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://localhost:3000; # Forward to a Node.js app
    }
}

Step 12: Nginx Load Balancing

Distribute traffic across multiple servers:

nginx

Copy

upstream backend {
    server 192.168.1.101;
    server 192.168.1.102;
}

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://backend;
    }
}

Step 13: Nginx Backup Server

Set up a backup server for high availability:

nginx

Copy

upstream backend {
    server 192.168.1.101;
    server 192.168.1.102 backup; # This server is used only if the primary fails
}

Linkedin: https://www.linkedin.com/in/khushi-dubey-6036a2305

Thank You!