AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1081546
Accepted
Jorge Mauricio
Jorge Mauricio
Asked: 2021-10-25 08:06:15 +0800 CST2021-10-25 08:06:15 +0800 CST 2021-10-25 08:06:15 +0800 CST

如何在 windows server 2016 / IIS 上部署服务器端渲染 (SSR) 反应应用程序(由 webpack 捆绑)

  • 772

提个醒:这是我第一次尝试在定制的 Windows 服务器上部署 react 应用程序,但我已经成功地在 Heroku 和 Linux (PM2) 上完成了它,所以我知道应用程序架构是假设的正常工作。

场景:

我已经构建了一个 Windows Server 2016 / 64 位托管服务器来托管多个网站。我已经使用 VPS Contabo 来做到这一点。我已经测试了所有应该工作的功能,即使是其他应用程序,如 ASP.NET、PHP、SSL 证书,一切都运行良好。

至于我试图在此服务器上托管的特定节点 js 项目,它由 2 个主要部分组成:

  • node 中的后端与 CMS 一起使用 node / javascript 开发。

我为此在子域上创建了一个托管空间,它运行良好,即使使用 Let's Encrypt SSL 证书也是如此。如果有人想访问它,请访问: https ://backendnode.fullstackwebdesigner.com/system

  • 反应服务器端渲染中的前端。

这就是问题发生的地方。如果有人想访问它,这里是链接: https ://fullstackwebdesigner.com/

我对它们都使用了基本相同的技术:

  • iis节点;
  • URL重写扩展;
  • iis节点模块;
  • web.config 文件配置;

问题:

我已经设法让它作为一个网站加载,就像我对后端所做的那样,但问题是它似乎没有加载 CSS 文件、图像等。所以布局不会加载。在控制台上,有错误消息: Uncaught SyntaxError: Unexpected token '<'

react 应用程序:正如我之前所说,它是作为服务器端渲染应用程序完成的,并与 webpack 捆绑在一起。因此,它将捆绑的文件构建到名为“/build”的目录中。在这个目录中,有一个“/public”目录,所有资产都在其中,例如 CSS 文件和图像。

在开发时,我会在终端上运行构建:node build/bundle.react.js

尽管看起来很奇怪,但当我在 Windows 服务器的终端上运行它时,它运行良好。但只能通过以下方式访问:http://localhost:3001 它会加载所有应该加载的内容。

这是文件结构的简化表示:

- /build/
--bundle.react.js
--/build/public/
---/files-layout/
---/fonts/
---bundle.react.client.js

这也是我在网站托管空间上用于反应构建的 web.config 文件:

<configuration>
    <system.webServer>
        <iisnode nodeProcessCommandLine="C:\Program Files\nodejs\node.exe" />
        
        <handlers>
            <add name="iisnode" path="/build/bundle.react.js" verb="*" modules="iisnode" />
        </handlers>

        <rewrite>
            <rules>
                <!-- Redirect to HTTPS (alternative). -->
                <rule name="Redirect to HTTPS / SSL" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    </conditions>
                    <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
                </rule>

                <!-- Don't interfere with requests for logs. -->
                <rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">
                    <match url="^[a-zA-Z0-9_\-]+\.js\.logs\/\d+\.txt$" />
                </rule> 
                <!-- Node. -->
                <rule name="sendToNode">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/build/bundle.react.js" />          
                </rule>
            </rules>    
        </rewrite> 
        <defaultDocument>
            <files>
                <clear />
                <add value="/build/bundle.react.js" />
            </files>
        </defaultDocument>
        <security>
            <requestFiltering>
                <hiddenSegments>
                    <add segment="node_modules" />
                </hiddenSegments>
            </requestFiltering>
        </security>
    </system.webServer>
    <system.web>
        <compilation defaultLanguage="js" />
    </system.web>
</configuration>

任何人都知道可能缺少什么或正在发生什么,因为在 web 上的 windows 服务器上托管服务器端渲染的参考文献不多?可能是 IIS、web.config 或网站托管空间上的额外配置?

编辑:

