Software & AppsOperating SystemLinux

How To Configure Nginx Reverse Proxy with Subdomains

Ubuntu 20

In this article, we will guide you through the process of configuring Nginx as a reverse proxy with subdomains. This setup is beneficial when you want to route traffic from different subdomains to various backend servers.

Quick Answer

To configure Nginx as a reverse proxy with subdomains, you need to define upstream blocks for each backend server and configure server blocks for each subdomain. This setup allows you to route traffic from different subdomains to various backend servers, providing a flexible and powerful way to manage your web traffic.

Prerequisites

Before we begin, ensure that you have the following:

  • A server with Nginx installed.
  • Access to the terminal and necessary permissions to edit configuration files.
  • The necessary DNS records set up correctly. For example, app1.domain.com should point to your proxy.domain.com server.

Understanding Nginx

Nginx is a powerful open-source web server that can also be used as a reverse proxy, load balancer, and HTTP cache. In the context of this guide, we will use Nginx as a reverse proxy to direct web requests from specific subdomains to different backend servers.

Step 1: Define Upstream Blocks

The first step is to define an upstream block for each server you want to proxy. The upstream module allows Nginx to distribute incoming traffic to several backend servers.

Open your Nginx configuration file, which is usually located at /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf.

Here’s an example of an upstream block for a Tomcat server:

upstream tomcat_server {
 server tomcat.domain.com:8080;
}

In this block, tomcat_server is the name we’ve given to the upstream block, and tomcat.domain.com:8080 is the address of the backend server.

Step 2: Configure Server Blocks

Next, you need to configure a server block for each subdomain. The server block defines how to process incoming requests.

Here’s an example for app1.domain.com:

server {
 listen 80;
 server_name app1.domain.com;

 location / {
 proxy_pass http://tomcat_server/app1;
 proxy_set_header X-Forwarded-Host $host;
 }
}

In this block:

  • listen 80; specifies that Nginx should listen on port 80 for incoming connections.
  • server_name app1.domain.com; sets the domain name that this block will respond to.
  • location / {...} defines how to process requests for the root URL (/).
  • proxy_pass http://tomcat_server/app1; tells Nginx to forward requests to the tomcat_server upstream block we defined earlier.
  • proxy_set_header X-Forwarded-Host $host; sets the X-Forwarded-Host header to the hostname from the request.

Step 3: Save and Test Your Configuration

After defining all the necessary upstream and server blocks, save your changes and exit the text editor.

To ensure that your configuration is correct, use the nginx -t command:

sudo nginx -t

If the configuration is correct, you should see a message saying nginx: configuration file /etc/nginx/nginx.conf test is successful.

Step 4: Restart Nginx

Finally, apply the changes by restarting Nginx. You can do this with the following command:

sudo service nginx restart

Step 5: Test Your Setup

Test the configuration by accessing app1.domain.com in your browser. Nginx should now proxy the request to the Tomcat server and serve the app.

Troubleshooting

If you’re experiencing issues, check the Nginx error logs (/var/log/nginx/error.log) for any error messages that might help identify the problem. Additionally, ensure that the DNS records are correctly set up and propagated.

Conclusion

In this guide, we’ve shown you how to configure Nginx as a reverse proxy with subdomains. This setup allows you to route traffic from different subdomains to various backend servers, providing a flexible and powerful way to manage your web traffic.

What is a reverse proxy?

A reverse proxy is a server that sits between client devices and backend servers. It receives requests from clients and forwards them to the appropriate backend server. It helps distribute incoming traffic, improve performance, and provide additional security features.

Why would I use Nginx as a reverse proxy with subdomains?

Using Nginx as a reverse proxy with subdomains allows you to route traffic from different subdomains to various backend servers. This is useful when you have multiple applications or services hosted on different servers and want to direct traffic based on the subdomain.

How do I define upstream blocks in Nginx?

To define an upstream block in Nginx, you need to specify the backend server’s address and port within the upstream directive. For example:

upstream tomcat_server {
 server tomcat.domain.com:8080;
}
What is the purpose of the `proxy_pass` directive in the server block?

The proxy_pass directive tells Nginx where to forward the incoming requests. It specifies the upstream block to which the requests will be proxied. For example:

location / {
 proxy_pass http://tomcat_server/app1;
}
How can I test if my Nginx configuration is correct?

You can test your Nginx configuration by running the command sudo nginx -t. If the configuration is correct, the output will show nginx: configuration file /etc/nginx/nginx.conf test is successful. If there are any errors, it will indicate the specific issue that needs to be resolved.

How do I troubleshoot issues with my Nginx reverse proxy setup?

If you’re experiencing issues, check the Nginx error logs located at /var/log/nginx/error.log. The error logs may provide helpful information to identify and resolve the problem. Additionally, ensure that the DNS records are correctly set up and propagated for the subdomains you are using.

Leave a Comment

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