为什么匹配规则重写会进入带有iisnode模块的URL



我使用的是windows 2016和IIS 10(这里的版本不太相关,只是说(,我正在使用模块iisnode将windows身份验证获取到Node.js中。一切都很好,但我已经考虑了几个小时的配置,我不知道如何增加一点改进。

目前的情况是我有一个脚本hello.js

var express = require('express');
var app = express.createServer(); // BTW why this deprecated syntax here? 
// maybe just because I've copied the folder from the old examples delivered in the setup!?
app.get('/toh-api/rest/foo', function (req, res) {
res.send('Hello from foo! [express sample]');
});
app.get('/toh-api/rest/bar', function (req, res) {
var username = req.headers['x-iisnode-auth_user'];
const logonuser = req.headers["x-iisnode-logon_user"];
var authenticationType = req.headers['x-iisnode-auth_type'];
console.log('username',username);
console.log('authenicationType',authenticationType)
res.send('Hello ' + username + ':' + logonuser + ' from bar! [express sample] ' + authenticationType);
});

app.all('/toh-api/rest/hello.js', function (req, res) {
res.send('Hello from hello.js! [express sample] ');
});

和web.config

<configuration>
<system.webServer>

<handlers>
<add name="iisnode" path="hello.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<rule name="my rool for toh">
<match url="rest/*" />
<action type="Rewrite" url="/toh-api/hello.js" />
</rule>
</rules>
</rewrite>

<iisnode 
loggingEnabled="true"
logDirectory="F:Logsmyappiisnode" 
promoteServerVars="AUTH_USER,AUTH_TYPE,HTTP_UID,LOGON_USER" />

</system.webServer>
</configuration>

当我从指向服务器URLhttp://myserver.mydomain/toh-api/rest/bar的客户端机器打开浏览器时,我会被问到网络凭据(正确,实际上我已经在IIS网站上设置了win-auth(,最终我看到

Hello MYDOMAINMYUSERNAME:MYDOMAINMYUSERNAME from bar! [express sample] Negotiate

到目前为止,一切都很好。

我只是对IIS URL REWRITE有点困惑。在这种情况下,我不得不将"rest"设置为match url,并且我需要将这个/rest/包含在我的URL的最后部分(我真的花了几个小时,做了很多尝试和错误,才像现在这样正确(。理想情况下,我更希望得到一个没有该部分的URL,比如http://myserver.mydomain/toh-api/bar(请注意,toh-api是IIS应用程序中的别名,所以它当然必须存在(。对node.js服务器文件(hello.js(的更改是微不足道的。

URL重写部分的更新是什么?<match url="*" />似乎不正确且不起作用。但我想这应该是可行的(对吧?(,据我所知,这或多或少是IIS子文件夹下Angular dist的url重写部分。。。

我想这或多或少是Angular的url重写部分dist在IIS的子文件夹下,据我所知。。。

是的,事实上我已经从IIS子文件夹下的Angular教程中复制了这个想法。

现在我有了(IIS别名/toh-api/部分可以从url跳过(

<rewrite>
<rules>
<rule name="my app rule" stopProcessing="true">
<match url=".*" />
<action type="Rewrite" url="hello.js" />
</rule>
</rules>
</rewrite>

只有星号*是RegEx中的模式错误(因此是500 Server Error(,你真的想要一个点星号.*-意味着任何字符的任何序列,包括0次出现-所以在url=".*"中需要一个(以避免500 Server Error(,它就可以了!问题已解决(hello.js有明显变化(。顺便说一句,stopProcessing="true"似乎没有必要。

另一个更简单的选项是带有规则名称的标记内的patternSyntax="Wildcard"。在这种情况下,只要星号就可以了。

##动词PUT和DELETE##然而,我注意到动词PUT和DELETE有一个不明显的错误:它们不是IIS URL重写的,它们会导致404未找到错误(也许值得一问(

(放入和删除动词,也已解决(

最新更新