为什么匹配规则重写进入带有 iisnode 模块的 URL [英] Why is matching rule rewriting going into the URL with iisnode module

查看:37
本文介绍了为什么匹配规则重写进入带有 iisnode 模块的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Windows 2016 和 IIS 10(这里的版本不是很相关,只是说一下),我正在使用模块 iisnode 将 Windows 身份验证导入 Node.js.一切正常,但我已经考虑了几个小时的配置,我不知道如何增加一点改进.

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

var express = require('express');var app = express.createServer();//顺便说一句,为什么这里不推荐使用这种语法?//也许只是因为我从设置中提供的旧示例中复制了文件夹!?app.get('/toh-api/rest/foo', function (req, res) {res.send('Hello from foo! [快递样本]');});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('用户名',用户名);console.log('authenticationType',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

<预><代码><配置><system.webServer><处理程序><add name="iisnode";路径=hello.js"动词=*"模块=iisnode"/></处理程序><重写><规则><规则名称=我的角色"><match url="rest/*";/><动作类型=重写"url="/toh-api/hello.js";/></规则></规则></rewrite><iis节点loggingEnabled=真"logDirectory=F:\Logs\myapp\iisnode"PromotionServerVars="AUTH_USER,AUTH_TYPE,HTTP_UID,LOGON_USER";/></system.webServer></配置>

当我从指向服务器 URL http://myserver.mydomain/toh-api/rest/bar 的客户端计算机打开浏览器时,我被要求提供网络凭据(正确,确实我已经在 IIS 站点上设置了 win 身份验证),最终我看到了

你好 MYDOMAIN\MYUSERNAME:MYDOMAIN\MYUSERNAME 来自吧!【快递样品】洽谈

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

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

URL REWRITE 部分的更新是什么? 似乎不正确且不起作用.但我想它应该是可行的(对吧?),我想它或多或少是 IIS 子文件夹下的 Angular dist 的 url 重写部分,据我所知......

解决方案

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

是的,事实上我已经从 IIS子文件夹下的Angular教程.

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

<规则><规则名称=我的应用程序规则"stopProcessing=真"><match url=".*";/><动作类型=重写"url="hello.js";/></规则></规则></rewrite>

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

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

<打击>##动词PUT和DELETE##

但是,我注意到动词 PUT 和 DELETE 的一个隐蔽错误:它们不是重写的 IIS URL,它们会导致 404 未找到错误(也许值得一个后续问题).(放置和删除动词,也已解决)

I'm on windows 2016 and IIS 10 (versions are not so relevant here, but just to say) and I'm using the module iisnode to get the windows authentication into Node.js. Everything is working fine but I've wrapped my head around the configuration for hours and I'm not sure how to add a little improvement.

The current situation is that I have a script 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] ');
});

and a 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:\Logs\myapp\iisnode" 
      promoteServerVars="AUTH_USER,AUTH_TYPE,HTTP_UID,LOGON_USER" />
    
  </system.webServer>
</configuration>

When I open a browser from a client machine pointing to the server URL http://myserver.mydomain/toh-api/rest/bar, I'm asked the network credential (correct, indeed I've set the win auth on the IIS site) and eventually I see

Hello MYDOMAIN\MYUSERNAME:MYDOMAIN\MYUSERNAME from bar! [express sample] Negotiate

So far, so good.

I'm only a little confused by the IIS URL REWRITE. In this case I've had to put "rest" as match url, and I need to include this /rest/ in the final part of my URL (I've really spent hours and made many trials and errors before having it right, like it is now). Ideally, what I would prefer is getting a URL without that part, so let's say http://myserver.mydomain/toh-api/bar (notice that toh-api is the alias in IIS application, so it must be there, of course). The changes to node.js server file (hello.js) are trivial.

What is instead the update for the URL REWRITE part? <match url="*" /> seems not correct and not working. But I guess it should be feasible (right?), I imagine it is more or less the url rewriting part for an Angular dist under a subfolder of IIS, as far as i can understand...

解决方案

I imagine it is more or less the url rewriting part for an Angular dist under a subfolder of IIS, as far as i can understand...

Yes, in fact I've copied the idea from a tutorial of Angular under IIS subfolder.

Now I have (the IIS alias /toh-api/ part can be skipped from the url)

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

Only an asterisk * is a pattern error in RegEx (hence the 500 Server Error), you really want a dot asterisk .* - meaning any sequence, including 0 occurrences, of any chars - so a dot is needed in url=".*" (to avoid a 500 Server Error) and it does the trick! Problem solved (with the obvious changes in hello.js). Btw, the stopProcessing="true" seems not necessary instead.

Another simpler option is patternSyntax="Wildcard" inside the tag with the rule name. In that case just the asterisk is perfectly fine.

## Verbs PUT and DELETE ##

However, I've notice an obscure bug for verbs PUT and DELETE: they are not IIS URL rewritten and they cause a 404 not found error (maybe it's worth a follow up question). (put and delete verbs, also solved)

这篇关于为什么匹配规则重写进入带有 iisnode 模块的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