Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Tutorials»How to Setup Apache Reverse Proxy for Node.js App

    How to Setup Apache Reverse Proxy for Node.js App

    By RahulOctober 18, 20224 Mins Read

    If your Node.js application is running on a server that is accessible to the internet, you will need to set up a reverse proxy so that external users can access your application via a URL, such as https://example.com. Even if your Node.js app is running on a private network and only accessible to internal users, setting up Apache reverse proxy will make it easier for you to scale your app in the future by allowing you to add new servers without changing all the existing URLs. In this blog post, we are going to discuss how you can set up Apache Reverse Proxy for your Node.js application using Apache web server and Let’s Encrypt TLS Certificate along with other related configurations.

    Advertisement

    A reverse proxy is an intermediary server that allows clients to reach content hosted on another server more efficiently and securely. It is commonly used with websites but can be used with any kind of network endpoint that requires dynamic incoming connections from many different sources.

    Apache as Reverse Proxy for Node.js

    Before Start

    • I am using Ubuntu 22.04 Linux system running in Virtualbox. You can also buy the VPS instance from DigitalOcean at low prices.
    • Installed latest Node.js version using this tutorial on this Linux machine.

    Step 1 – Create Sample Node Application

    As you are here 🙂 You must have a running Node.js application on some port. We assume you are running your application on port 3000. For the demonstration purpose, I am creating a sample web application on Node.js and running on port 3000. So it will be easier to understand for you.

    nano myapp.js 
    

    Then, add the following content in the JavaScript file.

    var http = require('http');
    
    http.createServer(function (req, res) {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Hello TecAdmin');
    }).listen(3000, "127.0.0.1");
    console.log('Server running at http://127.0.0.1:3000/');
    

    Your Node application is ready to serve on port 3000. Let’s start the Node.js application in the background.

    node myapp.js & 
    

    And access this in the browser. You will see the result below:

    How to Setup Apache Reverse Proxy for Node.js App
    Accessing Node Application on Port

    Step 2 – Installing Apache

    Now install the Apache web server using the default Package manager. The Ubuntu and Debian-based systems use apt, Fedora and CentOS/RHEL 8 use DNF, and CentOS/RHEL 7/6 use yum. Apache packages are available under default repositories on almost operating systems.

    sudo apt install apache2   ### Debians based systems 
    sudo dnf install httpd     ### Fedora & CentOS 8 
    

    After installation of the Apache web server, you must have enabled the Proxy module. This module is enabled in Apache for users who installed using rpm packages. If you don’t have enabled edit your Apache configuration /etc/httpd/conf/httpd.conf or for Apache 2.4 /etc/httpd/conf.modules.d/00-proxy.conf file and uncomment the following lines or put them in the file.

    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_http_module modules/mod_proxy_http.so
    

    For the Debian-based systems use the following command to enable the Proxy module with Apache.

    sudo a2enmod proxy proxy_http 
    

    Step 3 – Configure VirtualHost in Apache

    As you have configured and run a Node.js demo server. Now, start configuration with the Apache web server. Create a virtual host configuration file for your domain under /etc/apache2/sites-available/ directory.

    ### Debian based system's 
    sudo nano /etc/apache2/sites-available/example.com.conf 
    
    ### Redhat based system's 
    sudo vim /etc/httpd/conf.d/example.com.conf 
    

    Add the following content.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <VirtualHost *:80>
          ServerName example.com
          ServerAlias www.example.com
          ServerAdmin webmaster@example.com
     
          ProxyPreserveHost On
          ProxyPass / http://localhost:3000/
          ProxyPassReverse / http://localhost:3000/
     
          ErrorLog /var/log/apache2/error.log
          CustomLog /var/log/apache2/access.log combined
    </VirtualHost>

    Save the file and close it.

    After creating the configuration, restart the Apache webserver using the following command.

    ### Debian based system's 
    sudo a2ensite example.com 
    sudo systemctl restart apache2 
    
    ### Redhat based system's 
    sudo systemctl restart httpd 
    

    Step 4 – Verify Setup

    The Apache reverse proxy server is configured to accept requests to send them to the backend Node application. Now, access your domain name in a browser. If everything is configured correctly, you should see the Node application.

    How to Setup Apache Reverse Proxy for Node.js App
    Accessing Node Application via Apache Reverse Proxy

    Conclusion

    In this blog post, we discussed why you need to set up reverse proxy and how you can set up Apache Reverse Proxy for your Node.js application. We also discussed why it is important to configure Nginx in such a way and how you can do so. Now that you know how to set up Apache Reverse Proxy for your Node.js application, you can rest assured that your application will be accessible to anyone who wants to access it. Alternatively, Nginx has better performance as a Proxy server, so if you are not bounded with Apache, go with the Nginx proxy server.

    Apache NodeJs reverse proxy
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Modulus Operator (%) in Bash

    Using Modulus Operator (%) in Bash

    Calculate Remainder in Bash

    How to Calculate Remainder (%) of a Division in Bash

    Calculating Division and Remainder in Bash

    Calculating Division and Remainder in Bash

    View 9 Comments

    9 Comments

    1. Mihai on October 5, 2021 12:07 pm

      Be careful, having “ProxyRequests On” will turn your Apache into an open proxy that can be used for spamming unless you configure some access rules ( which are not configured by default ). According to apache’s documentation this is not required if you use “ProxyPass”

      Reply
      • Rahul on October 6, 2021 7:35 am

        Thanks, Mihai, I have updated the tutorial as per your suggestions.

        Reply
    2. dohab on October 2, 2020 7:09 am

      Hi,
      How to setup https instead?

      Reply
      • Rahul on October 3, 2020 2:25 am

        Hi Dohab, YOu can install free ssl certificate with lets encrypt on Apache:

        https://tecadmin.net/apache-lets-encrypt-ssl-ubuntu/

        Reply
        • Grid on June 13, 2021 2:18 am

          Thanks for the tutorial on “How to Setup Apache As Frontend Proxy for Node.js” Will it work for a https connection ie. in your case https://example.com

          Reply
    3. OK on June 23, 2020 7:58 am

      You accidently mentioned “Nginx” instead of “Apache” at some places in you article, e.g.

      – “Now install the Nginx web server…”
      – “Nginx is available under default repositories…”

      Reply
      • Rahul on June 23, 2020 8:52 am

        Thanks, Updated tutorial

        Reply
    4. Jon on February 6, 2020 1:25 pm

      nginx is the most popular webserver not Apache

      Reply
    5. Tigran Kryukov on February 2, 2020 4:23 am

      Nice! Thank you for this guide!

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Configure Postfix to Use Gmail SMTP on Ubuntu & Debian
    • PHP Arrays: A Beginner’s Guide
    • Deploying Flask Application on Ubuntu (Apache+WSGI)
    • OpenSSL: Working with SSL Certificates, Private Keys and CSRs
    • How to Create and Read List in Python
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.