如何配置IIS 10以将请求转发给Web应用程序

huangapple go评论51阅读模式
英文:

How to configure IIS 10 to forward requests to web apps

问题

I want IIS 10 to redirect requests to several other web apps (ASP.NET Core 6.0 and Angular 14) <br/>
我想让 IIS 10 将请求重定向到几个其他Web应用程序(ASP.NET Core 6.0 和 Angular 14) <br/>

Let's say I have Kestrel listening on http://localhost:5009/ serving one of those apps, I want to type
假设我有Kestrel监听 http://localhost:5009/ 为其中一个应用提供服务,我想要输入

http://localhost/PassThrough/

into browser address and to have it redirected to
浏览器地址中输入并要求将其重定向到

http://localhost:5171/

And also
还有

http://localhost/PassThrough/todo/param1

to

http://localhost:5171/todo/param1

UPDATE:

Tried a rewrite URL method suggested by @TengFeiXie
更新

尝试了@TengFeiXie建议的重写URL方法

如何配置IIS 10以将请求转发给Web应用程序
如何配置IIS 10以将请求转发给Web应用程序

如何配置IIS 10以将请求转发给Web应用程序
如何配置IIS 10以将请求转发给Web应用程序

and a bunch of variants. None of it worked.
以及其他几种变体。都没有奏效。

The problem with this method I think is IIS will only allow you to match a pattern after the first / and if you rewrite the matched part it will not add port in the correct place (before the first /).
我认为这种方法的问题在于IIS只允许您在第一个/之后匹配模式,如果您重写匹配的部分,它不会在正确的位置(在第一个/之前)添加端口。

Redirect will also not redirect correctly for the same reason.
由于相同的原因,重定向也不会正确重定向。

The second suggestion - reverse proxy also did not bare fruit:
第二个建议 - 反向代理也没有取得成果:

Both
两者

	&lt;rewrite&gt;
		&lt;rules&gt;
			&lt;rule name=&quot;Reverse Proxy to webmail&quot; stopProcessing=&quot;true&quot;&gt;
				&lt;match url=&quot;^PassThrough/(.*)&quot; /&gt;
				&lt;action type=&quot;Rewrite&quot; url=&quot;http://localhost:5171/{R:1}&quot; /&gt;
			&lt;/rule&gt;
		&lt;/rules&gt;
	&lt;/rewrite&gt;

and
以及

	&lt;rewrite&gt;
		&lt;rules&gt;
			&lt;rule name=&quot;Reverse Proxy to webmail&quot; stopProcessing=&quot;true&quot;&gt;
				&lt;match url=&quot;http://localhost/PassThrough/(.*)&quot; /&gt;
				&lt;action type=&quot;Rewrite&quot; url=&quot;http://localhost:5171/{R:1}&quot; /&gt;
			&lt;/rule&gt;
		&lt;/rules&gt;
	&lt;/rewrite&gt;

in web.config
在web.config中

end up redirecting to
最终重定向到

http://localhost/localhost:5171

instead of
而不是

http://localhost:5171

So same issue basically in both suggestions. It boils down to setting up the rule. If you are able to provide the rule that works let's say in xml form I'd be happy to test it.
基本上两种建议都有相同的问题。问题归结为设置规则。如果您能提供有效的规则,比如以XML形式,我会很愿意测试它。
<br/>
Latest:
最新的:

	&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
	&lt;configuration&gt;
		&lt;system.webServer&gt;
			&lt;rewrite&gt;
				&lt;rules&gt;
					&lt;rule name=&quot;PassThrough&quot; stopProcessing=&quot;true&quot;&gt;
						&lt;match url=&quot;http://localhost/PassThrough(/(.*))*&quot; /&gt;
						&lt;action type=&quot;Redirect&quot; url=&quot;http://localhost:5171{R:1}&quot; /&gt;
					&lt;/rule&gt;
				&lt;/rules&gt;
			&lt;/rewrite&gt;
		&lt;/system.webServer&gt;
	&lt;/configuration&gt;
英文:

I want IIS 10 to redirect requests to several other web apps (ASP.NET Core 6.0 and Angular 14) <br/>
Let's say I have Kestrel listening on http://localhost:5009/ serving one of those apps, I want to type

http://localhost/PassThrough/ 

into browser address and to have it redirected to

http://localhost:5171/

And also

http://localhost/PassThrough/todo/param1

to

http://localhost:5171/todo/param1

UPDATE:

Tried a rewrite URL method suggested by @TengFeiXie
如何配置IIS 10以将请求转发给Web应用程序

如何配置IIS 10以将请求转发给Web应用程序

and a bunch of variants. None of it worked.
The problem with this method I think is IIS will only allow you to match a pattern after the first / and if you rewrite the matched part it will not add port in the correct place (before the first /).
Redirect will also not redirect correctly for the same reason.

The second suggestion - reverse proxy also did not bare fruit:
Both

	&lt;rewrite&gt;
		&lt;rules&gt;
			&lt;rule name=&quot;Reverse Proxy to webmail&quot; stopProcessing=&quot;true&quot;&gt;
				&lt;match url=&quot;^PassThrough/(.*)&quot; /&gt;
				&lt;action type=&quot;Rewrite&quot; url=&quot;http://localhost:5171/{R:1}&quot; /&gt;
			&lt;/rule&gt;
		&lt;/rules&gt;
	&lt;/rewrite&gt;

