Study/Nginx

How to set up multiple NodeJS servers on Lightsail with a custom domain and Nginx

AC 2023. 2. 19. 14:42

If you're running multiple NodeJS servers on Amazon Lightsail and want to configure them to work with a custom domain and Nginx, here's a step-by-step guide to help you get started.

Prerequisites

Before you get started, make sure you have the following:

  • Two NodeJS servers running on Lightsail, one on port 2511 and the other on port 9010.
  • A Lightsail instance with a static IP address.
  • Access to the DNS management page for your domain name.
  • Basic knowledge of working with Linux servers and terminal commands.

Step 1: Set up DNS records

  1. Log in to your domain registrar and create an "A" record for your domain, pointing to your Lightsail instance's static IP address. For example, create an "A" record for kronworld.org and point it to your Lightsail static IP address.
  2. Next, create a "CNAME" record for a subdomain, such as peerServer.kronworld.org, pointing to your Lightsail static IP address.

Step 2: Configure Nginx

  1. Log in to your Lightsail instance and install Nginx by running the following command:
bashCopy code
sudo apt-get update sudo apt-get install nginx
sudo apt-get update
sudo apt-get install nginx​
  1. Once Nginx is installed, open the default Nginx configuration file by running the following command:
bashCopy code
sudo nano /etc/nginx/sites-available/default
sudo nano /etc/nginx/sites-available/default​
  1. Update the Nginx configuration file to proxy traffic to your NodeJS servers. Add the following code to the location block:
location / {
  proxy_pass http://localhost:2511;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection 'upgrade';
  proxy_set_header Host $host;
  proxy_cache_bypass $http_upgrade;
}

location /peerServer/ {
  proxy_pass http://localhost:9010;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection 'upgrade';
  proxy_set_header Host $host;
  proxy_cache_bypass $http_upgrade;
}

This code tells Nginx to proxy traffic from the root domain to your server running on port 2511, and traffic to the peerServer subdomain to the server running on port 9010.

  1. Save the configuration file and restart Nginx by running the following command:
sudo service nginx restart

Step 3: Configure your NodeJS servers

  1. For each NodeJS server, open the server's configuration file and update the server's listening port to use the localhost IP address. For example, if your NodeJS server is using port 2511, update the configuration file to listen on http://localhost:2511.
  2. Save the configuration file and restart the NodeJS server.

Step 4: Test your setup

  1. Open a web browser and navigate to your domain name. You should see the website served by the server running on port 2511.
  2. Navigate to the peerServer subdomain, for example peerServer.kronworld.org. You should see the website served by the server running on port 9010.

Congratulations! You've successfully set up multiple NodeJS servers on Lightsail, configured them to work with a custom domain, and proxy traffic using Nginx.

 

Practical application cases
 

 

  1. Add DNS records to LightSail:
    • Go to the LightSail Console and navigate to the "Networking" tab for your instance.
    • Click on "Create DNS zone" and add your domain name "kronworld.org".
    • Once the DNS zone is created, add an A record for your domain and a CNAME record for your subdomain. The A record should point to the public IP address of your instance and the CNAME record should point to your domain name.
  2. Configure Nginx:
    • Install Nginx on your instance using the following command:
sudo apt-get install nginx

Create a new configuration file for your domain and subdomain using the following commands:

sudo nano /etc/nginx/sites-available/x.org
sudo nano /etc/nginx/sites-available/x.x.org

Paste the following configuration into the x.org file:

 
server {
    listen 80;
    server_name x.org;
    location / {
        proxy_pass http://localhost:2511;
    }
}

 

Paste the following configuration into the peerServer.kronworld.org file:

server {
    listen 80;
    server_name peerServer.kronworld.org;
    location / {
        proxy_pass http://localhost:9010;
    }
}
 

Create a symbolic link for each configuration file in the sites-enabled directory using the following commands:

sudo ln -s /etc/nginx/sites-available/kronworld.org /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/peerServer.kronworld.org /etc/nginx/sites-enabled/

Test the Nginx configuration using the following command:

sudo nginx -t

If the configuration is successful, restart Nginx using the following command:

sudo systemctl restart nginx

 

Use pm2 to manage your NodeJS applications:

  • Install pm2 using the following command:
npm install pm2 -g

Start your NodeJS applications using pm2 with the following commands:

pm2 start app1.js --name app1 --watch --port 2511
pm2 start app2.js --name app2 --watch --port 9010

 

Verify that your NodeJS applications are running using the following command:

pm2 status

That should be it! Your NodeJS applications should now be accessible via your domain and subdomain, with Nginx acting as a reverse proxy for each application on their respective ports.

LIST

'Study > Nginx' 카테고리의 다른 글

Nginx Setting 값  (0) 2022.03.11