By KFSys
System Administrator
One of the powerful features that DigitalOcean offers is the cloud-init option, which allows you to automate the initial setup of your virtual servers or “Droplets.” With cloud-init, you can automatically install packages, configure services, and perform other tasks during the Droplet creation process, making it easier to deploy applications consistently and reduce manual intervention. This feature is especially useful for scaling your infrastructure, simplifying maintenance, and ensuring a consistent environment across multiple instances.
You can read more about that here :
https://docs.digitalocean.com/tutorials/droplet-cloudinit/
Anyway, I just wanted to create an example of how to Automate Nginx Reverse Proxy Setup for Node.js with Cloud-Init
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Accepted Answer
To create a cloud-init file which helps you(automatically) installs and configures Nginx as a reverse proxy with NodeJS you can follow these steps
Step 1: Create the cloud-init script Save the following script as cloud-init.yaml. This script will install the required packages, create necessary configuration files, and start the services.
#cloud-config
packages:
- nginx
- nodejs
- npm
write_files:
- path: /etc/nginx/sites-available/nodejs_proxy
content: |
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://localhost:3000;
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;
}
}
- path: /etc/systemd/system/nodejs-app.service
content: |
[Unit]
Description=Node.js App Service
After=network.target
[Service]
Environment=NODE_PORT=3000
WorkingDirectory=/var/www/nodejs-app
ExecStart=/usr/bin/node app.js
Restart=always
User=www-data
Group=www-data
[Install]
WantedBy=multi-user.target
runcmd:
- ln -s /etc/nginx/sites-available/nodejs_proxy /etc/nginx/sites-enabled/
- rm /etc/nginx/sites-enabled/default
- systemctl daemon-reload
- systemctl enable nodejs-app.service
- systemctl restart nginx
- systemctl start nodejs-app.service
Step 2: Customize the script Replace yourdomain.com with your domain name or public IP address. Adjust the proxy_pass line to match the port your Node.js application listens on if it’s different from 3000.
Step 3: Deploy your application Upload your Node.js application files to /var/www/nodejs-app on your server. Make sure that the main entry point is named app.js.
Step 4: Launch the cloud-init script on DigitalOcean
Conclusion: Now you have an automated way to set up Nginx as a reverse proxy for your Node.js applications on DigitalOcean. This cloud-init script makes it easy to deploy and scale your applications across multiple cloud platforms.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.