Nginx捕获所有主机作为前端到Apache对于ISPConfig 2在CentOS 5

Nginx将所有主机作为前端到Apache为ISPConfig 2在CentOS 5上

Nginx (发音为“引擎X”)是一种轻量级的高性能Web服务器/反向代理和电子邮件(IMAP / POP3)代理,根据类似BSD的许可证许可。 它运行于UNIX,GNU / Linux,BSD变体,Mac OS X,Solaris和Microsoft Windows。 Apache的表现一般也很好。 然而,在资源有限的环境中,Apache不一定能提供最佳性能或资源利用率。 减少Apache压力的“最佳实践”方法之一就是让更轻的Web服务器接管网站的静态文件(图像,javascript,css等)的服务,从而使服务动态内容到Apache。 本教程将介绍如何将Nginx设置为ISPConfig 2 On CentOS 5.x的前端到apache。

我不会保证这将为您工作!

配置ISPConfig 2

首先,您需要更改ISPConfig 2配置文件,如下所示:

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

查找行号1283并将端口80更改为8080:

$web_port = ":80";

$web_port = ":8080";

查找行号2088和2089,并将端口80更改为8080:

  $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>';

如果你喜欢使用更简单的方法:

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

配置Apache

将Apache中的默认监听端口从80更改为8080。

vi /etc/httpd/conf/httpd.conf
Listen 80
UseCanonicalName Off

Listen 8080
UseCanonicalName On

安装Nginx

要安装nginx Web服务器:

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

注意:如果你喜欢安装最新版本的nginx,请查看此链接: 安装Nginx On CentOS 5.5使用SSL,PCRE,GeoIP,Zlib,Gzip和DAV支持

现在,您必须配置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;
}

重要提示:请勿忘记在此配置文件中替换您的Apache服务器的IP ADDRESS。

重新启动所需服务,享受!

重新启动ISPConfig和Apache服务并启动nginx服务。

service httpd restart
service ispconfig_server restart
service nginx start

测试

首先检查虚拟主机ispconfig文件:

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

它必须看起来像:

######################################
# 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>

其次,检查nginx日志和错误文件。

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

如果需要,重新启动服务器。

赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