让apache反向代理iis及多域名设置

让apache反向代理iis及多域名设置

apache与iis是目前最流行,集合较强功能与扩展,拥有较高稳定性的两大服务器软件,但是两者分别支持php和asp程序,

之前研究了在iis6下建立全能主机的方法,,当然目前的iis 通过安装dll扩展能够支持php,但其执行效率没有apache高.

现在再尝试在iis6下安装apache,让两者共同工作.

因为在xp下安装iis,则不能支持多个虚拟主机绑定多个域名,因此,今天我们来探讨apache反向代理iis来支持asp,以及这种情况下的多个虚拟主机绑定域名的方法,以及在iis需要使用rewrite时该如何分别设置apache及iis的rewrite规则。

一、首先安装好apache及iis,设置apache端口80,iis端口81(这里只要别和apache冲突就行了)

二、找到apache的配置文件httpd.conf,去掉下面语句的注释

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_connect_module modules/mod_proxy_connect.so

LoadModule proxy_http_module modules/mod_proxy_http.so

LoadModule proxy_ftp_module modules/mod_proxy_ftp.so

这样你的apache就可以以80端口做代理服务器啦

三、建立虚拟主机,以主机ip为123.0.0.1为例,以建立一个ip直接访问和两个不同域名绑定不同目录访问为例。

先建立默认虚拟主机,即直接输入ip时所能访问到的内容,在httpd.conf中添加如下内容(或找到类似内容进行修改):

#默认虚拟主机

NameVirtualHost 123.0.0.1



ServerName 123.0.0.1

DocumentRoot “D:/www/docs”



Options FollowSymLinks IncludesNOEXEC Indexes

DirectoryIndex index.html index.htm default.htm index.php default.php index.cgi default.cgi index.pl default.pl index.shtml

AllowOverride None

Order Deny,Allow

Allow from all





接着以域名a.b.c绑定至该ip下目录“d:\www\abc”为例

NameVirtualHost 123.0.0.1



ServerName a.b.c

DocumentRoot “D:/www/abc”



Options FollowSymLinks IncludesNOEXEC Indexes

DirectoryIndex index.html index.htm default.htm index.php default.php index.cgi default.cgi index.pl default.pl index.shtml

AllowOverride None

Order Deny,Allow

Allow from all





以此类推,绑定域名d.e.f至“D:\www\def”,同理如下

NameVirtualHost 123.0.0.1



ServerName d.e.f

DocumentRoot “D:/www/def”



Options FollowSymLinks IncludesNOEXEC Indexes

DirectoryIndex index.html index.htm default.htm index.php default.php index.cgi default.cgi index.pl default.pl index.shtml

AllowOverride None

Order Deny,Allow

Allow from all





四、asp页面访问,apache反向代理iis,以及rewrite综合设置。

要在apac

he中访问asp,则需要用到proxypass,即在第二步中先打开的代理模块。

我们在第三步设置了两个虚拟主机“a.b.c”和“d.e.f”,下面以这两个域名绑定的主机为例说明在apache反向代理情况下,如何设置域名绑定,

一种方法是加入以下标为“蓝色”的语句

NameVirtualHost 123.0.0.1



ServerName a.b.c

DocumentRoot “D:/www/abc”

proxypass /test(此红括号为说明:该“test”目录是你浏览器中输入的,例“http://a.b.c/test”) http://123.0.0.1:81/test(此红括号为说明:该“test”是你iis中设置的虚拟目录,其中端口81为iis端口)

proxyreverse /test http://123.0.0.1:81/test

# 请使用相同目录名,否则路径会出错,就是通过在浏览器中输入a.b.c,会访问“d:\www\abc\”根目录下文件;若输入a.b.c/test,就会通过apache的反向代理访问至iis下的test目录(或test虚拟目录),以此类推,可以相应多设置需要执行asp的目录。

#特别注意,此段代码别加到之间。



Options FollowSymLinks IncludesNOEXEC Indexes

DirectoryIndex index.html index.htm default.htm index.php default.php index.cgi default.cgi index.pl default.pl index.shtml

AllowOverride None

Order Deny,Allow

Allow from all





另一种方法是使用apache的url rewrite来实现将需要执行的asp文件通过反向代理传递给iis执行,此种方法的优点是:1.htm,html,图片等文件可由apache完成执行,提高效率,而asp则通过rewrite规则传递给iis解释;2.在asp程序需要iis的rewrite时,通过该方法传递给iis,iis的rewrite规则仍然有效。即在iis中开启rewrite时,浏览器中输入http://123.0.0.1:808/test/index.html ,实际访问的是http://123.0.0.1:808/test/index.asp ;如果使用的第一种反向代理法,在浏览器中输入http://a.b.c/test/index.html,会出现找不到页面,若使用接下来来讲解的方法,将asp和需要rewrite的index.html等传递给iis解释,就能实现iis的rewrite在apache反向代理情况下不失效。例子如下(“蓝色”的语句为新添加代码):

NameVirtualHost 123.0.0.1



ServerName a.b.c

DocumentRoot “D:/www/abc”

RewriteEngine on

#禁止访问global.asa,global.asax,web.config

RewriteCond %{REQUEST_URI} ^.*(global\.asa|global\.asax|web\.config).*$ [NC]

RewriteRule ^/test/(.*) http://123.0.0.1:808/test/$1 [F,L]

#把所有对asp和aspx文件的请求转到iis处理

RewriteCond %{REQUEST_URI} ^/.*(\.asp)x?\??.*$ [NC]

RewriteRule ^/test/(.*) http://123.0.0.1:808/test/$1 [P]

#把html文件的请求转到iis处理

RewriteCond %{REQUEST_URI} ^/.*(\.html)?\??.*$ [NC]

RewriteRule ^/test/(.*) http://123.0.0.1:808/test/$1 [P]

#请使用相同目录名,否

则子目录路径会出错,导致图片、css等放在子目录的文件调用出错,此时浏览器输入“a.b.c/test/index.html”和“a.b.c/test/index.asp”时都能正常显示页面。

#特别注意,此段代码别加到之间。



Options FollowSymLinks IncludesNOEXEC Indexes

DirectoryIndex index.html index.htm default.htm index.php default.php index.cgi default.cgi index.pl default.pl index.shtml

AllowOverride None

Order Deny,Allow

Allow from all




相关文档
最新文档