NGINX Subdomain Tutorial & Subdomain Setup Guide

NGINX Subdomain Tutorial and Setup Guide

 

Nginx (engine-ex) is an open-source web server that allows you to host multiple domains and subdomains using a single IP address. It can deliver content quickly and reliably, which is why it’s a popular choice for improving the performance, security, and resilience of web servers and applications.

This article delves into how to set up the Nginx subdomain and multiple domains allowing you to optimize your web infrastructure efficiently.

Looking for Recommendations on Shared Hosting?

ProviderUser RatingDiscountsExpert & User Reviews
4.610% OffHostinger ReviewVisit Hostinger
5.075% OffHostArmada ReviewVisit HostArmada
5.075% OffChemiCloud ReviewVisit ChemiCloud

Takeaways
  • NGINX is an open-source web server and reverse proxy server initially created to solve the C10K problem
  • Key concepts of NGINX for subdomains include subdomain routing, subdomain proxy, subdomain reverse proxy, and subdomain wildcard
  • Many large companies use NGINX for its scalability, including Netflix, Hulu, Facebook, and Pinterest
  • NGINX subdomain may not work for several reasons such as DNS resolution issues, misconfigured server blocks, and others

How to Use NGINX for Subdomains: Key Concepts

NGINX is also popular because it can be used with multiple subdomains. Below we explored the key concepts of using NGINX for subdomains.

What Is NGINX?

NGINX is a high-performance, open-source web server, and reverse proxy server software. Its main features include high speed, scalability, and efficiency in handling web traffic.

Originally, NGINX was created to solve the “C10k problem” (handling thousands of concurrent connections) but it gained popularity, and today, it excels in serving static content, load balancing, and acting as a reverse proxy for web applications. You can also configure NGINX to enable SSL and listen on port 443 for added security.

NGINX is widely used on websites and web applications because of its great speed.

NGINX Subdomain Routing

NGINX subdomain routing involves directing incoming web traffic to different locations on your server, based on the subdomain portion of the URL.

For example, if your visitors want to visit your blog on blog.example.com, they’ll be routed to the blog section of your server. NGINX subdomain routing provides a clear path for users to access specific content or services.

NGINX Subdomain Proxy

NGINX subdomain proxy means that you’ll use NGINX as the middle-ground server. It will receive HTTP or HTTPS requests for a subdomain and then forward the request to another server or application. Simply put, a subdomain proxy acts like a receptionist who redirects visitors to different destinations on your website or application.

If your website has a subdomain called support.example.com, it could be proxied to your customer support system.

NGINX Subdomain Reverse Proxy

NGINX subdomain reverse proxy involves using NGINX to receive requests for a subdomain and then reverse proxying them to multiple backend servers or applications based on subdomains.

Think of it as a central hub that distributes incoming traffic to different departments within your organization, each identified by a unique subdomain, like sales.example.com or marketing.example.com.

NGINX Subdomain Wildcard

NGINX subdomain wildcard is a feature that allows you to catch all subdomain requests under a domain and route them to a specific location or application.

It’s like setting up a catch-all mailbox for all subdomains, ensuring that any subdomain not explicitly configured still lands somewhere useful. For example, *.example.com can route all unspecified subdomains to a default landing page or application.

Can NGINX Handle Multiple Domains?

NGINX can handle multiple domains efficiently. NGINX can serve as a server and reverse proxy for multiple domains simultaneously. This is often referred to as virtual hosting or server block configuration.

Each domain can have its own server block or configuration file where you define how NGINX should handle requests for that specific domain.

NGINX can route incoming traffic to the appropriate web applications or serve different websites based on the requested domain name.

This feature allows you to set up multiple domains on a single server efficiently and manage them independently, making NGINX a versatile choice for hosting multiple websites or applications on a single server.

Shared Hosting Plans with Hostinger
Hostinger has several affordable high-quality shared web hosting plans. Starting with “Single Web Hosting” plan, dedicated for one-site projects and ending with a “Business Web Hosting” plan for multi-site, high volume traffic and quickly growing businesses. Hostinger is known for providing Premium product and service for one of the lowest prices in the industry. There are plenty of quality features, high-speed performance, and brilliant live support. Definitely worth a try.
Visit Hostinger

