How to configure Nginx as a Reverse Proxy for Apache on Ubuntu 16.04 ?

  • September 23rd, 2016
  • By Amal S
  • Blog
  • apache_nginx_tw-min

    Apache and Nginx are the two open source web servers which account over 50% of the total traffic on the internet. Those solutions handle a diverse workload and are compatible with other software, which is associated with the server, in order to produce complete web stack.

    Both Apache and Nginx excel in its own way and have their own qualities but the latter is considered to be the fastest web server in the world. Big technology companies such as WordPress, Comodo, Netflix, Github, Cloudflare had already switched to Nginx due to its high performance and resource requirements. NGINX set of features and some server roles are:

    • Reverse proxy server for the HTTP, HTTPS, IMAP, POP3 and SMTP protocol.
    • Front-end proxy for Apache and other web servers, combining the flexibility of Apache along with the support of good static content performance of NGINX.
    • Load balancer and an HTTP cache.
    • As Nginx only passes true HTTP requests, it protects your server from common attacks such as DDOS (Distributed Denial of Service).
    • Compatible with GZIP compression
    • High output

    This blog guides on how to set up Nginx which is known for stability, simple configuration, rich attribute set and effective resource utilization as a reverse proxy for Apache on Ubuntu 16.04.

    Required Pre-requisites

    In order to configure Nginx as a reverse proxy over Apache on Ubuntu, there are Pre-requisites which is needed to be made available

    • Install a new Ubuntu version
    • A standard user account with sudo privileges is required for the configuration.
    • Install Nginx HTTP services on the server.
    • Skill for working on a Linux platform.

    Apache and PHP-FPM Installation

    Install the PHP FastCGI Apache module named as libapache2-mod-fastcgi. Check whether the repository is updated and thus make sure you have the latest version.

    sudo apt-get update

    Install the necessary packages.

    sudo apt-get install apache2 libapache2-mod-fastcgi php-fpm

    Apache and PHP-FPM Configuration

    In order to configure Apache and PHP-FPM, the Apache port number will be changed to 8080 and configure it to work with PHP-FPM using mod_fastcgi module. For this edit the Apache configuration file and change the Apache number.

    sudo nano /etc/apache2/ports.conf

    Find the following line:

    Listen 80

    Change it to:

    Listen 8080

    Save and exit ports.conf.

    Edit the default virtual host file of Apache. Only on the port 80, the <VirtualHost> directive in this file is set to serve sites, so we have to change that. Open the default virtual host file.

    sudo nano /etc/apache2/sites-available/000-default.conf

    The first line should be:

    <VirtualHost *:80>

    Change it to:

    <VirtualHost *:8080 >

    Save the file and reload Apache.

    sudo systemctl reload apache2

    Verify that Apache is now listening on 8080.

    sudo netstat -tlpn

    Apache to Use mod_fastcgi Configuration

    Using mod_php, Apache serves PHP pages but it requires additional configuration to work with PHP-FPM.

    sudo a2dismod php7.0

    The next step is to add a configuration block for mod_fastcgi which depends on mod_action. mod_action is disabled by default, so we first need to enable it.

    sudo a2enmod actions

    These configuration directives pass requests for .php files to the PHP-FPM UNIX socket.

    sudo nano /etc/apache2/mods-enabled/fastcgi.conf

    Following lines needed to be added within the <IfModule mod_fastcgi.c> . . . </IfModule> block, below the existing items in that block:

    AddType application/x-httpd-fastphp .php

    Action application/x-httpd-fastphp /php-fcgi

    Alias /php-fcgi /usr/lib/cgi-bin/php-fcgi

    FastCgiExternalServer /usr/lib/cgi-bin/php-fcgi -socket /run/php/php7.0-fpm.sock -pass-header Authorization

    <Directory /usr/lib/cgi-bin>

    Require all granted


    Save the changes you made to fastcgi.conf and does a configuration test.

    sudo apachectl –t

    PHP Functionality Verification

    Check whether the PHP works by creating a phpinfo () file and accessing it from your web browser.

    echo “<?php phpinfo(); ?>” | sudo tee /var/www/html/info.php

    Virtual Hosts Creation for Apache

    First, create the root directories:

    sudo mkdir -v /var/www/{,}

    Then create an index file for each site.

    echo “<h1 style=’color: green;’>Foo Bar</h1>” | sudo tee /var/www/

    echo “<h1 style=’color: red;’>Test IO</h1>” | sudo tee /var/www/

    Then create a phpinfo() file for each site so we can test PHP is configured properly.

    echo “<?php phpinfo(); ?>” | sudo tee /var/www/

    echo “<?php phpinfo(); ?>” | sudo tee /var/www/

    Now create the virtual host file for the domain.

    sudo nano /etc/apache2/sites-available/

    Place the following directive in this new file:

    <VirtualHost *:8080>



    DocumentRoot /var/www/

    <Directory /var/www/>

    AllowOverride All



    Now that both Apache virtual hosts are set up, enable the sites using the a2ensite command. Through this, a symbolic link is created to the virtual host file in the sites-enabled directory.

    sudo a2ensite

    sudo a2ensite

    Check Apache for configuration errors again.

    sudo apachectl -t

    Reload Apache if Syntax OK is displayed.

    sudo system reload apache2

    Nginx – Installation and Configuration

    Here, we will install Nginx and configure the domains and as Nginx’s virtual hosts. Install Nginx using,

    sudo apt-get install Nginx

    Remove the default virtual host’s symlink

    sudo rm /etc/Nginx/sites-enabled/default

    The same procedures which we have used for Apache will be used for creating virtual hosts for Nginx. Develop the root directories for both the websites.

    sudo mkdir -v /usr/share/nginx/{,}

    we’ll again create index and phpinfo() files for testing after setup is complete.

    echo ”

    “|sudo tee /usr/share/nginx/

    echo ”

    ” | sudo tee /usr/share/nginx/

    echo “” | sudo tee /usr/share/nginx/

    echo “” | sudo tee /usr/share/nginx/

    The next step is to create a virtual host file for the domain

    sudo nano /etc/nginx/sites-available/

    Paste the following into the file for

    server {
    listen 80 default_server;

    root /usr/share/nginx/;
    index index.php index.html index.htm;

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

    location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    include snippets/fastcgi-php.conf;

    Save and close the file. Now create a virtual host file for Nginx’s second domain,

    sudo nano /etc/nginx/sites-available/

    The server block for should look like this:

    server {
    root /usr/share/nginx/;
    index index.php index.html index.htm;

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

    location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    include snippets/fastcgi-php.conf;

    Save and close the file. Then enable both the sites by creating symbolic links to the sites-enableddirectory.

    sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

    sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

    Do an Nginx configuration test:

    sudo nginx -t

    Then reload Nginx if OK is displayed.

    sudo systemctl reload nginx

    Configuration of Nginx for Apache’s Virtual Hosts

    Create and an additional Nginx virtual host with multiple domain names in the server_name directives.

    Let’s create an additional Nginx virtual host with multiple domain names in the server_name directives. To apache, the requests for such domain names will be proxied.

    Create a new Nginx virtual host file:

    sudo nano /etc/nginx/sites-available/apache

    Add the code block below. It includes the names of both Apache virtual host domains and proxies to their requests to Apache.

    server {
    listen 80;

    location / {
    proxy_pass http://your_server_ip:8080;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    Enable this new virtual host by creating a symbolic link and after that save the file.

    sudo ln -s /etc/nginx/sites-available/apache /etc/nginx/sites-enabled/apache

    Do a configuration test:

    sudo nginx -t

    Reload Nginx if OK is displayed.

    sudo systemctl reload nginx


    Nginx is considered to be the most popular HTTP server and reverse proxy. It is used for the common protocols like SMTP, HTTPS, POP3, HTTP, and IMMP and used as the load balancer for HTTP cache. This method of a reverse proxy is a set up which utilizes Nginx as both the frontend and backend and handles the request coming from the browser by passing it to the backend of Apache. It is more suitable for static websites rather than dynamic websites. Nginx has rich features such as high performance and low memory/RAM usage. Even though the Nginx is a reverse proxy for Apache, Nginx’s proxy service is transparent and connections to Apache’s domains appear to be served directly from Apache itself.

    Recent Post

    Looking for a reliable 24/7 support provider?

    Contact US Today

    Contact Us