In this article, I will show you how to install and configure Nginx as a caching reverse proxy for an Apache web server on Ubuntu, Nginx is used as the front-end and Apache as the back-end.
Nginx will run on port 80 to respond to requests from user/browser, the request will be forwarded to the Apache server that is running on port 7070.
Apache
Install Apache & PHP
Log into your ubuntu server with SSH and switch to root user by running:
1
|
|
Then install apache with the apt-get command:
1
|
|
Once apache is installed, we must install PHP as apache module for this tutorial:
1
|
|
Configure Apache and PHP
By default, apache listens on port 80. We have to configure apache to run on port 7070 for our proxy setup as port 80 will be used by nginx later.
If you want to change the port for apache web server, you must edit the apache configuration file /etc/apache2/ports.conf
, and then proceed with the virtual host configuration in the /etc/apache2/sites-available/
directory.
First change the Apache port to 7070 by editing the file ports.conf
with the vim editor:
1
|
|
And then change port 80 to 7070:
1
|
|
And then save and exit.
And now go to the virtualhost directory and edit the file 000-default.conf
:
1
|
|
And then make sure your configuration is same as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
And then save and exit.
Next test the configuration and restart apache:
1 2 3 |
|
Then we verify that the apache and php is working by creating a new file with the name info.php
in the directory /var/www/html/
.
1
|
|
Visit your site www.reverse.com:7070/info.php.
Nginx
Install Nginx
Let install Nginx with the following apt-get command:
1
|
|
Configure Nginx
Once Nginx is installed, configure Nginx to act as reverse proxy for the apache web server that running on port 7070.
Go to the nginx configuration directory and edit the file nginx.conf
:
1
|
|
And then enable Gzip compression for Nginx by uncomment the gzip lines below:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
The most important is:
- gzip on : to turn gzip compression.
- gzip_types : is list of MIME-types which you want to turn the compression.
- gzip_proxied any : is enable compression for proxied request.
Under gzip settings, add these proxy cache settings:
1 2 3 4 |
|
The important is:
- The directory for the proxy cache is /var/cache.
- levels : is a directive that tells Nginx how the cache is saved in file system.
- key_zone : is just a name of the cache zone, you can choose it freely, but don’t add special chars or whitespace in the name. I will use the name “reverse_cache” here.
And then save and exit.
And then we will configure proxy params in /etc/nginx/proxy_params
file for using in virtualhost later.
1
|
|
Paste the configuration below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
And then save and exit.
Now we will configure a virtualhost in the directory /etc/nginx/sites-available
. And I will create a new virtualhost configuration file named reverse.conf
. Just got to the directory and create new file with vim:
1
|
|
Paste the configuration below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
And then save and exit.
And then activate the new virtualhost configuration:
1
|
|
And then we will test the nginx configuration and restart nginx:
1 2 3 |
|
Nginx is configured as reverse proxy now. You can test it with curl:
1 2 3 |
|
Configure Logging
I will configure apache to log the real ip of the visitor instead of the local IP.
Let go to install the apache module libapache2-mod-rpaf
and edit the module configuration file:
1 2 3 |
|
Add the server IP to the line 10. My server IP is: 192.168.1.117.
1
|
|
And then save and exit.
And then restart apache:
1
|
|
Test rpaf by viewing the apache access log with the tail command:
1
|
|
Nginx is installed as reverse proxy in front of the Apache web server. If a visitor requests a php file, the request will be passed to apache on port 7070, and you can see the real IP visitor on the apache log file.
Conclusion
Nginx is fast and popular web server with low memory usage that can act as web server and reverse proxy for HTTP and HTTPS protocol. Nginx reverse proxy for apache is a setup that uses Nginx as front-end, and apache as back-end. Nginx handles the incoming request from the browser and passes it to the apache back-end. In this article, we have setup a configuration for nginx as http cache that caches PHP file requests and images.