# Nginx Domain Resolution

This article introduces the basics of Nginx and best practices for domain resolution. It also demonstrates how to configure domain resolution using [`www.example.pub`](http://www.example.pub) as an example.

---

### **Background Knowledge**

#### **Nginx**

Nginx is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3 mail proxy server. Due to its lightweight nature, high concurrency performance, and flexible configuration, Nginx has become a preferred tool for domain and site management in modern web services.

In terms of domain configuration, Nginx offers powerful flexibility and scalability, suitable for various scenarios:

**Common Use Cases:**

1. **Single Domain Configuration**:
    
    * Supports pointing a single domain to a specific site on the server (e.g., [`www.example.com`](http://www.example.com)).
        
    * Can be combined with HTTP and HTTPS configurations to provide secure and efficient access.
        
2. **Multiple Domain Configuration**:
    
    * Provides services for multiple domains simultaneously, with each domain managed separately via independent configuration files or virtual hosts (e.g., [`www.example.com`](http://www.example.com) and [`api.example.com`](http://api.example.com)).
        
3. **Subdomain Support**:
    
    * Uses subdomains (e.g., [`blog.example.com`](http://blog.example.com) or [`shop.example.com`](http://shop.example.com)) to distinguish different business modules or applications, allowing separate configurations for each subdomain.
        
4. **Domain Forwarding and Redirection**:
    
    * Enables redirection between different domains, such as automatically redirecting [`example.com`](http://example.com) to [`www.example.com`](http://www.example.com).
        
    * Configures global HTTP-to-HTTPS secure redirection.
        
5. **Reverse Proxy and Load Balancing**:
    
    * Directs domain traffic to backend services or distributed server clusters to enhance performance and availability.
        
6. **Multi-Site Management**:
    
    * Supports managing multiple sites through independent configuration files (virtual hosts), with different domains, directory paths, and access rules for each site.
        

**Key Features of Nginx in Domain Configuration**:

* **Flexibility**: Implements precise domain matching and routing through the `server` directive.
    
* **Modular Configuration**: Separates main and sub-site configurations for easier expansion and maintenance.
    
* **High Performance**: Supports domain resolution caching and efficient TCP connection reuse, optimizing resolution and access speeds.
    
* **Strong Compatibility**: Works with various protocols (e.g., HTTP, HTTPS) and complex deployment scenarios (e.g., reverse proxy, load balancing, API gateway).
    

---

### **Environment**

* **Operating System**: Debian (or Debian-based distributions like Ubuntu)
    
* **Software Version**: Latest stable version of Nginx
    

---

### **Best Practices for Domain Resolution**

To ensure flexible and maintainable site management, follow these best practices for domain resolution and Nginx configuration:

1. **Separate Configurations for Main and Sub-Sites**:
    
    * Store each domain's configuration file separately in `/etc/nginx/sites-available/` (e.g., [`www.example.pub`](http://www.example.pub) and [`nav.example.pub`](http://nav.example.pub)).
        
    * This prevents configuration overlap between sites, making maintenance and troubleshooting easier.
        
2. **Domain Resolution Setup**:
    
    * On your domain registrar's management platform, add the following resolution records:
        
        * **Sub-Site (Subdomain)**: Point [`nav.example.pub`](http://nav.example.pub) to your server's IP address.
            
        * **Main Site (Top-Level Domain)**: Point [`www.example.pub`](http://www.example.pub) to your server's IP address.
            
3. **Enable Site Isolation in Nginx**:
    
    * Store each site configuration file in `/etc/nginx/sites-available/` and enable it using a symbolic link to `/etc/nginx/sites-enabled/`.
        
        * **Sub-Site Configuration File**: `nav.example.pub.conf`
            
        * **Main Site Configuration File**: `www.example.pub.conf`
            

---

### **1\. Install Nginx**

#### **1.1 Update System Packages**

```bash
sudo apt update
```

#### **1.2 Install Nginx**

```bash
sudo apt install nginx -y
```

#### **1.3 Start and Check Service Status**

```bash
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx
```

#### **1.4 Verify Installation**

* Open your browser and visit the server's IP address (e.g., `http://<your-server-ip>`). If the Nginx welcome page appears, the installation was successful.
    

---

### **2\. Edit Nginx Configuration Files**

The main configuration file is located at `/etc/nginx/nginx.conf`, and site-specific configuration files are typically stored in `/etc/nginx/sites-available/`.

#### **2.1 Create a Top-Level Domain Configuration**

Create the `/etc/nginx/sites-available/nav.example.pub.conf` configuration file:

```nginx
server {
    listen 80;
    server_name www.example.pub;
    root /var/www/html;

    location / {
        index index.html index.htm;
    }

    error_page 404 /404.html;
}
```

**Steps**:

1. Create the configuration file:
    
    ```bash
    sudo vim /etc/nginx/sites-available/www.example.pub.conf
    ```
    
2. Create the site root directory and add a homepage:
    
    ```bash
    sudo mkdir -p /var/www/html
    echo "<h1>Welcome to www.example.pub</h1>" | sudo tee /var/www/html/index.html
    ```
    
3. Enable the site configuration and reload Nginx:
    
    ```bash
    sudo ln -s /etc/nginx/sites-available/www.example.pub.conf /etc/nginx/sites-enabled/
    sudo nginx -t  # Check configuration for errors
    sudo systemctl reload nginx
    ```
    

---

#### **2.2 Create a Subdomain Configuration**

Create the configuration file:

`/etc/nginx/sites-available/nav.example.pub.conf`

```nginx
server {
    listen 80;
    server_name nav.example.pub;
    root /var/www/nav;

    location / {
        proxy_pass http://127.0.0.1:8080; # Forward requests to the backend service
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
```

**Steps**:

1. Create the configuration file:
    
    ```bash
    sudo vim /etc/nginx/sites-available/nav.example.pub.conf
    ```
    
2. Forward requests to a backend service (e.g., running at [`http://127.0.0.1:8080`](http://127.0.0.1:8080)).
    
3. Enable the site configuration and reload Nginx:
    
    ```bash
    sudo ln -s /etc/nginx/sites-available/nav.example.pub.conf /etc/nginx/sites-enabled/
    sudo nginx -t  # Check configuration for errors
    sudo systemctl reload nginx
    ```
    

---

### **Conclusion**

By following the steps outlined above, you can deploy and manage multi-level domain services efficiently with Nginx. The approach ensures a modular, flexible, and high-performance setup suitable for modern web applications.

**Collaboratively edited with ChatGPT for educational and discussion purposes.**  
© Helianthus. Please credit the source when sharing.
