【问题标题】:Not working - deploying node application on IIS (iisnode) - Getting back 403 error不工作 - 在 IIS (iisnode) 上部署节点应用程序 - 返回 403 错误
【发布时间】:2018-10-03 17:09:16
【问题描述】:

我有一个 node/express API 项目。我正在尝试通过 Internet 信息服务 (IIS) 运行该项目。我在 IIS 中的当前结构 - 站点 -> 环境(网站) -> project1 | project2(此处为节点应用程序)。我将 project2 转换为具有环境应用程序池名称的应用程序。然后我创建应用程序。

我尝试点击我的网址,但我收到了 403 禁止错误。我已经给了它所有的凭据。不确定为什么会这样。

我一直在使用的一些资源 - video blog

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>

    <!-- 
      By default IIS will block requests going to the bin directory for security reasons. 
      We need to disable this since that's where Express has put the application entry point. 
    -->
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin" />
        </hiddenSegments>
      </requestFiltering>
    </security>

    <handlers>
      <!-- Indicates that the www file is a node.js entry point -->
      <add name="iisnode" path="app.js" verb="*" modules="iisnode"/>
    </handlers>
    <!--<rewrite>
      <rules>
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="app.js\/debug[\/]?" />
        </rule>


        <rule name="StaticContent">
          <action type="Rewrite" url="public{REQUEST_URI}"/>
        </rule>


        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
          </conditions>
          <action type="Rewrite" url="app.js"/>
        </rule>
      </rules>
    </rewrite> -->

    <rewrite>
      <rules>
        <rule name="sendToNode">
        <match url="/*" />
        <action type="Rewrite" url="app.js" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

app.js

const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
const cors = require('cors')
const compression = require('compression')
const helmet = require('helmet')
const expressSanitizer = require('express-sanitizer')
const jwt = require('jwt-simple');
const index = require('./routes/index')
const something1 = require('./routes/something1')
const something2 = require('./routes/something2')
const responseTime = require('response-time')

const app = express()
// const app = express.createServer()
const port = 3000

var corsOptions = {
    origin: 'http://localhost:8100',
    optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204 
}



//added security
app.use(helmet())

// //set logger
// app.use(logger)

//cors options
app.use(cors(corsOptions))

//body parser middleware
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))

// Mount express-sanitizer here
app.use(expressSanitizer()) // this line follows bodyParser() instantiations

//set static path
app.use(express.static(path.join(__dirname, 'client')))
// app.use(express.static(path.join(__dirname, '../../www')))

//Use response time
app.use(responseTime())

// set our default template engine to "ejs"
// which prevents the need for using file extensions
app.set('view engine', 'ejs')

//gzip compression
app.use(compression())

//add authorization request header
app.use((req, res, next) => {
    if(!req.headers.authorization){
        return res.status(403).json({ error: 'No credentials sent!'});
    }
    // try {
    //     let token = req.headers.authorization.split(' ')[1]
    //     var decoded = jwt.decode(token, 'your secret here');
    //     console.log(decoded, 'decoded')
    //   } catch (err) {
    //     console.log('err ', err.stack);
    //     return res.status(403).json({
    //       error: 'invalid token'
    //     });
    //   }
    next();
})

//set views for error and 404 pages
app.set('views', path.join(__dirname, 'views'))

app.use('/', index)
app.use('/something1/v1', something1)
app.use('/something2/v1', something2)

// app.listen(port, () => {
//     console.log('server started on port 3000')
// })

//run export NODE_ENV=production
//NODE_ENV=production node app.js

app.listen(process.env.port)

我已经安装了所有东西 - (rewrite/node/iisnode) 只是不知道如何设置它。谢谢

【问题讨论】:

  • 您是否尝试过设置简单的 hello world 程序而不是完整的应用程序...使用更少的 web.config...一旦它工作您可以继续使用它,那么您将了解确切的 root阻止您运行应用程序的原因...
  • 是的,我能够使用网站作为源而不是该网站中的应用程序来使其工作。这就是问题所在。

标签: node.js express iis deployment web-deployment


【解决方案1】:

嗯,我认为您在 IIS 中设置网站的方式不太正确。

听起来你的 IIS 结构是这样的:

Sites
└─── environment
     └─── project1
     └─── project2
          |    app.js
          |    web.config
          |    ...

其中environment是一个网站,它下面的每个文件夹可能是一个转换为application的文件夹。

这似乎是人们常犯的错误,因为 hello world 教程作为应用程序在 Default Web Site 下运行(虽然我不确定为什么会这样,但我的猜测是因为它不能取代默认网站,可通过 localhost 上的端口 80 轻松访问)。这不是设置新 IISNode 网站的最佳方式。

您应该做的是添加一个新站点(右键单击Sites 并从上下文菜单中选择Add Website)。填写必要的详细信息并将Physical path: 指向您的app.jsweb.config 所在的位置。

如果您设置了 web.config 正确,IISNode 应该会处理其余部分。

祝你好运,如果我做了任何错误的假设,请告诉我。

【讨论】:

  • 你是 100% 正确的。有没有办法使用相同的 IIS 结构?这个包有帮助吗? npmjs.com/package/express-subdomain
  • 嗯,这不是我有过的经验。您希望能够通过子域访问您的网站吗?仅使用我上面描述的方法就可以做到这一点……但是也许您应该看一下使用 IIS 的反向代理?然后,您可以将应用程序作为普通 Node 进程运行,但让 IIS 管理与远程连接的连接:serverfault.com/q/47537 虽然我不确定这是否能解决您的问题。
【解决方案2】:

我可以通过添加 iisnode 标签来解决类似的问题。

Web.config 如下所示:

<configuration>
  <system.webServer>

    <handlers>
      <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
    </handlers>

    <rewrite>
      <rules>
        <rule name="sendToNode">
          <match url="/*" />
          <action type="Rewrite" url="app.js" />
        </rule>
      </rules>
    </rewrite>
<iisnode
      watchedFiles="*.js;node_modules\*;views\*.ejs" 
nodeProcessCommandLine="\program files\nodejs\node.exe" />

  </system.webServer>
</configuration>

我按照这些教程来托管我的 node.js 应用程序:

Harvey Williams TutorialScott Hanselman Tutorial

【讨论】:

    猜你喜欢
    • 2018-02-16
    • 2020-08-02
    • 2021-12-21
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多