Nginx Reverse Proxy Redirect Subdomain to Different Applications

ClickUp
Note
AI Status
75%
Last Edit By
Last edited time
Oct 15, 2023 06:12 AM
Metatag
Slug
nginx-redirect-subdomain
Writer
Published
Published
Date
Oct 15, 2023
Category
Nginx

Introduction

In today's interconnected world, managing multiple web applications with subdomains has become a common practice.
As a programmer, understanding how to set up a reverse proxy server with multiple subdomains using Nginx is crucial for efficient web traffic management.
In this tutorial, we will walk through the process step-by-step, providing practical examples and code snippets.

Prerequisites

Before we begin, make sure you have the following prerequisites in place:
  • A server running Nginx (if not installed, refer to the Nginx documentation for installation instructions)
  • Basic knowledge of Nginx configuration syntax

Step 1: Configuring DNS

To set up multiple subdomains, we need to configure DNS records.
This can usually be done through your domain registrar or DNS hosting provider.
Here's an example of how to configure DNS records:
sub1.example.com IN A 192.168.1.10 sub2.example.com IN A 192.168.1.20 sub3.example.com IN A 192.168.1.30
Replace sub1.example.com, sub2.example.com, and sub3.example.com with your desired subdomains, and 192.168.1.XX with the respective IP addresses of your backend servers.

Step 2: Nginx Configuration

Next, we need to configure Nginx to handle the incoming requests and proxy them to the appropriate backend servers.
Open your Nginx configuration file (usually located at /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf) and add the following code:
server { listen 80; server_name sub1.example.com; location / { proxy_pass http://192.168.1.10; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } server { listen 80; server_name sub2.example.com; location / { proxy_pass http://192.168.1.20; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } server { listen 80; server_name sub3.example.com; location / { proxy_pass http://192.168.1.30; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
Make sure to replace sub1.example.com, sub2.example.com, and sub3.example.com with your desired subdomains, and 192.168.1.XX with the respective IP addresses of your backend servers.

Step 3: Testing

After saving the Nginx configuration file, restart or reload Nginx to apply the changes.
You can do this by running the following command:
sudo systemctl restart nginx
To test if the reverse proxy is working correctly, open a web browser and navigate to http://sub1.example.com, http://sub2.example.com, or http://sub3.example.com.
You should see the respective backend application or website loading.

Conclusion

Congratulations! You have successfully set up a reverse proxy server with multiple subdomains using Nginx.
This configuration allows you to efficiently manage and direct web traffic to different backend servers based on subdomain requests.
Feel free to explore more advanced configuration options and optimizations as per your specific requirements.
For more detailed information on Nginx configuration directives and options, refer to the Nginx documentation.

Discussion (0)

Related Posts