Reverse Proxy Using Nginx to Serve Static Files Faster

Reverse Proxy
A proxy in common usage, is a stand-in for a web-browser. When a web browser requests access to a web resource, the web resource needs some idea of where it should send the answer. In reverse, the proxy stands-in for the web server. In such a case, the true location of the web server (whose content is being served) is hidden, from the standpoint of the client making the request.

The common solutions found on web is to point all traffic to NGINX and route traffic to APACHE if it's not static files, but with our case, we don't want to change the current configuration of existing APACHE installation and settings as much as possible. The safe technique will be used to serve static contents like images from another server using a much lighter web server application name NGINX. The rest of the page would remain served via APACHE.

I've done this prototype in 64 Bit Red Hat Linux, some command may vary in other Linux distro.

Configuration

Host File
On client PC, add the following to hosts file so the virtual DNS could be resolved unto that IP to which corresponds to the IP of the server.

  • 10.10.25.140 jun.images.com
  • 10.10.25.140 jun.homepage.com

Port
Since this Proof of Concept will be hosted on same server, we would retain the current port 80 for APACHE, while for NGINX we could choose other ports e.g. 8000, 8888 or anything within that range whichever is available. Assigning which port will be covered under VHOST.

Vhost
Let's create a vhost in Apache under /etc/httpd/conf.d/vhosts named jun.homepage.vhost , it should contain the following code:

 
   DocumentRoot "/var/www/html/sites/jun.homepage.com"
   ServerName jun.homepage.com

   
       Options Indexes MultiViews FollowSymLinks
       AllowOverride All
       Order allow,deny
       Allow from all
   

  RewriteEngine On
  #RewriteLog "/var/log/httpd/rewrite.log"
  #RewriteLogLevel 9

  RewriteRule ^/(.*\.(js|css|rdf|xml|ico|txt|gif|jpg|png|jpeg))  http://127.0.0.1:8888/$1 [P,L]
  

As can be seen on the rewrite rule, any static content will be passed thru a specified URL (localhost in this case).


And for Nginx under /etc/nginx
  • Edit nginx.conf and include this line include /etc/nginx/conf.d/vhosts/*.vhost; at the bottom (before })
  • Create file under /etc/nginx/conf.d/vhosts, and name it jun.images.vhost , containing the following code:
    server {
             listen   8888;
             server_name  jun.images.com;
    
             location / { root /var/www/html/sites/jun.images.com/;
             index  index.html;
                         }
           }
Here we have specified Nginx to listen on port 8888, and the URL of server name. Under Location, this is where all static contents should be placed at the specified directory.


Sample Page
  • Place a sample image under /var/www/html/sites/jun.images.com/ name test1.jpg
  • Create an index.html under /var/www/html/sites/jun.homepage.com with the following code:
  • 
     <html>
        <head>
            <title>Image Pass-Thru POC</title>
        </head>
        <body>
            <img width="20%" height="20%" src="http://jun.homepage.com/test1.jpg" />
        </body>
     </html>
     
On this page (http://jun.homepage.com/index.html - take note that this is only local and may not be accessible elsewhere, you must substitute your own site to which you wish to simulate the test), we are referencing images as is but under the hood, the rewrite rule in apache would convert the current URL to another URL as pass-thru, meaning on the surface, user won't see any changes in terms of image URL.

Benchmark Results

With Reverse Proxy enabled, (second test on same setup is a repost which could use OS/hardware cache in the process)

ab -n 100 -c 100 http://jun.homepage.com/index.html

Benchmarking jun.homepage.com (be patient).....done

Server Software:        Apache/2.2.8
Server Hostname:        jun.homepage.com
Server Port:            80

Document Path:          /index.html
Document Length:        326 bytes

Concurrency Level:      100
Time taken for tests:   0.19610 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      59500 bytes
HTML transferred:       32600 bytes
Requests per second:    5099.44 [#/sec] (mean)
Time per request:       19.610 [ms] (mean)
Time per request:       0.196 [ms] (mean, across all concurrent requests)
Transfer rate:          2957.67 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    4   2.8      5       9
Processing:     3    6   2.2      7      10
Waiting:        2    6   2.1      6       9
Total:          3   11   4.8     12      19

Percentage of the requests served within a certain time (ms)
  50%     12
  66%     14
  75%     15
  80%     16
  90%     18
  95%     18
  98%     19
  99%     19
 100%     19 (longest request)

Repost:
 
Server Software:        Apache/2.2.8
Server Hostname:        jun.homepage.com
Server Port:            80

Document Path:          /index.html
Document Length:        326 bytes

Concurrency Level:      100
Time taken for tests:   0.16391 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      59500 bytes
HTML transferred:       32600 bytes
Requests per second:    6100.91 [#/sec] (mean)
Time per request:       16.391 [ms] (mean)
Time per request:       0.164 [ms] (mean, across all concurrent requests)
Transfer rate:          3538.53 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   2.2      3       7
Processing:     3    6   1.9      6       9
Waiting:        2    5   1.9      6       8
Total:          3    9   3.9     10      16

Percentage of the requests served within a certain time (ms)
  50%     10
  66%     12
  75%     13
  80%     14
  90%     15
  95%     15
  98%     15
  99%     16
 100%     16 (longest request)



And the result having served all contents in Apache (second test on same setup, is a repost which could use OS/hardware cache in the process)
ab -n 100 -c 100 http://jun.homepage.com/index.html

Benchmarking jun.homepage.com (be patient).....done


Server Software:        Apache/2.2.8
Server Hostname:        jun.homepage.com
Server Port:            80

Document Path:          /index.html
Document Length:        326 bytes

Concurrency Level:      100
Time taken for tests:   0.27270 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      59500 bytes
HTML transferred:       32600 bytes
Requests per second:    3667.03 [#/sec] (mean)
Time per request:       27.270 [ms] (mean)
Time per request:       0.273 [ms] (mean, across all concurrent requests)
Transfer rate:          2126.88 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    5   3.3      5      10
Processing:     4   13   3.0     14      17
Waiting:        3   12   3.2     13      16
Total:          4   18   5.7     20      27

Percentage of the requests served within a certain time (ms)
  50%     20
  66%     22
  75%     23
  80%     24
  90%     25
  95%     26
  98%     26
  99%     27
 100%     27 (longest request)

 
 Repost:
 
Server Software:        Apache/2.2.8
Server Hostname:        jun.homepage.com
Server Port:            80

Document Path:          /index.html
Document Length:        326 bytes

Concurrency Level:      100
Time taken for tests:   0.18472 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      59500 bytes
HTML transferred:       32600 bytes
Requests per second:    5413.60 [#/sec] (mean)
Time per request:       18.472 [ms] (mean)
Time per request:       0.185 [ms] (mean, across all concurrent requests)
Transfer rate:          3139.89 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   2.5      4       8
Processing:     3    6   2.3      6      10
Waiting:        2    5   2.5      6       9
Total:          3   10   4.6     10      18

Percentage of the requests served within a certain time (ms)
  50%     10
  66%     13
  75%     14
  80%     15
  90%     17
  95%     17
  98%     18
  99%     18
 100%     18 (longest request)


Summary
With reverse proxy enabled, it's able to serve static images much faster.

No comments:

Post a Comment