ABHIONLINUX
Site useful for linux administration and web hosting

2013/03/03

CpNginx as reverse proxy in cpanel servers

We all know that nginx is very faster than apache to handle static pages/image. In this section, I would like to demonstrate to configure nginx as reverse proxy to handle static pages/images and apache will be handling dynamic pages. This will really improve the high traffic server performance.

CpNginx is a cpanel plugin to manage nginx web service in cpanel server. Since nginx is vcry good in procession static files, we will configure nginx to run on port 80 and will forward dynamic request to apache on port 8081. Apache module mod_rpaf is the module that will be helping to configure this. First we need to install mod_rpaf. Following are the steps to install mod_rpaf to apache module directory.
===========

Login as root:
# cd /usr/local/src
# wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
# tar xvzf mod_rpaf-0.6.tar.gz
# cd mod_rpaf-0.6
# /usr/local/apache/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
===========

Following are the steps to install cpnginx in cpanel servers.
============
cd /usr/local/src
wget http://nginxcp.com/latest/nginxadmin.tar
tar xf nginxadmin.tar
cd publicnginx
./nginxinstaller install
=============

Before we install nginx, we need to change the apache port to 8081 from the tweak settings

WHM > Server Configuration > Tweak Settings > Apache non-SSL IP/port

Once this has been done, we need to run the commands given below to permanently save the apache configuration.


#  /usr/local/cpanel/whostmgr/bin/whostmgr2 –updatetweaksettings
#  /usr/local/cpanel/bin/apache_conf_distiller –update –main
#  /scripts/rebuildhttpdconf

Now you need to convert apache virtualhost to nginx virtualhosts. For that you can use the script given below and name it as nginx.sh and give the executable permission. Before that you need to create an empty file /usr/local/nginx/conf/vhost.conf

touch /usr/local/nginx/conf/vhost.conf 

Script to convert apache virtualhost
=============================
#!/bin/sh
 
cat > "/usr/local/nginx/conf/nginx.conf" <
user  nobody;
# no need for more workers in the proxy mode
worker_processes  1;
 
error_log  logs/error.log info;
 
worker_rlimit_nofile  8192;
 
events {
 worker_connections  512; # you might need to increase this setting for busy servers
 use epoll; #  Linux kernels 2.4.x  change to rtsig
}
 
http {
 server_names_hash_max_size 2048;
 
 include    mime.types;
 default_type  application/octet-stream;
 
 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 
 keepalive_timeout  10;
 
 gzip on;
 gzip_min_length  1100;
 gzip_buffers  4 32k;
 gzip_types    text/plain application/x-javascript text/xml text/css;
 ignore_invalid_headers on;
 
 client_header_timeout  3m;
 client_body_timeout 3m;
 send_timeout     3m;
 connection_pool_size  256;
 client_header_buffer_size 4k;
 large_client_header_buffers 4 32k;
 request_pool_size  4k;
 output_buffers   4 32k;
 postpone_output  1460;
 
 include "/usr/local/nginx/conf/vhost.conf";
}
 
EOF
 
/bin/cp /dev/null /usr/local/nginx/conf/vhost.conf
 
cd /var/cpanel/users
for USER in *; do
 for DOMAIN in `cat $USER | grep ^DNS | cut -d= -f2`; do
  IP=`cat $USER|grep ^IP|cut -d= -f2`;
  ROOT=`grep ^$USER: /etc/passwd|cut -d: -f6`;
  echo "Converting $DOMAIN for $USER";
 
  cat >> "/usr/local/nginx/conf/vhost.conf" <
   server {
  access_log off;
 
  error_log  logs/vhost-error_log warn;
  listen    80;
  server_name  $DOMAIN www.$DOMAIN;
 
  # uncomment location below to make nginx serve static files instead of Apache
  # NOTE this will cause issues with bandwidth accounting as files wont be logged
  #location ~* \.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|js|css)$ {
  # root   $ROOT/public_html;
  #}
 
  location / {
   proxy_send_timeout   90;
   proxy_read_timeout   90;
 
   proxy_buffer_size    4k;
   # you can increase proxy_buffers here to suppress "an upstream response
   #  is buffered to a temporary file" warning
   proxy_buffers     16 32k;
   proxy_busy_buffers_size 64k;
   proxy_temp_file_write_size 64k;
 
   proxy_connect_timeout 30s;
 
   proxy_redirect  http://www.$DOMAIN:81   http://www.$DOMAIN;
   proxy_redirect  http://$DOMAIN:81   http://$DOMAIN;
 
   proxy_pass   http://$IP:81/;
 
   proxy_set_header   Host   \$host;
   proxy_set_header   X-Real-IP  \$remote_addr;
   proxy_set_header   X-Forwarded-For \$proxy_add_x_forwarded_for;
  }
 }
EOF
 done
done
==================================================

# chmod 755 nginx.sh
# ./nginx.sh

Now you can test the nginx configuration using 

# root@server [~]# /usr/local/sbin/nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful

now you can start nginx using 
# /etc/init.d/nginx start






No comments:

Post a Comment