Create Virtual Host with NGINX Server

Introduction

In modern web management, subdomains are an effective way to separate specific services or features from the main domain. For example, you might create blog.example.com for blog content or app.example.com for a web application. By leveraging NGINX as a web server, configuring subdomains becomes both flexible and efficient.

NGINX is widely recognized for its lightweight architecture, high performance, and robust reverse proxy capabilities. When hosting subdomains, NGINX allows you to:

  • Direct subdomains to different directories or applications.
  • Configure virtual hosts for each subdomain.
  • Optimize performance and security through proper configuration.

This guide will walk you through the process of creating a subdomain using NGINX, covering DNS preparation, server block configuration, and testing to ensure the subdomain is accessible and functioning correctly.

Topology Explanation

1. Access Flow

  • Internet → NGINX Server

    All incoming requests from visitors are routed to the NGINX server, which acts as both a reverse proxy and a web server.

  • Public DNS

    Subdomains such as portfolio.mursidin.net and project.mursidin.net are resolved by public DNS to the server’s IP address.

2. NGINX Server

Primary Role
Handles HTTP (port 80) and HTTPS (port 443) requests and performs routing based on server blocks (virtual hosts).

  • Server Block Structure
    • portfolio.mursidin.net → root directory: /var/www/portfolio.mursidin.net
    • project.mursidin.net → root directory: /var/www/project.mursidin.net
  • Directory Configuration
    Each root directory must contain an index.html or index.php file or the appropriate web application files.

Initial Preparation

Prerequisites

  • Root or sudo access to the server
  • NGINX installed and properly configured
  • Active domain or subdomain pointing to the server’s IP address
  • Certbot installed for SSL certificate management

Creating the Web Directory

Configuration File

Create a dedicated server block for the subdomain portfolio.mursidin.net:

Contents of file configurations:

Create a dedicated server block for the subdomain project.mursidin.net:

Activate the virtual host

Test and reload

Add DNS Record

Note : xxx.xxx.xxx.xxx is your ip DNS public

Test and Validation

Open the web browser and access https://portfolio.mursidin.net

Also access web https://project.mursidin.net/

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *