How to Set Up an NGINX Reverse Proxy

We’re going to put Nginx in front of an Apache webserver now. The Apache server was chosen because it is more capable of handling dynamic content.

As a result, all static content will be served by Nginx, while dynamic content will be served by Apache. This will boost efficiency by optimizing content delivery based on the handling criteria.

Next, we’ll set the Nginx Proxy Server’s IP address to 192.x.x.1 and the back-end Apache server’s IP address to 192.x.x.2. Following the installation of Apache, we can proceed to the following steps:

  1. Install Nginx
    We’ll be using the apt command on Ubuntu 18.04:
    sudo apt-get update
    sudo apt-get install nginx

    2. Disable the Default Virtual Host
    After the NGINX is installed, we need to disable the virtual host. Follow the below command to disable it.

    sudo unlink /etc/nginx/sites-enabled/default

    3. Create the Nginx Reverse Proxy
    After disabling the virtual host, we need to create a reverse-proxy.conf file within the etc/nginx/sites-available directory to keep reverse proxy information.

    We should first access the directory using the cd command:

    cd etc/nginx/sites-available/

    Then we can create the file using the vi editor:

    vi reverse-proxy.conf

    In the file we need to paste in these strings:

    server {
        listen 80;
        location / {
            proxy_pass http://192.x.x.2;
        }
    }

    The proxy pass in the preceding command allows requests coming through the Nginx reverse proxy to be forwarded to 192.x.x.2:80, which is an Apache remote socket. As a result, the content is shared by both Nginx and Apache web servers.

    Simply save the file and exit the vi editor once it’s finished. You can do so by entering :wq

    Now, use the following command to link to /sites-enabled/ and activate the directives:

    sudo ln -s /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf

    4. Test Nginx and the Nginx Reverse Proxy
    Lastly, we need to run an Nginx configuration test and restart Nginx to check its performance. Type the below command to verify the Nginx functioning on the Linux terminal:

    service nginx configtest
    service nginx restart

    Leave a Reply

    Your email address will not be published. Required fields are marked *