Sling Academy
Home/DevOps/NGINX Name-based and IP-based Virtual Server: Explained with Examples

NGINX Name-based and IP-based Virtual Server: Explained with Examples

Last updated: January 22, 2024

Introduction

NGINX is a powerful open-source web server and reverse proxy server that is known for its high performance, stability, scalability, and low resource consumption. One of the key features of NGINX is its ability to efficiently manage multiple domains or interfaces through virtual servers. There are two main types of virtual servers: name-based and IP-based. This tutorial will explore NGINX name-based and IP-based virtual servers, explaining them with practical examples.

Understanding NGINX Virtual Servers

In NGINX, virtual servers are used to run more than one website (also known as ‘server blocks’ in NGINX terminology) on a single machine. Virtual servers can be categorized into name-based and IP-based virtual servers. The primary difference lies in how NGINX responds to incoming HTTP requests. In name-based virtual hosting, the server uses the hostname presented by the client. In IP-based virtual hosting, the server relies on the IP address that was used to make the connection.

Setting Up IP-Based Virtual Servers

To set up an IP-based virtual server, NGINX must listen to different IP addresses assigned to the server. Here’s an example of setting up two IP-based virtual servers running on the same NGINX instance:

server {
    listen 12.34.56.78:80;
    server_name example.com;
    
    location / {
        root /var/www/example.com;
        index index.html;
    }
}

server {
    listen 12.34.56.79:80;
    server_name another-example.com;
    
    location / {
        root /var/www/another-example.com;
        index index.html;
    }
}

In this configuration, each server block is listening to a different IP address. NGINX will serve the content based on the IP address used in the client’s request.

Setting Up Name-Based Virtual Servers

Name-based virtual servers are more common and allow you to serve different domains from a single IP address. This is useful when you have limited IP addresses. Here’s how you configure name-based virtual servers in NGINX:

server {
    listen 80;
    server_name example.com;

    location / {
        root /var/www/example.com;
        index index.html;
    }
}

server {
    listen 80;
    server_name another-example.com;

    location / {
        root /var/www/another-example.com;
        index index.html;
    }
}

Both server blocks listen on the same IP address, but they respond to different domain names specified by the server_name directive. Make sure your DNS settings are correctly configured to point your domain names to your NGINX server’s IP address.

Advanced Configuration: Handling www and non-www Domains

It is common to configure both www and non-www versions of a domain to point to the same website. In NGINX, you can easily handle this by modifying the server_name directive.

server {
    listen 80;
    server_name example.com www.example.com;

    location / {
        root /var/www/example;
        index index.html;
    }
}

This configuration ensures that both www.example.com and example.com will be served by the same virtual server block.

Redirecting HTTP to HTTPS

It’s critical to secure your website traffic by using SSL/TLS. Once you have your SSL certificates in place, you can configure NGINX to redirect all HTTP traffic to HTTPS. Here’s how:

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ssl_certificate /path/to/ssl/certificate.crt;
    ssl_certificate_key /path/to/ssl/private.key;
    # Additional SSL settings...

    location / {
        root /var/www/example.com;
        index index.html;
    }
}

The first server block listens on port 80 (HTTP) and redirects all requests to HTTPS by using a 301 permanent redirect. The second server block listens on port 443 (HTTPS) and serves the secured content.

Testing Configuration and Troubleshooting

After making changes to your NGINX configuration files, it’s important to test the configuration and check for any syntax errors. Run the following command:

nginx -t

If you encounter errors, carefully review your configuration files for any typos or syntax issues. Once NGINX reports that the syntax is okay and the test is successful, you can apply the changes by reloading NGINX:

systemctl reload nginx

Conclusion

In this tutorial, we’ve covered the basics of setting up and understanding NGINX name-based and IP-based virtual servers. With these examples, you should be able to configure your virtual servers to suit different hosting scenarios and requirements.

Remember that while name-based virtual hosting is widely used due to its efficiency with IP address utilization, there are certain situations, like SSL/TLS configurations for older clients, where IP-based virtual hosting might be necessary. Choose the method that fits your use case, keep security practices in mind, and you’ll be well on your way to leveraging the power of NGINX virtual servers.

Next Article: URL Rewriting in NGINX: The Ultimate Guide

Previous Article: NGINX: Using wildcards in server_name directive

Series: NGINX Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide