How to host multiple servers behind nginx?


by tina , in category: General Help , 3 months ago

How to host multiple servers behind nginx?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer


by ethelyn , 3 months ago


To host multiple servers behind Nginx, you can use a reverse proxy configuration. Here are the steps to follow:

  1. Install Nginx: Start by installing Nginx on your server. You can typically do this using the package manager of your operating system. For example, on Ubuntu, you would run sudo apt-get install nginx.
  2. Configure Nginx: The default Nginx configuration file is usually located at /etc/nginx/nginx.conf. Open this file in a text editor.
  3. Remove default configuration: By default, Nginx is configured to host a single server. You will need to remove this configuration before proceeding. Look for a section that starts with server { and ends with }. Delete this entire section.
  4. Create new server configuration blocks: For each server you want to host, you need to create a separate server configuration block. These blocks define the server-specific settings and the location of the server files. Below is an example configuration block that you can use as a template:
    server {
        listen 80;

        location / {
            proxy_pass http://localhost:3000;  // The address of your server
            proxy_set_header HOST $host;
            proxy_set_header X-Real-IP $remote_addr;

  1. Repeat step 4 for each server you want to host: For each additional server, create a separate configuration block like the one shown above. Make sure to replace with the appropriate domain name and update the proxy_pass directive with the address of the server you want to host.
  2. Save and exit the configuration file: Save the changes you made to the Nginx configuration file and exit the text editor.
  3. Restart Nginx: Finally, restart Nginx to apply the new configuration. You can do this using the command sudo systemctl restart nginx.

After following these steps, Nginx will act as a reverse proxy, routing incoming requests to the appropriate server based on the domain name specified in the configuration blocks.