我刚刚做了一个有趣的测试:在我的本地开发计算机上,当我通过终端运行时:

node bundle.react.js

从 /build 文件夹中,我得到与在线发生的问题相同的结果(没有布局、样式、图像等)。

但是当我通过终端运行时:

node build/bundle.react.js

从 /build 文件夹外部(从基本目录),它可以完美加载。

关于更多的事情。基本目录的组织方式如下:

…(some folders)
- /build/
--bundle.react.js
--/build/public/
---/files-layout/
---/fonts/
---bundle.react.client.js
-node_modules
…(some root files, like webpack and so on)

我猜由于我如何编写 web.config 文件,引用 /node_modules 文件夹存在某种问题,但我不知道应该如何引用它。

deployment iis node.js windows-server-2016
  • 1 1 个回答
  • 923 Views

1 个回答

  • Voted
  1. Best Answer
    Jorge Mauricio
    2021-10-25T12:45:29+08:002021-10-25T12:45:29+08:00

    让它工作!以下链接帮助我进行了最后的调整,以及我所做的测试: https ://www.thecodehubs.com/how-to-deploy-ssr-angular-universal-to-iis/

    总结起来,我不得不将服务器包复制到根目录。并更改 web.config 文件以调用根文件。以下是 web.config 文件的最终结果,以及我在发布的链接中发现的一些有趣的配置(没有额外配置尚未测试):

    <configuration>
        <system.webServer>
            <iisnode nodeProcessCommandLine="C:\Program Files\nodejs\node.exe" />
            
            <handlers>
                <add name="iisnode" path="bundle.react.js" verb="*" modules="iisnode" />
            </handlers>
    
            <rewrite>
                <rules>
                    <!-- Redirect to HTTPS. -->
                    <rule name="Redirect to HTTPS / SSL" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                        </conditions>
                        <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
                    </rule>
    
                    <!-- Don't interfere with requests for logs. -->
                    <rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">
                        <match url="^[a-zA-Z0-9_\-]+\.js\.logs\/\d+\.txt$" />
                    </rule>
                    
                    <!-- Node. -->
                    <rule name="sendToNode">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="bundle.react.js" />
                    </rule>
                    
                    <rule name="StaticContent" stopProcessing="true">
                      <match url="([\S]+[.](jpg|jpeg|gif|css|png|js|ts|cscc|less|ico|html|map|svg))" />
                      <action type="None" />
                    </rule>
                </rules>
            </rewrite> 
            
            <staticContent>
                <clientCache cacheControlMode="UseMaxAge" />
                <remove fileExtension=".svg" />
                <remove fileExtension=".eot" />
                <remove fileExtension=".ttf" />
                <remove fileExtension=".woff" />
                <remove fileExtension=".woff2" />
                <remove fileExtension=".otf" />
                <mimeMap fileExtension=".ttf" mimeType="application/octet-stream" />
                <mimeMap fileExtension=".svg" mimeType="image/svg+xml"  />
                <mimeMap fileExtension=".eot" mimeType="application/vnd.ms-fontobject" />
                <mimeMap fileExtension=".woff" mimeType="application/x-woff" />
                <mimeMap fileExtension=".woff2" mimeType="application/x-woff" />
                <mimeMap fileExtension=".otf" mimeType="application/otf" />
            </staticContent>
            
            
            <defaultDocument>
                <files>
                    <clear />
                    <add value="bundle.react.js" />
                </files>
            </defaultDocument>
    
            <security>
              <requestFiltering>
                <hiddenSegments>
                  <add segment="node_modules" />
                  <!--add segment="iisnode" /-->
                </hiddenSegments>
              </requestFiltering>
            </security>
        </system.webServer>
        <system.web>
            <compilation defaultLanguage="js" />
        </system.web>
    </configuration>
    
    • 0

相关问题

  • 网络监控产品公司的部署工程师需要具备哪些技能?[关闭]

  • Mac OS X 的无人值守安装

  • Firefox 打包以包含用于网络安装的插件

  • 我应该将 Rails 应用程序部署到哪个目录?

  • 将 iChat 设置部署到多个用户

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve