Skip to content

Nginx Installation & Config Guide

Nginx (engine x) is a high-performance HTTP and reverse proxy web server. It is known for its stability, rich feature set, simple configuration files, and low system resource consumption.

Web Server

Nginx Installation

Linux (Ubuntu / Debian)

Install using apt package manager:

bash
sudo apt update
sudo apt install nginx -y

Check service status after installation:

bash
systemctl status nginx

Linux (CentOS / RHEL)

Install using yum (you may need to install EPEL repository first):

bash
sudo yum install epel-release -y
sudo yum install nginx -y

Start Nginx and enable on boot:

bash
sudo systemctl start nginx
sudo systemctl enable nginx

Windows

  1. Go to Nginx Official Download Page and download the Stable version zip file.
  2. Unzip the downloaded file to any directory (e.g., C:\nginx).
  3. Open Command Prompt (CMD) or PowerShell and navigate to that directory.
  4. Run start nginx to start the service.

API Command Cheat Sheet

CommandDescription
nginx -tTest configuration file for syntax errors
nginx -s reloadReload configuration (without stopping service)
nginx -s stopFast stop service
nginx -s quitGraceful stop (stop after processing current requests)

Basic Configuration Examples

The main configuration file for Nginx is usually located at /etc/nginx/nginx.conf. It is recommended to create new .conf files in the /etc/nginx/conf.d/ directory to manage different sites.

1. Hosting a Static Website

Create a file named website.conf:

nginx
server {
    listen 80;
    server_name example.com; # Your domain or IP

    root /var/www/html/my-website; # Website root directory
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}

2. Reverse Proxy

Reverse proxies are commonly used to forward requests to backend application servers (like Node.js, Python, Go, etc.).

nginx
server {
    listen 80;
    server_name api.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000; # Forward to local port 3000
        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;
    }
}

All resources are from the open-source community. Disclaimer