【问题标题】:Deploying Angular 8 Project after brotli compressionbrotli 压缩后部署 Angular 8 项目
【发布时间】:2021-02-10 17:16:10
【问题描述】:

我在 Angular 8 中有一个项目。 我已经参考https://medium.com/@subodhkumarjc/angular-build-optimization-part-2-compression-b866dd0593c3 在我的项目中实现 bortli 压缩,因为我的 main.js 文件在正常构建后变得相当大。 运行ng build --prod 后。我得到以下一组 dist 文件。 File explorer view

现在,通常为了在 IIS 中部署它,我只是从我当前的本地 dist 文件夹进行简单的复制粘贴到服务器中 IIS 指向的我的 web 文件夹。但是当我尝试做同样的事情时,我的浏览器仍在加载 main.js 文件而不是压缩的 main.js.br 文件。

谁能帮我解决我在部署时缺少的东西。

注意:到目前为止,我还没有对我的 IIS 进行任何更改

【问题讨论】:

    标签: angular iis gzip brotli


    【解决方案1】:

    尝试在您的 web.conifg 文件中添加以下代码:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <httpProtocol>
                <customHeaders>
                    <remove name="X-Powered-By" />
                </customHeaders>
            </httpProtocol>
            <rewrite>
                <rules>
                    <clear />
                    
                    <rule name="br_rewrite" enabled="true" stopProcessing="true">
                        <match url="(.*).(js$|svg|css)" />
                        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                            <add input="{HTTP_ACCEPT_ENCODING}" pattern="br" />
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" />
                        </conditions>
                        <action type="Rewrite" url="{R:1}.{R:2}.br" logRewrittenUrl="true" />
                    </rule>
                    <rule name="gzip_rewrite" enabled="true" stopProcessing="true">
                        <match url="(.*).(js$|svg|css)" />
                        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                            <add input="{HTTP_ACCEPT_ENCODING}" pattern="gzip" />
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" />
                        </conditions>
                        <action type="Rewrite" url="{R:1}.{R:2}.gz" logRewrittenUrl="true" />
                    </rule>
                </rules>
                <outboundRules rewriteBeforeCache="true">
                    <rule name="Remove Server header" enabled="true">
                        <match serverVariable="RESPONSE_Server" pattern=".+" />
                        <action type="Rewrite" value="" />
                    </rule>
                    <rule name="Rewrite content-encoding header gzip" preCondition="IsGZ" enabled="true" stopProcessing="false">
                        <match serverVariable="RESPONSE_CONTENT_ENCODING" pattern=".*" />
                        <action type="Rewrite" value="gzip" />
                    </rule>
                    <rule name="Rewrite content-encoding header br" preCondition="IsBR" enabled="true" stopProcessing="false">
                        <match serverVariable="RESPONSE_CONTENT_ENCODING" pattern=".*" />
                        <action type="Rewrite" value="br" />
                    </rule>
                    <rule name="css content type" preCondition="IsCSS" enabled="true" stopProcessing="false">
                        <match serverVariable="RESPONSE_CONTENT_TYPE" pattern="(.*)" />
                        <action type="Rewrite" value="text/css" />
                    </rule>
                    <rule name="js content type" preCondition="IsJS" enabled="true" stopProcessing="false">
                        <match serverVariable="RESPONSE_CONTENT_TYPE" pattern="(.*)" />
                        <action type="Rewrite" value="application/javascript" />
                    </rule>
                    <rule name="svg content type" preCondition="IsSVG" enabled="true" stopProcessing="false">
                        <match serverVariable="RESPONSE_CONTENT_TYPE" pattern="(.*)" />
                        <action type="Rewrite" value="image/svg+xml" />
                    </rule>
                    <preConditions>
                        <preCondition name="IsGZ">
                            <add input="{URL}" pattern="\.gz$" />
                        </preCondition>
                        <preCondition name="IsBR">
                            <add input="{URL}" pattern="\.br$" />
                        </preCondition>
                        <preCondition name="IsCSS">
                            <add input="{URL}" pattern="css" />
                        </preCondition>
                        <preCondition name="IsJS">
                            <add input="{URL}" pattern="js" />
                        </preCondition>
                        <preCondition name="IsSVG">
                            <add input="{URL}" pattern="svg" />
                        </preCondition>
                    </preConditions>
                </outboundRules>
            </rewrite>
            <urlCompression doStaticCompression="true" doDynamicCompression="false" />
            <httpCompression sendCacheHeaders="false" />
            <staticContent>
                <mimeMap fileExtension=".br" mimeType="application/brotli" />
                <clientCache cacheControlMode="UseMaxAge" />
            </staticContent>
        </system.webServer>
    </configuration>
    

    【讨论】:

    • web.config 是我的 IIS 的 application.host.config 文件?
    • @ShivamThakur 您需要将此代码添加到您站点文件夹下的 web.conifg 文件中。
    • @ShivamThakur 你最终能否让它在 IIS 上运行?我有完全相同的问题,找不到出路。
    • @ShivamThakur 在挣扎了 2 周后,我终于让它工作了。我的 nod32 防病毒软件禁用了 gzip 或 brotli 压缩。暂停它是不够的,我必须去 nod32 -> 高级设置 -> Web 和电子邮件 -> 关闭“启用 SSL/TLS 协议过滤”。
    猜你喜欢
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2021-12-16
    相关资源
    最近更新 更多