Nginx as Reverse Proxy in Front of Apache on CentOS/Redhat
Posted by admin in Unixtags: apache, centos, debian, freebsd, nginx, redhat, reverse proxy, ubuntu
Many peoples got problems with Apache Load if their website is having a high traffic.
There’re are also many solutions regarding this
- Buy new higher specs of Hardware
- Make a clustered webserver
- More optimization
What we will do in this article is MORE OPTIMIZATION
I assume that you already have Apache installed and running.
Nginx Installation
Download the latest version of nginx from nginx.net
wget http://sysoev.ru/nginx/nginx-0.7.63.tar.gz
Extract nginx
tar zxvf nginx-0.7.63.tar.gz
Now is the time to compile nginx
cd nginx-0.7.63
./configure –prefix=/etc/nginx
make && make install
If you got an error, make sure you need to install nginx dependencies such as
- libpcre
- libpcre-dev
- libssl
- libssl-dev
- zlib1g
- zlib1g-dev
If you have successfully installed nginx, then it’s the time to configure the nginx to works as we expected
When installation process, we have described that nginx is installed at /etc/nginx. So that the configuration location should be on /etc/nginx/conf/nginx.conf
Here’s the example of the nginx.conf
user www-data;
worker_processes 2;
error_log /var/log/error.log;events {
worker_connections 1024;
use epoll;
}server {
listen 80;
server_name your.server.name;
index index.php index.html;
root /path/to/your/root/directory;location = /1×1.gif {
empty_gif;
}location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|wav|bmp|rtf|js)$ {
expires 10d;
}location / {
proxy_pass http://127.0.0.1:8080/;
proxy_redirect off;proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_intercept_errors off;client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;proxy_buffer_size 8k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;}
}
Check with command nginx -t to make sure you have the correct configuration.
It should appear as below
# nginx -t
the configuration file /etc/nginx/conf/nginx.conf syntax is ok
configuration file /etc/nginx/conf/nginx.conf test is successful
Now it’s the time to reconfigure your apache settings.
Before we start, we need to add new modules of apache. This module is used for apache to read the REAL IP of the clients that’s forwarded by nginx.
Structure : Clients –> nginx –> Apache
If we don’t install this module, on apache logs you’ll see 127.0.0.1 as the clients. Because nginx and apache is on the same machine.
Installing rpaf Module for Apache
cd /usr/local/src
wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
tar xzf mod_rpaf-0.6.tar.gz
cd mod_rpaf-0.6
nano /usr/local/src/mod_rpaf-0.6/makefile
Change APXS=$(shell which apxs) to APXS=/usr/sbin/apxs.
Or to make sure, you can type whereis apxs to find your current apxs path
There’re 2 options to compile rpaf module
- make rpaf-2.0 && make install-2.0
- apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
Now edit your httpd.conf, and add the value below
LoadModule rpaf_module modules/mod_rpaf-2.0.so
#Mod_rpaf settings
RPAFenable On
RPAFproxy_ips 127.0.0.1
RPAFsethostname On
On apache virtual host settings, you should change from *:80 into *:8080 to make it works
Last step, we need to restart Apache service.
/etc/init.d/httpd restart
To start nginx service, please read Adding Nginx Startup Script on CentOS/RedHat
Entries (RSS)
[...] « Nginx as Reverse Proxy in Front of Apache on CentOS/Redhat Nov 12 2009 [...]