Automate Nginx Reverse Proxy Setup for Node.js with Cloud-Init

  • Posted on April 13, 2023• Last validated on April 13, 2023
  • NginxNode.js
  • KFSysAsked by KFSys

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 :

Anyway, I just wanted to create an example of how to Automate Nginx Reverse Proxy Setup for Node.js with Cloud-Init

Submit an answer

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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Site Moderator
Site Moderator badge
April 13, 2023
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.


  - nginx
  - nodejs
  - npm

  - path: /etc/nginx/sites-available/nodejs_proxy
    content: |
      server {
        listen 80;

        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: |
      Description=Node.js App Service

      ExecStart=/usr/bin/node app.js


  - 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 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

  1. Log in to your DigitalOcean account and navigate to the Droplets page.
  2. Click the “Create Droplet” button.
  3. Choose your desired distribution, region, and size for the droplet.
  4. In the “Select additional options” section, enable “User data” and paste the content of your cloud-init.yaml file into the text area. Configure your SSH keys and any other desired settings.
  5. Click the “Create Droplet” button to start the creation process.
  6. DigitalOcean will now create a new Droplet and run the cloud-init script, automating the installation and configuration of Nginx as a reverse proxy for your Node.js application.

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.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel