简体   繁体   English

在 azure 上的 asp.net vnext 网站上禁用端口 80

[英]Disabling port 80 on an asp.net vnext website on azure

I have a feeling this should be easy, but I'm struggling to find out how to do it.我觉得这应该很容易,但我正在努力找出如何去做。

I have a website that I want to restrict to HTTPS.我有一个我想限制为 HTTPS 的网站。 It is an asp.net vnext website (not mvc) that is deployed to Azure.它是一个部署到 Azure 的 asp.net vnext 网站(不是 mvc)。 It is serving up static files without going through the ASP.NET pipeline.它在不通过 ASP.NET 管道的情况下提供静态文件。

In previous versions of asp.net, you could add system.webServer rules to do a redirect.在以前版本的 asp.net 中,您可以添加 system.webServer 规则来进行重定向。 This is gone from vNext.这已经从 vNext 消失了。 If I was using Mvc I could use the RequireHttps attribute or I could write custom middleware to do the redirect, but this only kicks in when the asp.net pipeline is activated.如果我使用 Mvc,我可以使用 RequireHttps 属性,或者我可以编写自定义中间件来执行重定向,但这仅在 asp.net 管道被激活时才会启动。 My html and js (it's a SPA app) would still be served up.我的 html 和 js(它是一个 SPA 应用程序)仍会提供。 If I was deploying to IIS instead of Azure, I could configure it there.如果我部署到 IIS 而不是 Azure,我可以在那里配置它。

So, how do I tell an azure website to only respond on port 443 without a web config file?那么,如何告诉 azure 网站只在没有 Web 配置文件的情况下在端口 443 上响应?

According to Azure Documentation you can add a web configuration file for applications written in any programming language supported by Azure (Node.js, PHP, Python Django, Java).根据 Azure 文档,您可以为使用 Azure 支持的任何编程语言(Node.js、PHP、Python Django、Java)编写的应用程序添加 Web 配置文件。 You can find detailed information here.您可以在此处找到详细信息

Here is a sample:这是一个示例:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Force HTTPS" enabled="true">
         <match url="(.*)" ignoreCase="false" />
         <conditions>
          <add input="{HTTPS}" pattern="off" />
         </conditions>
         <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
        </rule>
      </rules>
     </rewrite>
  </system.webServer>
</configuration>

You can achieve the redirect by adding the web.config file to your deployment.您可以通过将web.config文件添加到您的部署来实现重定向。 The documentation says:文档说:

when hosted on Azure App Service- Azure creates the file automatically during deployment, so you never see it.当托管在 Azure 应用服务上时 - Azure 在部署期间会自动创建文件,因此您永远不会看到它。 If you include one as part of your application, it will override the one that Azure automatically generates.如果将其中一个作为应用程序的一部分,它将覆盖 Azure 自动生成的那个。

for additional level of security probably you can use nsg.为了提高安全级别,您可能可以使用 nsg。 Azure documentation says NSG as "you can use Azure network security group to filter network traffic to and from Azure resources in an Azure virtual network". Azure 文档称 NSG 为“您可以使用 Azure 网络安全组来过滤进出 Azure 虚拟网络中的 Azure 资源的网络流量”。 You can try using cli to create a nsg and put rule in there to block traffic to port 80.您可以尝试使用 cli 创建一个 nsg 并在其中放置规则以阻止到端口 80 的流量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM