Nginx Catch-All Host As Front End To Apache For ISPConfig 2 On CentOS 5

Want to support HowtoForge? Become a subscriber!
 
Submitted by poustchi (Contact Author) (Forums) on Mon, 2010-10-18 18:04. :: CentOS | ISPConfig | nginx

Nginx Catch-All Host As Front End To Apache For ISPConfig 2 On CentOS 5

Nginx (pronounced “engine X”) is a lightweight, high-performance Web server/reverse proxy and e-mail (IMAP/POP3) proxy, licensed under a BSD-like license. It runs on UNIX, GNU/Linux, BSD variants, Mac OS X, Solaris, and Microsoft Windows. Apache's performance is generally very good as well. However, in a resource-constrained environment , Apache does not necessarily provide optimal performance or resource utilization. One of the “best practice” approaches to reducing the strain on Apache is to let a leaner lighter web server take over the serving of the site's static files (images, javascript, css, etc.), leaving the heavy lifting of serving the dynamic content to Apache. This tutorial shows how you can setup Nginx as front end to apache for ISPConfig 2 On CentOS 5.x.

I do not issue any guarantee that this will work for you!

 

Configure ISPConfig 2

First, you need to change ISPConfig 2 config file as follows :

cp -v /root/ispconfig/scripts/lib/config.lib.php /root/ispconfig/scripts/lib/config.lib.php.orig
vi /root/ispconfig/scripts/lib/config.lib.php

Find line number 1283 and change port 80 to 8080:

$web_port = ":80";

$web_port = ":8080";

Find line numbers 2088 and 2089 and change port 80 to 8080 as well:

  $test_vhost = "\n".'<virtualhost 10.0.0.1:80="">
ServerName www.test.tld:80
ServerAdmin webmaster@test.tld
DocumentRoot /home
'.$web_httpd_include.'
</virtualhost>';

  $test_vhost = "\n".'<virtualhost 10.0.0.1:8080="">
ServerName www.test.tld:8080
ServerAdmin webmaster@test.tld
DocumentRoot /home
'.$web_httpd_include.'
</virtualhost>';

If you like much simpler method use :

sed -ie 's/:80/:8080/g' /root/ispconfig/scripts/lib/config.lib.php

 

Configure Apache

Change the default listening port in Apache from 80 to 8080.

vi /etc/httpd/conf/httpd.conf

Listen 80
UseCanonicalName Off

Listen 8080
UseCanonicalName On

 

Install Nginx

To install nginx web server:

yum install httpd-devel pcre perl pcre-devel zlib zlib-devel
cd /tmp
wget http://download.fedora.redhat.com/pub/epel/5/i386/nginx-0.6.39-5.el5.i386.rpm
rpm -ivh nginx-0.6.39-5.el5.i386.rpm

Note: if you like to install latest version of nginx please check this link : Install Nginx On CentOS 5.5 With SSL, PCRE, GeoIP, Zlib, Gzip And DAV Support

And now, you must configure nginx.

vi /etc/nginx/nginx.conf

user nginx;
worker_processes 4;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
#----------------------------------------------------------------------
# Events Module 
#
# http://wiki.codemongers.com/NginxEventsModule
#
#----------------------------------------------------------------------
events {
	worker_connections 1024;
	use epoll;
}
#----------------------------------------------------------------------
# HTTP Core Module
#
# http://wiki.codemongers.com/NginxHttpCoreModule 
#
#----------------------------------------------------------------------
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;
	gzip_vary on;
	gzip_comp_level 6;
	gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
	gzip_buffers 16 8k;
	gzip_disable "MSIE [1-6].(?!.*SV1)";
	
	# 
	proxy_temp_path /var/cache/nginx/temp;
	proxy_cache_path /var/cache/nginx/cached levels=1:2 keys_zone=global:60m inactive=15m max_size=1G;
	proxy_cache_valid    200 302 10m;
	proxy_cache_valid    301 1h;
	proxy_cache_valid    404 3m;	
	proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504; 
	proxy_cache_key "$scheme$host$request_uri $cookie_user";
	#
	
	upstream apache {
	server PLACE-IP-ADDRESS-OF-YOUR-SERVER-HERE:8080;
	}
	###########################################################
	#
	# The default server
	#
	server {
	
		proxy_cache global;
		
		listen 80;
		server_name _;
		server_name_in_redirect off;
		resolver 127.0.0.1;
		server_tokens off;
		
		if ($host ~* ^(www\.)(.+)) {
			set $rawdomain $2;
			rewrite ^/(.*)$ http://$rawdomain/$1 permanent;
		}
		
		client_max_body_size 30m;
		client_body_buffer_size 256k;
		
		location / {
		
		if (-d $request_filename) {
			rewrite ^/(.+[^/])$ http://$host/$1/ permanent;
		}
		
		proxy_pass http://apache;
		proxy_redirect off;
		proxy_pass_header Set-Cookie;
		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 120;
		proxy_send_timeout 120;
		proxy_read_timeout 120;
		proxy_buffer_size 4k;
		proxy_buffers 4 32k;
		proxy_busy_buffers_size 64k;
		
		root /var/www/www.$host/web;
			index index.php index.html index.htm default.htm default.html;
		}
		
		error_page 404 /404.html;
		location = /404.html {
			root /usr/share/nginx/html;
		}
		
		# redirect server error pages to the static page /50x.html
		#
		error_page 500 502 503 504 /50x.html;
		location = /50x.html {
			root /usr/share/nginx/html;
		}
		
		location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|swf|flv|mp3)$ {
			root /var/www/www.$host/web; 
			access_log off;
			proxy_cache_valid 200 10h;
			expires 3d;
		}
	}
	include /etc/nginx/conf.d/*.conf;
}

Important note: Do not forget to replace IP ADDRESS of your Apache server in this config file.

 

Restart required services and enjoy!

Restart ISPConfig and Apache services and start the nginx service.

service httpd restart
service ispconfig_server restart
service nginx start

 

Testing

First, check the virtual host ispconfig file:

cat /etc/httpd/conf/vhosts/Vhosts_ispconfig.conf

It must look like:

######################################
# Vhost: www.example.com:8080
######################################
#
#
<virtualhost xxx.xxx.xxx.xxx:8080="">ServerName www.example.com:8080
ServerAdmin info@example.com
DocumentRoot /var/www/web100/web
...
</virtualhost>

Secondly, check the nginx log and error files.

tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log

Restart the server if required.


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 Aaron M (not registered) on Mon, 2010-12-13 06:38.

I'm looking at using the Nginx as front end for Apache using ISPconfig 3. Has anyone tried this?