Nginx: From Setup to Hosting Websites

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
Sign in to AWS: Go to the AWS Management Console.
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.
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
Create a Website Directory:
bash
Copy
sudo mkdir -p /var/www/mywebsite/htmlAdd Your Website Files:
Upload your HTML, CSS, and JavaScript files to the/var/www/mywebsite/htmldirectory.Create a Server Block:
Create a new configuration file for your website:bash
Copy
sudo nano /etc/nginx/sites-available/mywebsiteAdd 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; } }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/Test and Reload Nginx:
Test the configuration for errors:bash
Copy
sudo nginx -tReload Nginx:
bash
Copy
sudo systemctl reload nginxNow, 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:
Edit the Nginx configuration file:
bash
Copy
sudo nano /etc/nginx/sites-available/mywebsiteChange the
listendirective:nginx
Copy
listen 8080;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
Buy a Domain: Purchase a domain from a registrar like GoDaddy or Namecheap.
Point Domain to Your Server: Update the DNS settings to point to your EC2 instance’s public IP.
Update Nginx Configuration:
Replaceserver_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:
Install Certbot:
bash
Copy
sudo apt install certbot python3-certbot-nginxObtain an SSL Certificate:
bash
Copy
sudo certbot --nginx -d yourdomain.comCertbot 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
}



