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.