and

	&lt;rewrite&gt;
		&lt;rules&gt;
			&lt;rule name=&quot;Reverse Proxy to webmail&quot; stopProcessing=&quot;true&quot;&gt;
				&lt;match url=&quot;http://localhost/PassThrough/(.*)&quot; /&gt;
				&lt;action type=&quot;Rewrite&quot; url=&quot;http://localhost:5171/{R:1}&quot; /&gt;
			&lt;/rule&gt;
		&lt;/rules&gt;
	&lt;/rewrite&gt;

in web.config
end up redirecting to

http://localhost/localhost:5171

instead of

http://localhost:5171

So same issue basically in both suggestions. It boils down to setting up the rule. If you are able to provide the rule that works let's say in xml form I'd be happy to test it.
<br/>
Latest:

	&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
	&lt;configuration&gt;
		&lt;system.webServer&gt;
			&lt;rewrite&gt;
				&lt;rules&gt;
					&lt;rule name=&quot;PassThrough&quot; stopProcessing=&quot;true&quot;&gt;
						&lt;match url=&quot;http://localhost/PassThrough(/(.*))*&quot; /&gt;
						&lt;action type=&quot;Redirect&quot; url=&quot;http://localhost:5171{R:1}&quot; /&gt;
					&lt;/rule&gt;
				&lt;/rules&gt;
			&lt;/rewrite&gt;
		&lt;/system.webServer&gt;
	&lt;/configuration&gt;

答案1

得分: 1

IIS提供官方组件,URL重定向反向代理都可以满足您的需求。

  1. 获取URL重写模块:
    https://iis-umbraco.azurewebsites.net/downloads/microsoft/url-rewrite
    您可以参考官方文档中的步骤来创建重定向规则,将http://localhost/App1/重定向到http://localhost:5009/。

  2. 反向代理需要应用程序请求路由:
    https://iis-umbraco.azurewebsites.net/downloads/microsoft/application-request-routing

    针对反向代理的配置规则:
    https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/reverse-proxy-with-url-rewrite-v2-and-application-request-routing#configuring-rules-for-the-reverse-proxy

URL重定向和反向代理的官方文档具有详细的步骤和参考示例。

更新:

好的,看来您对重写规则不太熟悉。我将演示从**http://localhost/PassThrough/http://localhost:5171/**的步骤。您会发现这非常容易。

我在端口5174上有一个article.html

如何配置IIS 10以将请求转发给Web应用程序

然后我在Default Web Site(端口80)中添加了以下入站规则。

如何配置IIS 10以将请求转发给Web应用程序

在Web.config中

            &lt;rules&gt;
                &lt;rule name=&quot;ReverseProxy&quot; stopProcessing=&quot;true&quot;&gt;
                    &lt;match url=&quot;passthrough/(.*)&quot; /&gt;
                    &lt;action type=&quot;Rewrite&quot; url=&quot;http://localhost:5171/{R:1}&quot; /&gt;
                &lt;/rule&gt;
            &lt;/rules&gt;

请求:http://localhost/PassThrough/article.html。成功获取article.html

如何配置IIS 10以将请求转发给Web应用程序

英文:

IIS provides official components, URL Redirection or Reverse Proxy both can meet your needs.

  1. Get the URL Rewrite module:
    https://iis-umbraco.azurewebsites.net/downloads/microsoft/url-rewrite.
    You can refer to the steps in the official documentation to
    create redirect rules, redirect from http://localhost/App1/ to
    http://localhost:5009/.

  2. Reverse Proxy needs Application Request Routing:
    https://iis-umbraco.azurewebsites.net/downloads/microsoft/application-request-routing.

    Configuring Rules for the Reverse Proxy:
    https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/reverse-proxy-with-url-rewrite-v2-and-application-request-routing#configuring-rules-for-the-reverse-proxy.

The official documentation for URL redirection and reverse proxy has detailed steps and reference examples.

Update:

OK, It seems you are not familiar with rewrite rules. I'll demonstrate the steps to go from http://localhost/PassThrough/ to http://localhost:5171/. You'll find it's really easy.

I have an article.html on port 5174.

如何配置IIS 10以将请求转发给Web应用程序

Then I added the following inbound rules in Default Web Site(port 80).

如何配置IIS 10以将请求转发给Web应用程序

In Web.config

            &lt;rules&gt;
                &lt;rule name=&quot;ReverseProxy&quot; stopProcessing=&quot;true&quot;&gt;
                    &lt;match url=&quot;passthrough/(.*)&quot; /&gt;
                    &lt;action type=&quot;Rewrite&quot; url=&quot;http://localhost:5171/{R:1}&quot; /&gt;
                &lt;/rule&gt;
            &lt;/rules&gt;

Request: http://localhost/PassThrough/article.html. Successfully got the article.html.

如何配置IIS 10以将请求转发给Web应用程序

huangapple
  • 本文由 发表于 2023年4月10日 22:59:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定