NGINX Subdomain Examples

NGINX uses a scalable, asynchronous architecture that allows it to handle thousands of simultaneous requests without using too much memory. That’s why so many large companies trust it to serve multiple domains. Some of the companies that use NGINX include Netflix, Hulu, Pinterest, and others.

How to Add a Subdomain or Multiple Domains in NGINX

This guide will help you with the configuring nginx process. Make sure to install NGINX on your system before starting the process. Otherwise, you won’t be able to create NGINX server block.

Set Up DNS Records

  • Log in to your DNS management dashboard or DNS control panel
  • Click on “Add Record,” usually located next to the root domain or subdomain you want to add the record for
  • Choose “A record” from the list and configure your DNS settings
  • Fill in the Host field with “@” and the Value field with your server IP
  • Enter the TTL (Time to Live) field and save your changes by clicking “Confirm” or “Add new record”
  • Repeat these steps for each subdomain you want to add

Set Up Web Directories and NGINX Configuration File

The first step is complete and now it’s time to create a subdomain directory. NGINX is located in a default virtual host file and serves a web directory located at /usr/share/nginx/html.

In the next guide, you’ll create a web directory for each domain and subdomain name inside the NGINX document root (/var/www/html.)

Step 1: Navigate to your terminal and run the commands in the screenshot below. That way, you’ll create web directories for all subdomains and multiple domains you want to add in NGINX.

sudo mkdir /var/www/html/mywebsite.com
sudo mkdir /var/www/html/web1.mywebsite.com
sudo mkdir /var/www/html/web2.mywebsite.com

Step 2: Run the “chown” command. This command will recursively (-R) alter the ownership of each directory you created earlier to www-data user and group.

This step is necessary because NGINX functions as a www-data user.

Step 3: Open a coding editor of choice and create a file named index.html.

Type in the following code in the image below and save the file inside the primary web directory of your domain (/var/www/html/mywebsite.com.)

<html>
     <head>
         <title>Welcome to mywebsite.com!</title>
     </head>
         <body>
            <h1>Hello, mywebsite.com is up and running!</h1>
         </body>
</html>

This HTML code will display a simple message that confirms that the website is working once you open the file in a web browser.

Step 4: Create the same index.html file in the following directories

/var/www/html/web1.mywebsite.com

/var/www/html/web2.mywebsite.com

but modify the body of the HTML file to match that of the subdomains web1.mywebsite.com and web2.mywebsite.com.

Create an NGINX Server Block for Each Subdomain or Multiple Domains

Now, you need to create an NGINX virtual host configuration file (server block) for each subdomain and then serve the HTML pages. The steps to create NGINX server block are as follows:

Step 1: Create an NGINX virtual host configuration file named mywebsite.com in your code editor and then copy and paste the following code.

After that, save the file in the /etc/nginx/sites-available/ directory.

The following lines show how the file should look like.

server {
        listen 80;
        root /var/www/html/mywebsite.com;
        index index.html index.html
       server_name mywebsite.com;
       location /{
           try_files $uri $uri/ =404;
       }
     }

  • Step 2: Run the nginx command and make sure to check (-t) in the NGINX configuration file hit enter to check for any syntax error.
  • Step 3: The following command will create a symbolic link (ln -s) from the /etc/nginx/sites-available to the /etc/nginx/sites-enabled/ directory. That way mywebsite.com virtual host configuration file is enabled.
  • Step 4: Repeat steps 1-3 to create NGINX server blocks for web1.mywebsite.com and web2.mywebsite.com. Make sure to replace the line root/var/www/html/mywebsite.com with the webroot directory for each subdomain.

Also, make sure to replace the line server_name mywebsite.com with the name of each subdomain you’ll point to that domain.

  • Step 5: Next, run the sudo ln commands so the virtual host configuration files will be enabled.
  • Step 6: Run the systemctl restart nginx command to restart the NGINX service so that all the configuration changes may apply.

