Installation and Configuration Nginx as Reverse Proxy for Apache on Ubuntu Server

Installation and Configuration Nginx as Reverse Proxy for Apache on Ubuntu Server

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
sudo su

Then install apache with the apt-get command:

1
apt-get install apache2

Once apache is installed, we must install PHP as apache module for this tutorial:

1
apt-get install php5 php5-mysql libapache2-mod-php5

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
vim /etc/apache2/ports.conf

And then change port 80 to 7070:

ports.conf
1
Listen 7070

And then save and exit.

And now go to the virtualhost directory and edit the file 000-default.conf:

1
vim /etc/apache2/sites-available/000-default.conf

And then make sure your configuration is same as below:

000-default.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<VirtualHost *:7070>
  ServerName www.reverse.com
  ServerAlias reverse.com

  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/html/geekhmer-dev

  <Directory "/var/www/html/geekhmer-dev">
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
  </Directory>

  ErrorLog ${APACHE_LOG_DIR}/geekhmer-dev_error.log
  CustomLog ${APACHE_LOG_DIR}/geekhmer-dev_access.log combined
</VirtualHost>

And then save and exit.

Next test the configuration and restart apache:

1
2
3
apachectl configtest

systemctl restart apache2

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
echo "<?php phpinfo(); ?>" > /var/www/html/info.php

Visit your site www.reverse.com:7070/info.php.

Nginx

Install Nginx
Let install Nginx with the following apt-get command:

1
apt-get install nginx

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
vim /etc/nginx/nginx.conf

And then enable Gzip compression for Nginx by uncomment the gzip lines below:

nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";

gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/    javascript;

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:

nginx.conf
1
2
3
4
##
# Proxy Cache Settings
##
proxy_cache_path /var/cache levels=1:2 keys_zone=reverse_cache:60m inactive=90m max_size=1000m;

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
vim /etc/nginx/proxy_params

Paste the configuration below:

proxy_params
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;

##
# Cache configuration
##
proxy_cache reverse_cache;
proxy_cache_valid 3s;
proxy_no_cache $cookie_PHPSESSID;
proxy_cache_bypass $cookie_PHPSESSID;
proxy_cache_key "$scheme$host$request_uri";
add_header X-Cache $upstream_cache_status;

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
vim /etc/nginx/sites-available/reverse.conf

Paste the configuration below:

reverse.conf
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
server {
  listen 80;

  # Site Directory same in the apache virtualhost configuration
  root /var/www/html/geekhmer_dev;
  index index.php index.html index.htm;

  # Domain
  server_name www.reverse.com reverse.com;

  location / {
    try_files $uri @proxy;
  }

  location @proxy {
    proxy_pass http://127.0.0.1:7070;
    include /etc/nginx/proxy_params;
  }

  location ~* \.php$ {
    proxy_pass http://127.0.0.1:7070;
    include /etc/nginx/proxy_params;
  }

  # Enable Cache the file 30 days
  location ~* .(jpg|png|gif|jpeg|css|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx|css|js)$ {
    proxy_cache_valid 200 120m;
    # expires max;
    expires 30d;
    proxy_cache reverse_cache;
    access_log off;
  }

  # Disable Cache for the file type html, json
  location ~* .(?:manifest|appcache|html?|xml|json)$ {
    expires -1;
  }

  location ~ /\.ht {
    deny all;
  }
}

And then save and exit.

And then activate the new virtualhost configuration:

1
ln -s /etc/nginx/sites-available/reverse.conf /etc/nginx/sites-enabled/

And then we will test the nginx configuration and restart nginx:

1
2
3
nginx -t

systemctl restart nginx

Nginx is configured as reverse proxy now. You can test it with curl:

1
2
3
curl -I www.reverse.com

curl -I www.reverse.com/info.php

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
apt-get install libapache2-mod-rpaf

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

Add the server IP to the line 10. My server IP is: 192.168.1.117.

rpaf.conf
1
RPAFproxy_ips 127.0.0.1 192.168.1.117 ::1

And then save and exit.

And then restart apache:

1
systemctl restart apache2

Test rpaf by viewing the apache access log with the tail command:

1
tail -f /var/log/apache2/geekhmer-dev_access.log

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.