• Get In Touch
June 11, 2016

Configure Nginx as a Reverse Proxy for Apache on Ubuntu-14.04

Want your very own server? Get our 1GB memory, Xeon V4, 25GB SSD VPS for £10.00 / month.
Get a Cloud Server

Nginx is free, open source HTTP server and reverse proxy. It is also a mail proxy server for IMAP/POP3. Nginx is high performance web server with a rich set of features, simple configuration style and low memory usage.

Using Nginx as a reverse proxy is a great idea for several reasons. Firstly it handles static content very well. It is able to handle the requests and serve static content much faster in our tests and this has cut our page load time in about half. Both nginx and apache are powerful and effective servers. Apache is known for it’s power and Nginx is known for it’s speed.

Common uses for a reverse proxy server include:

  • Load Balancing – A reverse proxy server sitting in front of your backend servers and distributing client requests across a group of servers in a manner that maximises speed and capacity utilisation whilst ensuring no single server is overloaded, which can degrade performance. If a server goes down, the load balancer redirects traffic to the remaining online servers.

  • Web Acceleration – Reverse proxies can compress inbound and outbound data, as well as cache commonly requested content, both of which speed up the flow of traffic between clients and servers.

  • Security and Anonymity – By intercepting requests headed for your backend servers, a reverse proxy server protects their identities and acts as an additional defense against security attacks.

In this tutorial, we will install and configure the Nginx web server as reverse proxy for Apache on Ubuntu-14.04. Apache will run on port 8080, then we will configure Nginx run on port 80 to receive a request from user, the request will then be forwarded to the apache server that is running on port 8080.

Requirements

  • A server running Ubuntu-14.04.
  • A static IP Address for your server.
  • A non-root user account with sudo privilege set up on your server.

Getting Started

Let’s start by making sure that your Ubuntu server is fully up to date.

You can update your server by running the following command:

sudo apt-get update
sudo apt-get upgrade

Install Nginx

Firstly, you will need to install and configure Nginx which will serve the front end of your site.

Run the following command to install Nginx on terminal:

sudo apt-get install nginx

Configure Nginx

Once Nginx is installed, you will need to setup Nginx as reverse proxy.

You can do this by creating new virtual host file:

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

Add the following content:

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

Save and close the file.

In the above file, The root directive specify the path of default document root directory. try_files attempts to serve whatever page the visitor requests. If nginx is unable, then the file is passed to the proxy.

proxy_pass defines the address of the proxied server. localtion directive block denies access to .htaccess file.

You can check Nginx configuration syntax by running the following command:

sudo nginx -t

You should see the following output if everything is ok:

    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful

Now, activate the virtual host by running the following command:

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

Additionally, delete the default nginx server block.

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

Next, start Nginx service by running the following command:

sudo /etc/init.d/nginx start

Then, add Nginx service to start at boot time by running the following command:

sudo update-rc.d nginx defaults

You should see the following output:

     Adding system startup for /etc/init.d/nginx ...
       /etc/rc0.d/K20nginx -> ../init.d/nginx
       /etc/rc1.d/K20nginx -> ../init.d/nginx
       /etc/rc6.d/K20nginx -> ../init.d/nginx
       /etc/rc2.d/S20nginx -> ../init.d/nginx
       /etc/rc3.d/S20nginx -> ../init.d/nginx
       /etc/rc4.d/S20nginx -> ../init.d/nginx
       /etc/rc5.d/S20nginx -> ../init.d/nginx

Install and Configure Apache

Next, you will need to install Apache for the backend. You can install Apache by running the following command:

sudo apt-get install apache2

Next, you will need to configure apache to take over the backend. Since Nginx is listening to 80 and we told it to pass the proxy to 8080 which should be where Apache is listening to receive a request. Let’s tell Apache to listen to 8080 and leave 80 for Nginx.

Open up the apache ports file to start setting apache on the correct port:

sudo nano /etc/apache2/ports.conf

Add/Edit the file as shown below:

    NameVirtualHost 127.0.0.1:8080
    Listen 127.0.0.1:8080

Save and close the file and restart Apache service.

sudo /etc/init.d/apache2 restart

Then, add Apache service to start at boot time by running the following command:

sudo update-rc.d apache2 defaults

Next, you will need to edit Apache default virtualhost file:

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

