jjzjj

iis - <serverVariables> <set name ="..."/> 中断 web.config

coder 2024-02-29 原文

问题底部有一个 TL;DR!

这里...

问题的背景(简单地说)是 IIS Rewrite 在重定向操作中删除了协议(protocol)。所以一个规则 <action type="Redirect" url="whatever"/>如果您没有在 url="..." 中明确指定协议(protocol),则从 https 转到 http ,像这样url="https://{HOST}" .

经过大量 Google 搜索后,我找到了 this article ,它描述了重定向时保持协议(protocol)的多种方式。最适合我的方法如下:

<rule name="Create HTTP_PROTOCOL">
    <match url=".*" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{CACHE_URL}" pattern="^(.+)://" />
    </conditions>
    <serverVariables>
        <set name="HTTP_PROTOCOL" value="{C:1}" />
    </serverVariables>
    <action type="None" />
</rule>

<rule name="Redirect to www" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^localtest\.me$" />
    </conditions>
    <action type="Redirect" url="{HTTP_PROTOCOL}://www.localtest.me/{R:1}" />
</rule>

...出现这种情况有两个原因:

  1. 有多个规则需要维护协议(protocol)。每个都写 2 个(一个仅用于 http,另一个用于 https)是不可行的。

  2. 此文件必须适用于直接 SSL 连接(其中 {HTTP}=on)并与 Cloudflare 的灵活 SSL 兼容(其中 {HTTP}=off 但 {HTTP_X-Forwarded-Proto} 是“http”或“https”)。

所以,

我的想法是设置一个我可以用来对付 rewriteMap 的变量。该变量的值取决于 HTTPS=on或者 HTTP_X-Forwarded-Proto=https

这是我的原始代码,它删除了协议(protocol):

<!-- remove trailing slashes looses protocol -->
<rule name="RemoveTrailingSlashRule" stopProcessing="true">
    <match url="(.*)/+$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    </conditions>
    <action type="Redirect" url="{R:1}" />
</rule>

这是我修改后的代码,无论是直接 SSL 连接还是通过 CloudFlare 的灵活 SSL 连接,它都会使用所需的协议(protocol)进行重定向:

<!-- rewrite config -->
<rewrite>

  <!-- rewrite maps -->
  <rewriteMaps>
      <rewriteMap name="RedirectBase">
          <add key="http" value="http://{HTTP_HOST}/" />
          <add key="https" value="https://{HTTP_HOST}/" />
      </rewriteMap>
  </rewriteMaps>

  <!-- rewrite rules -->
  <rules>

    <!-- capture incoming protocol -->
    <rule name="HTTP_PROTOCOL - Capture Default">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{CACHE_URL}" pattern="^(.+)://" />
      </conditions>
      <serverVariables>
        <set name="HTTP_PROTOCOL" value="{C:1}" />
      </serverVariables>
      <action type="None" />
    </rule>

    <!-- overwrite protocol var if using CloudFlare's flexible SSL -->
    <rule name="HTTP_PROTOCOL - Overwrite with CloudFlare header">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_X-Forwarded-Proto}" pattern="^https$" />
      </conditions>
      <serverVariables>
        <set name="HTTP_PROTOCOL" value="{C:1}" replace="true" />
      </serverVariables>
      <action type="None" />
    </rule>

    <!-- remove trailing slashes but keep protocol -->
    <rule name="RemoveTrailingSlashRule" stopProcessing="true">
        <match url="(.*)/+$" />
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        </conditions>
        <action type="Redirect" url="{RedirectBase:{HTTP_PROTOCOL}}{R:1}" />
    </rule>

  </rules>
</rewrite>

除了,

A) 我得到一个空白页面,url 重写失败,没有错误信息。

B) 但如果我删除此标记的两个实例,它会起作用:

TL;DR 为什么这行不通?

  <serverVariables>
    <set name="HTTP_PROTOCOL" value="something" />
  </serverVariables>

最佳答案

找到答案了!

我必须在 URL 重写配置中手动将“HTTP_PROTOCOL”添加到允许的服务器变量列表。这似乎只能通过 IIS UI 而不是通过 web.config 文件来完成。 <强> Full instructions here .

关于iis - <serverVariables> <set name ="..."/> 中断 web.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30665339/

有关iis - <serverVariables> <set name ="..."/> 中断 web.config的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  4. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  5. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  6. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  7. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  8. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  9. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  10. ruby-on-rails - Nokogiri:使用 XPath 搜索 <div> - 2

    我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll

随机推荐