Reduce Apache's Load With Nginx On RHEL 5.2

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Submitted by Nikitos (Contact Author) (Forums) on Mon, 2008-07-21 18:21. :: CentOS | Apache

Reduce Apache's Load With Nginx On RHEL 5.2

This how-to describes how to install and configure Nginx to accelerate an Apache server based on RHEL 5.2.

 

1. Update your installed packages

yum install update

 

2. Install new packages

yum install -y httpd-devel

wget http://rpmfind.net/linux/EPEL/5Server/i386/nginx-0.6.31-3.el5.i386.rpm

rpm nginx-0.6.31-3.el5.i386.rpm

 

3. Edit /etc/nginx/nginx.conf

Setting up nginx as a reverse proxy:

nano /etc/nginx/nginx.conf

Change worker_processes 1; To worker_processes 2;

Replace

http {
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  log_format main '$remote_addr - $remote_user [$time_local] $request '
  '"$status" $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';
  access_log /var/log/nginx/access.log main;
  sendfile on;
  #tcp_nopush on;
  #keepalive_timeout 0;
  keepalive_timeout 65;
  #gzip on;

# Load config files from the /etc/nginx/conf.d directory
 include /etc/nginx/conf.d/*.conf;
#
# The default server
#
server {
listen 80;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

with

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
gzip on;
# Load config files from the /etc/nginx/conf.d directory
include /etc/nginx/conf.d/*.conf;
#
# The default server
#
server {
listen 80;
server_name YOURSERVERNAME;
#charset koi8-r;
#access_log logs/host.access.log main;
#Main location
location / {
proxy_pass http://127.0.0.1:8080/;
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;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
root /usr/share/nginx/html;
index index.html index.htm index.php;
}

This function allows nginx to work with these file types:

Change

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

to

location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|wav|bmp|rtf|js)$ {root /var/www;}

 

4. Check nginx conf

nginx -t

The result must be like this:

2008/07/17 16:09:46 [info] 17197#0: the configuration file /etc/nginx/nginx.conf syntax is ok
2008/07/17 16:09:46 [info] 17197#0: the configuration file /etc/nginx/nginx.conf was tested successfully

 

5. Installing rpaf-2.0 module

This module gives active Nginx IPs to 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

 

Compiling rpaf module:

nano /usr/local/src/mod_rpaf-0.6/makefile

Change APXS=$(shell which apxs) to APXS=/usr/sbin/apxs.

Compile ways:

I)

make rpaf-2.0 && make install-2.0

II)

apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

 

6. Edit httpd.conf

nano /etc/httpd/conf/httpd.conf

Add

...
LoadModule rpaf_module modules/mod_rpaf-2.0.so
....

#Mod_rpaf settings
RPAFenable On
RPAFproxy_ips 127.0.0.1 REALIP1 REALIP2
RPAFsethostname On

Change Listen 80 to Listen 8080, because nginx will use port 80.

 

7. Restart Apache & start Nginx

service httpd restart

service nginx start

 

8. View results

Apache test page by Nginx:


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Tue, 2009-07-28 05:32.
Great tutorial. Works as described but if you are running Apache with many different vhosts and vhosts with their own home directory, you will run into problems.  if anyone knows how to do the vhosts with this tutorial, that would be awesome.
Sponsored Links: Unified Communications: Thoughts, Strategies and Predictions
Join the discussion.
www.seamlessenterprise.com

IP Convergence
Integrate your wireless and wireline networks.
Learn how from the experts at Sprint.
www.seamlessenterprise.com

Wireless & Wireline Integration
Thoughts, strategies and solutions: join the discussion
www.seamlessenterprise.com

Unified Communications 2009
Join the Discussion. Now.
www.seamlessenterprise.com

Red Hat Virtual Experience - a free virtual event. Dec. 9th