Grid Guides

Explore How System On Grid Can Work For You

Grid Guide Topics

How to Configure Nginx as a Reverse Proxy for Apache

Table Of Contents


    ## Table of Contents

    – [Introduction](#introduction
    – [Prerequisites](#prerequisites
    – [How to Configure Nginx as a Reverse Proxy for Apache](#how-to-configure-nginx-as-a-reverse-proxy-for-apache
    – [Install Nginx Web Server](#install-nginx-web-server
    – [Configure Nginx Web Server](#configure-nginx-web-server
    – [Install Apache Web Server](#install-apache-web-server
    – [Configure Apache Web Server](#configure-apache-web-server
    – [Finish Up](#finish-up
    – [Conclusion](#conclusion

    ## Introduction

    [Apache](https://httpd.apache.org/ and [Nginx](https://www.nginx.com/ are the two most popular free and open-source cross-platform web servers in the world. Together, Apache and Nginx are estimated to serve over 50% of the traffic on the internet. To know how both the web servers stack up in different areas, follow our guide, Apache vs Nginx: Practical Considerations.

    ## Why Use Nginx and Apache Together

    If you can’t choose between Apache and Nginx even after going through the benefits and limitations of both the servers, there is an option to use both as a combination. However, the practical way of approaching it is putting Nginx in front of Apache as a reverse proxy. In this combination, Nginx will take all the requests from clients. If a request is for static content, Nginx will deliver the content. If a request is for dynamic content, Nginx will proxy it onto Apache where it will be processed and rendered back the content to Nginx to provide it for the client.

    ## Prerequisites

    You should have access to an Ubuntu 18.04 server and created a non-root user account with sudo privileges by following our guide, [Initial server setup with Ubuntu 18.04](https://systemongrid.com/support/guides/how-to-do-initial-server-setup-with-ubuntu-18.04.

    ## How to Configure Nginx as a Reverse Proxy for Apache

    ## Install Nginx Web Server

    First, you need to install and configure Nginx web server which serves as the front end and delivers the static content. To do so, use the apt-get command.

    “`
    $ sudo apt-get install nginx
    “`

    Then, configure a virtual host to run on the front end.

    ## Configure Nginx Web Server

    Now, you need to make a few changes in the Nginx configuration file.

    “`
    $ sudo vi /etc/nginx/sites-available/example
    “`

    Use the below configuration to set up to use Nginx web server as the front end server.

    “`
    server {
    listen 80;

    root /var/www/;
    index index.php index.html index.htm;

    server_name example.com;

    location / {
    try_files $uri $uri/ /index.php;
    }

    location ~ .php$ {

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:8080;

    }

    location ~ /.ht {
    deny all;
    }
    }
    “`

    The above configuration sets up a system where extensions with a .php ending will be switched to the Apache backend that runs on port 8080.

    Active the virtual host using the command:

    “`
    $ sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/example
    “`

    Besides doing that, delete the default nginx server block using the command:

    “`
    $ sudo rm /etc/nginx/sites-enabled/default
    “`

    ## Install Apache Web Server

    Now, install Apache, your backend web server.

    “`
    $ sudo apt-get install apache2
    “`

    The Apache web server will start running on port 80.

    ## Configure Apache Web Server

    Now, you need to configure the Apache web server to take over the backend process. To do so, open the Apache ports file to set up the Apache on the correct port number.

    “`
    $ sudo vi /etc/apache2/ports.conf
    “`

    Find the following lines within the file, edit them to make Apache running on port 8080.

    “`
    NameVirtualHost 127.0.0.1:8080
    Listen 127.0.0.8080
    “`

    Then, save and exit the file.

    Copy the default Apache file’s layout and past it in the new virtual host file.

    “`
    $ sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/example
    “`
    “`
    $ sudo vi /etc/apache2/sites-available/example
    “`

    Make sure the virtual host is running on port 8080 and your Document Root is correct.

    “`

    “`

    Then, save and close the file.

    Activate the virtual host with the following command.

    “`
    $ sudo a2ensite example
    “`

    Equip your backend web server, Apache with PHP. To do so, use the below command.

    “`
    $ sudo apt-get install php5
    “`

    Restart your both server to apply the changes.

    “`
    $ sudo service apache2 restart
    “`

    “`
    $ sudo service nginx restart
    “`

    ## Finish Up

    You have set up the VPS with Nginx running on the front end of your site and Apache processing PHP, running on the back end of your site. Now, you can load your domain to take you to your site’s default page.

    You can check if the information is being routed to Apache is working by running a PHP script.

    For that, create the php.info file.

    “`
    $ sudo vi /var/www/info.php
    “`

    Paste the following line within the file.

    “`

    “`

    Then, save and exit the file.

    Now, open your web browser and type in your domain. You should be redirected to the PHP info screen mentioning that this is handled by Apache.

    Use the below command to see which ports are open and which application is running on each one.

    “`
    $ sudo netstat -plunt
    “`

    ## Conclusion

    The combination of both the web servers, Nginx and Apache might be appealing for you to use Apache for serving dynamic content and Nginx for serving static content. It can be a great boost to your server.