Deploying MEAN stack application to AWS 3-Tier-Architecture
Partho Das
I help businesses design and automate their cloud infrastructure, streamline software deployment pipelines, and ensure they scale efficiently and securely.
Streamlining Node.js Services with Nginx Reverse Proxy on a 3-Layer Architecture
In the dynamic landscape of web development, managing multiple Node.js services efficiently becomes a critical aspect of ensuring seamless application functionality. In this article, I'll share our journey of architecting a 3-Layer live server with Node.js services, and how we tackled challenges using Nginx reverse proxy.
The Setup
We had two Node.js services running – one for our main application API on port 5000 and another for file uploads to an S3 bucket on port 7000. During development, our standalone server with a specific public IP posed no issues with the Nginx reverse proxy. However, as we transitioned to a more sophisticated live server architecture with a 3-Layer setup, things became more intricate.
The live server was designed with applications residing in a private subnet behind a load balancer. We set up multiple sub-domains for various functionalities, including one for the API, one for public-facing e-commerce, and another for the Admin panel. Each application had its own root folder, and we pushed the code from our repository to these folders.
The Challenge
With the new architecture in place, we needed three distinct Nginx configurations for the API, e-commerce, and Admin panel. We initiated the Nginx service in production mode, and the API service endpoint was ready to use. However, our journey hit a hurdle when we attempted to test the new API endpoints from Postman, encountering the error:
{
"message": "Something went wrong",
"stack": "Error: Access denied for user 'root'@'localhost' (using password: YES)"
}
Overcoming the Setback
To address this issue, we took the following steps:
Strengthened Database Security: We changed the database password to enhance security and updated the .env file with the new password.
Unified Nginx Configuration: Given that both Node.js services were accessible under the same base URL (api.domain.com) but running on different ports, we needed to route requests to these ports based on specific paths. To achieve this within the same Nginx configuration file, we updated the API Nginx configuration with an additional listener on a different port.
server {
listen 80;
server_name api.domain.com;
location / {
proxy_pass https://localhost:5000;
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 /s3 {
proxy_pass https://localhost:7000;
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;
}
}
领英推荐
Starting Individual Node.js Services
In addition to configuring Nginx, starting individual Node.js services requires careful attention:
cd /folder/app1
2. Start PM2 Service: Initiate the PM2 process for each service using the following commands:
pm2 start npm --name "app_name1" -- run "start"
Ensure that your package.json file in each app folder has a script named "start" (or the script name you specified in the PM2 command) that starts your Node.js application.
3. Verify Package.json Scripts: Check the package.json file for both Node.js projects to ensure that both have a "start" script defined, like the examples below:
For Node.js code inside /folder/app1:
{
"scripts": {
"start": "node file-upload-server.js"
}
}
For Node.js code inside /folder/app2:
{
"scripts": {
"start": "node server.js"
}
}
Testing Success
After implementing these changes, we rigorously tested the API endpoints for both the application and S3 uploads. The result was a resounding success – a testament to the power of strategic Nginx configuration in optimizing the performance and accessibility of our Node.js services.
In conclusion, navigating the intricacies of a 3-Layer server architecture requires thoughtful consideration and adaptability. Leveraging tools like Nginx as a reverse proxy not only helps address challenges but also enhances the overall efficiency of Node.js services in a complex environment.