TechTorch

Location:HOME > Technology > content

Technology

Configuring Multiple Server Blocks in Nginx on the Same Host and Port for a Linux Reverse Proxy

April 15, 2025Technology1343
Configuring Multiple Server Blocks in Nginx on the Same Host and Port

Configuring Multiple Server Blocks in Nginx on the Same Host and Port for a Linux Reverse Proxy

Using multiple server blocks in Nginx on the same host and port is a common practice for hosting multiple applications or services effeciently. Each server block can respond to different domain names or IP addresses, allowing you to route traffic appropriately. This guide will walk you through setting it up, particularly for a use case involving Nextcloud and other applications.

Step-by-Step Guide to Configure Multiple Server Blocks in Nginx

Install Nginx

If you haven't already installed Nginx on your Linux server, do so using the following command:

sudo apt update sudo apt install nginx

Create Server Block Configuration Files

You can create separate configuration files for each application in the `/etc/nginx/sites-available` directory. For example, let's create a configuration for Nextcloud:

sudo nano /etc/nginx/sites-available/nextcloud

In this file, add the following configuration:

server {
    listen 80;
    server_name ;
    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Create another configuration for a different application, such as a blog:

sudo nano /etc/nginx/sites-available/blog

Add the following configuration:

server {
    listen 80;
    server_name ;
    location / {
        proxy_pass http://localhost:8081;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Enable the Server Blocks

Create symbolic links in the `sites-enabled` directory to enable your configurations:

sudo ln -s /etc/nginx/sites-available/nextcloud /etc/nginx/sites-enabled/ sudo ln -s /etc/nginx/sites-available/blog /etc/nginx/sites-enabled/

Test the Nginx Configuration

Before reloading Nginx, ensure there are no syntax errors in your configuration files:

sudo nginx -t

Reload Nginx

If the configuration test is successful, reload Nginx to apply the changes:

sudo systemctl reload nginx

Important Considerations

Domain Names

Ensure that the domain names are correctly pointed to your server's IP address in your DNS settings.

Firewall

Make sure your firewall allows traffic on port 80 and 443 if you plan to use HTTPS.

SSL Configuration

For production environments, consider setting up SSL certificates using Let's Encrypt or another certificate authority. This involves listening on port 443 and configuring SSL settings.

For example, to set up SSL for Nextcloud, you would modify the server block like this:

server { listen 443 ssl; server_name ; ssl_certificate /path/to/ssl_; ssl_certificate_key /path/to/ssl_certificate_; location / { proxy_pass http://localhost:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }

Conclusion

By following the steps above, you can effectively manage multiple server blocks in Nginx on the same host and port, allowing you to serve different applications like Nextcloud and a blog from a single server. Be sure to monitor your Nginx logs for any issues and adjust your configurations as necessary.