IIS 在Windows Server 实现反向代理

系统版本:    Windows Server 2019

IIS版本:    IIS 10.0.17763.1

需求描述: .Net Core 3.1写了一个Asp.Net Web API服务,端口是10000。外部需要通过80端口访问到这个服务。

解决步骤:

1,安装IIS

 

 

 

 

 勾选对应的选项,基本上一路Next下去就搞定。

iis安装完毕后,有一个默认的网站。

 

 我们需要做的,就是将外界对这个默认网站的访问路径(http://xxx.xxx.xxx.xxx:80),rewrite成为Asp.Net Web API的路径(http://xxx.xxx.xxx.xxx:10000)

 

2,在IIS安装“URL Rewrite” 以及 “Application Request Routing”

去微软官网下载 URL Rewrite , 安装。

安装后,在IIS除了会出现 URL Rewrite 2.1 模块,还会多出一个 Web Platform Install,我们可以用这个Installer来安装Application Request Routing

 

 双击Web Platform Installer,在搜索框输入“arr”,在搜索结果中选择Application Request Routing 3.0,安装。或去 Application Request Routing : The Official Microsoft IIS Site 下载安装亦可。

 

 

 

3,配置IIS Server的Application Request Routing

安装成功后,在IIS找到Application Request Routing Cache,双击,然后点击右侧的“Server Proxy Settings”

 

 

勾选“Enable proxy”,其余设置保持默认,点击右侧的“Apply”按钮即可。

 

4, 配置IIS默认网站的URL Rewrite

双击默认网站的URL Rewrite模块,"Add Rule(s)" -> "Blank Rule",然后按如下图设置:

 

在配置URL Rewrite之后,默认网站的文件夹会产生一个web.config文件,只要确定文件内容如下面所示即可:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://localhost:10000/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

 

 

这样做完后,外界访问http://xxx.xxx.xxx.xxx:80,就能访问到10000端口的Asp.Net Web API服务。

 

参考:

ReverseProxy IIS反向代理插件 官方安装版

IIS8如何安装和使用URL重写工具-URL Rewrite

 

posted @ 2022-05-11 22:40  AlvinLiang  阅读(3119)  评论(0编辑  收藏  举报