Monday-Friday, 9:00-17:00
 

IIS forward request(转发请求)

feature image

IIS forward request(转发请求)

最近部门要开发一个简单的APP,部分数据是现有项目已经存在的,为了方便维护,希望只提供一个交互的入口,并且协议的规则不变。

基于这个需求,有两套解决方案:

1.用代码将现有的api封装一层,对请求数据和返回数据不做任何改变,只是中转,然后和新的数据接口一起部署在一个项目里;

2.用IIS进行请求转发,调用现有接口回应请求,剩余部分开发新的api,部署在一个项目里,用URL Rewrite进行过滤分发。

第一个方案很传统,没什么好评价的,这里主要讲一下第二种方案的实现,第二个方案的好处是可以节省时间成本,需要依赖IIS插件(Application Request Routing + URL Rewrite)。

先下载ARR 和 URL Rewrite 进行安装,使用过程中发现ARR对IIS的“目录浏览”功能有依赖(未验证,如果无法使用,可以查看一下是否安装了“目录浏览”功能):

http://www.iis.net/downloads/microsoft/application-request-routing#additionalDownloads
http://www.iis.net/downloads/microsoft/url-rewrite#additionalDownloads

安装好插件,重新打开IIS

双击IIS根目录

双击Application Request Routing Cache

双击右边的 Server Proxy Settings

勾上 Enable proxy

取消勾选“Reverse rewrite host in response headers”,否则所有的响应内容的host都会被重写为当前站点域名,简单来讲,最直接的影响就是对外部站点的重定向都会失败,所以这里取消勾选。

点击“应用”后,新建一个站点,用来接受请求做转发

双击站点,双击 URL Rewrite -> Add Rules(新建规则) -> Blank rule(空白规则) 

Name:填写你的规则名称

Match URL 是匹配Requested URL的规则

http://www.test.com?name=michael&age=30

host: www.test.com

requested url: ?name=michael&age=30

query string: name=michael&age=30

Requested URL 选择 Matches the Pattern (匹配符合规则的url)

Using 选择 Regular Expressions (使用正则表达式来匹配)

Pattern 里填写 ^(.*) 这里不对正则表达式做讲解,有需要的可以自己了解。

勾选 Ignore case 忽略大小写

展开 Conditions 条件筛选

Logical grouping 选择 Match Any

Match All 是列表中所有规则都要匹配才符合(与)

Match Any 是列表中有一个规则匹配就算符合(或)

track capture group across conditions 跟踪捕获组,这个功能跟正则有关,这里不需要不勾选,可以查询关键词 capture group  自行了解详情

点击 Add 添加条件 

Condition input 填写  {HTTP_HOST}  ,HTTP_HOST 代表请求头里的host,就是上面例子里的 www.test.com 部分, 更多可过滤条件查询 Server Variables  自行了解

Check if input string 选择 Matches the Pattern 

Pattern 填写 ^arrtest.com$  ,这里的意思是如果host是 arrtest.com 则匹配通过,例:http://arrtest.com?asdf=1234

如果这里填写的是 ^www.arrtest.com$  ,则匹配 http://www.arrtest.com?asdf=1234

勾选 Ignore case 忽略大小写

双击展开 Action 部分

Action type 选择 Rewrite 重写转发

Rewrite URL 里填写 https://cn.bing.com/{R:1} 转发目标地址, {R:1} 代表 Match URL 部分匹配到的 Request URL 

勾选 Append query string 追加查询字符串 

到此配置结束,保存这个规则,在浏览器访问 http://arrtest.com/search?q=测试 就等同于访问 https://cn.bing.com/search?q=测试  

为了防止该站点下的其他接口被这个规则无脑转发,我们需要新增一个转发条件

现有的需要转发的 API 格式如下 http://arrtest.com?PROTOID=123456 

其他接口是没有 PROTOID 这个关键词的,并且 PROTOID 后面的value都是数字,那么这里就用这个关键词来过滤需要转发的请求

再回到刚刚的 Conditions 部分,点 Add 新增条件

Condition input 填写 {QUERY_STRING} 

Check if input string 选择 Matches the Pattern

Pattern 填写 PROTOID=\d+ 这个规则的意思是,匹配查询字符串为 PROTOID 开头参数值为数字的请求(例:http://arrtest.com/?PROTOID=456789)

勾选 Ignore case 忽略大小写

确定保存,修改 匹配逻辑为 Match All (与),列表内所有的规则都匹配,请求才会通过

现在只有 QueryString 为 PROTOID 开头参数值为数字的请求才会被转发了

例子:http://arrtest.com/search?PROTOID=4564&q=测试   =>   http://cn.bing.com/search?PROTOID=4564&q=测试

http://arrtest.com/search?q=测试&PROTOID=4564  则不会被转发 

至此请求转发的功能就实现了,除此之外,强大的 ARR + URL Rewrite 还可以实现高可用负载均衡。

https://www.cnblogs.com/mahatmasmile/p/5725253.html

10 Comments

It is with sad regret to inform you StarDataGroup.com is shutting down.
It has been a tough year all round and we decided to go out with a bang!

Any group of databases listed below is $49 or $149 for all 16 databases in this one time offer.
You can purchase it at http://www.StarDataGroup.com and view samples.

– LinkedIn Database
43,535,433 LinkedIn Records

– USA B2B Companies Database
28,147,835 Companies

– Forex
Forex South Africa 113,550 Forex Traders
Forex Australia 135,696 Forex Traders
Forex UK 779,674 Forex Traders

– UK Companies Database
521,303 Companies

– German Databases
German Companies Database: 2,209,191 Companies
German Executives Database: 985,048 Executives

– Australian Companies Database
1,806,596 Companies

– UAE Companies Database
950,652 Companies

– Affiliate Marketers Database
494,909 records

– South African Databases
B2B Companies Database: 1,462,227 Companies
Directors Database: 758,834 Directors
Healthcare Database: 376,599 Medical Professionals
Wholesalers Database: 106,932 Wholesalers
Real Estate Agent Database: 257,980 Estate Agents
Forex South Africa: 113,550 Forex Traders

Visit http://www.stardatagroup.com or contact us with any queries.

Kind Regards,
StarDataGroup.com

I go to see every day some websites and websites to read articles or reviews, except this weblog gives quality based content. Leia Niki Prebo

Last time I saw your Ma was when she way staying with pam in London as it turned out Pam was dying too. Sad, loved them both. Valeria Waverly Bekki

Can I just now say thats a relief to find someone that truly knows what theyre speaking about on the web. You certainly learn how to bring a problem to light and earn it critical. The best way to should check out this and can see this side from the story. I cant think youre no more common since you absolutely possess the gift. Joeann Kennedy Shotton

I have to voice my love for your kindness giving support to women who really need help with in this topic. Your real commitment to passing the message all through had been amazingly insightful and have in every case made somebody just like me to attain their objectives. The helpful tips and hints entails so much a person like me and extremely more to my office colleagues. Thanks a lot; from all of us. Lilllie Vaughn Sherar

just beneath, are many absolutely not associated web pages to ours, having said that, they may be surely worth going over Cathrin Vasilis Fillian

Hiya, I am really glad I have found this info. Today bloggers publish only about gossip and web stuff and this is really frustrating. A good site with exciting content, this is what I need. Thank you for making this site, and I will be visiting again. Do you do newsletters by email? Katrina Nils Dunlavy

Leave a Reply

Your email address will not be published. Required fields are marked *

Post navigation

  Next Post :