DEV Community

Oluwajubelo
Oluwajubelo

Posted on

7

Nginx for Beginners

What is Nginx?

Nginx (pronounced "Engine-X") is a powerful, open-source web server software that can also function as a reverse proxy, load balancer, and HTTP cache. It’s known for its high performance, stability, and low resource usage, making it a popular choice for hosting websites and web applications.

Why Use Nginx?

  • Speed: Nginx is designed to handle many simultaneous connections efficiently.

  • Scalability: It can manage high-traffic websites with ease.

  • Flexibility: Works as a web server, reverse proxy, or load balancer.

  • Simplicity: Configuration files are straightforward and easy to understand.

  • Community: A large community and plenty of documentation are available.

Basic Concepts

Web Server

Nginx serves static content (like HTML, CSS, and images) to users’ browsers. It can also handle dynamic content by passing requests to application servers (e.g., those running PHP, Python, or Node.js).

Reverse Proxy

As a reverse proxy, Nginx forwards client requests to backend servers, distributing the load and improving security by hiding the backend infrastructure.

Load Balancer

Nginx can distribute incoming traffic across multiple servers to prevent any single server from becoming overwhelmed, ensuring reliability.

Configuration

Nginx uses a simple, text-based configuration file (usually nginx.conf) to define how it handles requests. This file is typically located in /etc/nginx/ on Linux systems.

Getting Started with Nginx

Installation

Here’s how to install Nginx on a Linux system (Ubuntu/Debian):

1. Update the package list:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

2. Install Nginx:

sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode

3. Start Nginx and enable it to run on boot:

sudo systemctl start nginx
sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

4. Verify Nginx is running by visiting http://your_server_ip in a browser. You should see the default Nginx welcome page.

Basic Configuration

The main configuration file is /etc/nginx/nginx.conf. A simple configuration might look like this:

http {
    server {
        listen 80;
        server_name example.com;

        location / {
            root /var/www/html;
            index index.html;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  • listen 80: Nginx listens for HTTP requests on port 80.

  • server_name: Specifies the domain name (e.g., example.com).

  • location /: Defines how to handle requests to the root URL.

  • root: Sets the directory where website files are stored.

  • index: Specifies the default file to serve (e.g., index.html).

Serving a Simple Website

1. Create a directory for your website:

sudo mkdir -p /var/www/mywebsite
Enter fullscreen mode Exit fullscreen mode

2. Create a basic index.html file:

echo "<h1>Hello from Nginx!</h1>" | sudo tee /var/www/mywebsite/index.html
Enter fullscreen mode Exit fullscreen mode

3. Update the Nginx configuration (/etc/nginx/sites-available/mywebsite):

server {
    listen 80;
    server_name mywebsite.com;

    root /var/www/mywebsite;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }
}
Enter fullscreen mode Exit fullscreen mode

4. Enable the site by linking it to sites-enabled:

sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode

5. Test the configuration for errors:

sudo nginx -t 
Enter fullscreen mode Exit fullscreen mode

6. Reload Nginx to apply changes:

sudo systemctl reload nginx 
Enter fullscreen mode Exit fullscreen mode

7. Visit http://your_server_ip to see your “Hello from Nginx!” page.

Common Commands

  • Start Nginx: sudo systemctl start nginx

  • Stop Nginx: sudo systemctl stop nginx

  • Restart Nginx: sudo systemctl restart nginx

  • Reload Configuration: sudo systemctl reload nginx

  • Check Status: sudo systemctl status nginx

  • Test Configuration: sudo nginx -t

Heroku

The AI PaaS for deploying, managing, and scaling apps.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Get Started

Top comments (7)

Collapse
 
dannykinz profile image
Daniel Ayo Akinleye

This article really helped me get started with Nginx. I love how practical and beginner-friendly the steps are, especially the part on setting up a simple website. Clear, straight to the point, and easy to follow. Great work!

Collapse
 
oluwajubelo1 profile image
Oluwajubelo

Thanks for your kind words. I'm glad this article is of help to you.

Collapse
 
jeremiah_tomiwaabolaji_7 profile image
Jeremiah Tomiwa Abolaji

This is a very enlightening piece. Thank you for the very detailed explanation, it was really helpful.

Collapse
 
oluwajubelo1 profile image
Oluwajubelo

I'm glad you found it helpful

Collapse
 
emmanueliyanu21 profile image
Emmanuel

I love this article. It helps me understand the basis of Nginx with relatable examples

Collapse
 
oluwajubelo1 profile image
Oluwajubelo

Excited to hear this. Keep learning!!!

Collapse
 
highshirt99 profile image
Aishat Akinyemi

Thanks for answering some of the questions I had about Nginx.This is an insightful one!

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM apps—no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

👋 Kindness is contagious

Embark on this engaging article, highly regarded by the DEV Community. Whether you're a newcomer or a seasoned pro, your contributions help us grow together.

A heartfelt "thank you" can make someone’s day—drop your kudos below!

On DEV, sharing insights ignites innovation and strengthens our bonds. If this post resonated with you, a quick note of appreciation goes a long way.

Get Started