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?
Provider | User Rating | Discounts | Expert & User Reviews | |
---|---|---|---|---|
4.6 | 10% Off | Hostinger Review | Visit Hostinger | |
5.0 | 75% Off | HostArmada Review | Visit HostArmada | |
5.0 | 75% Off | ChemiCloud Review | Visit ChemiCloud |
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.
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 what a subdomain is and how your website can benefit from it
- Add subdomain with GoDaddy with or without cPanel
- Choose the best web hosting plan for your business or personal needs
- Create a subdomain using cPanel
Learn More About Subdomains
How to Create a Subdomain for a Primary Domain: Comprehensive Guide
How to Create a Subdomain in GoDaddy with and without cPanel
What Is a Subdomain: Everything You Need to Know
Subdomain vs Subdirectory: Which is Better for SEO?
Domain vs Subdomain: What They Are and What’s the Difference?
How to Find All Subdomains of a Domain with Free Online Tools
Wildcard Subdomains: What They Are & How To Set Them Up
How To Redirect a Subdomain to Url: A Complete Guide
DNS Record for Subdomain: CNAME, DMARC, A Records & more
Email Subdomain: What It Is & How to Use It
SSL Certificate for Subdomain: How to Pick and Install the Right One?
How to Create a Subdomain in cPanel: Step-by-Step Guide
Subdomain Delegation to Another DNS Service Provider: Complete Guide
SEO for Subdomains: Benefits, Disadvantages & Strategies for Success
How to Create a Subdomain in Namecheap Hosting