Home » Linux » How To » Apache Reverse Proxy Nginx

Apache Reverse Proxy Nginx

We know Apache is the most popular web server, because of its flexibility. It is opensource, there is lots of module to handle different types of language like Python, Perl, Rubi, PHP. But apache have some issue with thread & process management. Apache slows down under heavy load, because of the need to spawn new processes, thus consuming more computer memory. So basically apache is excellent in handling dynamic content but bad in serving static content due to its memory & CPU consumption.

In other hand Nginx can serve static content quicker, it has small memory footprint & opensource.
Apache reverse proxy nginx reduce memory & CPU consumption of server. It deliver deliver static content like image, CSS, JS more faster than Apache.

In this tutorial I will show you how to configure apache reverse proxy nginx or configure nginx as frontend proxy of apache.


Test Environment

OS : Centos 6.4 x64_86
IP :
Apache : 2.2
Nginx : 1.6

Install required packages

[root@localhost ~]# yum install httpd httpd-devel

Now edit Apache main configuration file

[root@localhost ~]# nano /etc/httpd/conf/httpd.conf

Find the line “Listen 80” & Change it to 8080

Listen 8080

Go to to bottom of the file to make changes

 ServerAdmin webmaster@mysite.com
 DocumentRoot /var/www/mysite.com
 ServerName mysite.com
 ServerAlias www.mysite.com
 <Directory "/var/www/mysite.com">
 Options FollowSymLinks -Includes
 AllowOverride All
 Order allow,deny
 Allow from all
 RewriteEngine on
 ErrorLog logs/mysite.com-error_log
 CustomLog logs/mysite.com-access_log common

Now ready nginx repository

[root@localhost ~]# nano /etc/yum.repos.d/nginx.repo
 name=nginx repo
[root@localhost ~]# yum install nginx
Running Transaction
 Installing : nginx-1.6.2-1.el6.ngx.x86_64 1/1
Thanks for using nginx!
Please find the official documentation for nginx here:
 * http://nginx.org/en/docs/
Commercial subscriptions for nginx are available on:
 * http://nginx.com/products/
 Verifying : nginx-1.6.2-1.el6.ngx.x86_64 1/1
 nginx.x86_64 0:1.6.2-1.el6.ngx

Create A new file mysite.com.conf under /etc/nginx/conf.d/

[root@localhost ~]# nano /etc/nginx/conf.d/mysite.com.conf

Put the following code into the file.

server {
 listen 80;
 server_name mysite.com www.mysite.com;
 access_log /var/log/nginx/mysite.com-access_log ;
 error_log /var/log/nginx/mysite.com-error_log ;
location ~* .(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|html|htm|wml)$ {
 root /var/www/mysite.com;
 expires 365d;
# All files are cashed for 120 minutes and up to 200 pages, this have to be removed
 location / {
 root /var/www/mysite.com;
 proxy_redirect off;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 client_max_body_size 10m;
 client_body_buffer_size 128k;
 proxy_connect_timeout 90;
 proxy_send_timeout 90;
 proxy_read_timeout 90;
 proxy_buffers 32 4k;
 # If the backend server send one of these error Nginx will serve from cashed files
 proxy_cache_use_stale error timeout invalid_header updating;

Make the document root directory

[root@localhost ~]# mkdir /var/www/mysite.com

Start Apache service

[root@localhost ~]# chkconfig httpd on
[root@localhost ~]# service httpd start

Check if website is opening or not

[root@localhost ~]# curl http://mysite.com
 Its Working!

check which server is serving

 [root@localhost ~]# curl -I http://mysite.com
 HTTP/1.1 200 OK
 Server: nginx/1.6.2
 Date: Sun, 22 Mar 2015 18:33:03 GMT
 Content-Type: text/html; charset=UTF-8
 Content-Length: 13
 Connection: keep-alive
 Last-Modified: Sun, 22 Mar 2015 17:39:34 GMT
 ETag: "40db5-d-511e408668751"
 Accept-Ranges: bytes

Check Also

Password less SSH authentication

Password less SSH authentication is one of the best security practices to avoid any password …

Leave a Reply

Your email address will not be published. Required fields are marked *