Make sure your configuration is same as below:




ServerAdmin webmaster@localhost DocumentRoot /var/www/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined

Save and close the file and restart both services to make the changes take effect:

sudo /etc/init.d/apache2 restart
sudo /etc/init.d/nginx restart

Test Your Nginx Reverse Proxy

Once everything is configured properly, it’s time to test your nginx reverse proxy.

You can do this by running the following curl command:

curl -I localhost

You should see the following output:

    HTTP/1.1 200 OK
    Server: nginx/1.6.2 (ubuntu)
    Date: Wed, 1 Jun 2016 09:34:32 IST
    Content-Type: text/html
    Content-Length: 11321
    Last-Modified: Tue, 31 May 2016 11:23:08 IST
    Connection: keep-alive
    Vary: Accept-Encoding
    ETag: "564f1a7a-2c39"
    Expires: Wed, 1 Jun 2016 09:34:31 IST
    Cache-Control: no-cache
    Accept-Ranges: bytes

Configure Logging

If you want to configure apache to log the real IP address of the visitor instead of the local IP address, then you will need to install the apache module “libapache2-mod-rpaf”.

You can install it by running the following command:

sudo apt-get install libapache2-mod-rpaf

Next, edit the module configuration file:

sudonano /etc/apache2/mods-available/rpaf.conf

Add the server IP address, in this example use 192.168.1.10 as server IP.

    RPAFproxy_ips 127.0.0.1 192.168.1.10 ::1

Save and close the file and restart Apache.

sudo /etc/init.d/apache2 restart

You can test rpaf by viewing the Apache access log:

tail -f /var/log/apache2/access.log

You should see the following output:

    #Before:
    127.0.0.1 - - [1/Jun/2016:09:45:08 +0000] "GET /index.html HTTP/1.1"
    127.0.0.1 - - [1/Jun/2016:09:45:11 +0000] "GET /index.html HTTP/1.1"
    #After
    192.168.1.10 - - [1/Jun/2016:09:45:34 +0000] "GET /index.html HTTP/1.1"
    192.168.1.10 - - [1/Jun/2016:09:45:37 +0000] "GET /index.html HTTP/1.1"

Conclusion

And that’s that! That’s how easy Nginx is to configure and use and act as reverse proxy for apache. We hope you have enjoyed this tutorial.

Want your very own server? Get our 1GB memory, Xeon V4, 25GB SSD VPS for £10.00 / month.
Get a Cloud Server

Share this Article!

Related Posts

Node.js Authentication – A Complete Guide with Passport and JWT

Node.js Authentication – A Complete Guide with Passport and JWT

Truth be told, it’s difficult for a web application that doesn’t have some kind of identification, even if you don’t see it as a security measure in and of itself. The Internet is a kind of lawless land, and even on free services like Google’s, authentication ensures that abuses will be avoided or at least […]

Node.js and MongoDB: How to Connect MongoDB With Node

Node.js and MongoDB: How to Connect MongoDB With Node

MongoDB is a document-oriented NoSQL database, which was born in 2007 in California as a service to be used within a larger project, but which soon became an independent and open-source product. It stores documents in JSON, a format based on JavaScript and simpler than XML, but still with good expressiveness. It is the dominant […]

Using MySQL with Node.js: A Complete Tutorial

Using MySQL with Node.js: A Complete Tutorial

Although data persistence is almost always a fundamental element of applications, Node.js has no native integration with databases. Everything is delegated to third-party libraries to be included manually, in addition to the standard APIs. Although MongoDB and other non-relational databases are the most common choice with Node because if you need to scale an application, […]

Node.Js Vs Django: Which Is the Best for Your Project

Node.Js Vs Django: Which Is the Best for Your Project

Django and NodeJs are two powerful technologies for web development, both have great functionality, versatile applications, and a great user interface. Both are open source and can be used for free. But which one fits your project best? NodeJs is based on JavaScript, while Django is written in Python. These are two equally popular technologies […]

Nodejs Vs PHP:  Which Works Best?

Nodejs Vs PHP: Which Works Best?

Before getting into the “battle” between Node.js and PHP we need to understand why the issue is still ongoing. It all started with the increased demand for smartphone applications, their success forcing developers to adapt to new back-end technologies that could handle a multitude of simultaneous requests. JavaScript has always been identified as a client-side […]