Install SSL Certificates for NGINX Subdomains and Domains

The next step is to add SSL certificate for NGINX subdomains and domains.

  • Step 1: Run sudo apt-get install certbot python3-certbot-nginx -y to install the Certbot software package. Certbot is used to let you download an SSL certificate for your subdomains.
  • Step 2: Run sudo certbot ceronly –agree-tos –email myemail@email.com -d mywebsite.com.
  • Step 3: Enter ‘1’ which will select the NGINX Web Server plugin (nginx) to authenticate with the ACME CA. Wait for the download process to conclude. Let’s Encrypt is a free SSL certificate service and certificate authority. The SSL certificates will be stored in the /etc/letsencrypt/live/ directory.
  • Step 4: Repeat processes 1-3 to download and install SSL certificates for the other subdomains you use.
  • Step 5: Run ls /etc/letsencrypt/live/*mywebsite.com to list all SSL certificates.

Access the Subdomains or Domains

To test whether you added subdomains or multiple domains with NGINX, navigate to your browser type in the domain or subdomains’ URLs, and see whether the websites load. If the text from your index.html file is displayed, the websites are serving.

NGINX Subdomain Not Working: What to Do?

NGINX subdomain could not be served due to several reasons. We’ll discuss them below, as well as potential solutions to these problems.

  • Misconfigured Server Block: Your NGINX server block configuration for the subdomain could contain some errors. Double-check the configuration for syntax errors and make sure that the server_name directive accurately reflects the subdomain.
  • DNS Resolution Issues: DNS entries for the subdomain might not be set up correctly or have not been propagated yet. Make sure that the DNS records for the subdomain point to the correct server’s IP address and allow time for DNS propagation.
  • Missing Document Root or Content: The document root may be misconfigured. Make sure that the root directive in the server block points to the correct directory that contains the subdomain’s web content.
  • Network Issues: Firewall rules or network issues could be preventing access to the subdomain. Check your server’s firewall settings. Ensure that the traffic to the subdomain’s port is allowed. The subdomain ports in use are usually 80 or 443.
  • SSL/TLS Certificate Mismatch: SSL/TLS certificates for the subdomain might not match the requested domain. Make sure that SSL configuration is properly set up.
  • Conflicting Server Blocks: Conflicting server block configurations may interfere with subdomain routing. Review your NGINX configuration files for potential conflicts or overlapping server blocks.

Final Word

We hope that this guide helped you learn more about what NGINX is and how to use NGINX for adding subdomains and multiple domains.

If you’re looking to build a website from scratch, check out our comparison of the best website builders and choose the best website hosting for your needs.

Next Steps: What Now?

Learn More About Subdomains

Frequently Asked Questions

How do I add a subdomain to my server?

The process of adding a subdomain to your server depends on what DNS management dashboard you use. First, you’ll need to add a DNS record to your domain or subdomain, and then create virtual hosts that your subdomain will point to.

10 Best VPS Hosting on Reddit: Most Recommended Providers 2024

Reddit is a popular source for hosting recommendations, including VPS hosting. With multiple conversations on choosing a service and dozens o…
4 min read
Ela Gal-Kfir
Ela Gal-Kfir
Digital Marketing Specialist

HostAdvice Speaks to ScalaHosting: An Interview with Chris Rusev

HostAdvice had the opportunity to speak with Chris Rusev, the CEO and co-founder of , a web hosting company that offers shared, cloud VPS, and res…
8 min read
Eddie Segal
Eddie Segal
Digital Marketing Specialist

Email Deliverability: What Is It, Key Factors & Best Practices

What is Email Deliverability? Think of it like mailing a letter and making sure it lands right in the recipient’s hands, not lost or thrown…
17 min read
Ela Gal-Kfir
Ela Gal-Kfir
Digital Marketing Specialist

Email Marketing vs. Social Media: Which is More Effective?

What is Email Marketing? Email marketing is a  that involves companies reaching out to potential and existing customers via email&nbsp…;
10 min read
Ela Gal-Kfir
Ela Gal-Kfir
Digital Marketing Specialist
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top