如何强制 Azure 网站中 node.js 上的 express 使用 https? [英] How do you force express on node.js in Azure Websites to use https?
问题描述
在 Windows Azure 网站上运行,我想通过默认的 *.azurewebsites.net 证书使用 ssl.它无需执行任何操作即可工作,但 http 也可用于每个目的地,而不仅仅是 https.如何强制从 http 重定向到 https?通常我可以这样做:
Running on Windows Azure Websites, I want to use ssl via the default *.azurewebsites.net certificate. It works without doing anything, but http is also available for every destination, not just https. How do I force a redirect from http to https? Normally I could just do something like:
var https = require('https');
...
var options = {
key: fs.readFileSync('path.key'),
cert: fs.readFileSync('path.crt')
};
...
https.createServer(options, app)
但由于我对 *.azurewebsites.net 证书一无所知,例如它的路径,所以这行不通.
but since I don't know anything about the *.azurewebsites.net certificate, such as its path, that's not going to work.
如何将所有或部分请求重定向到 https?
How do I redirect all or some requests to https?
推荐答案
在 web.config
中,在任何其他具有 stopProcessing="true" 的规则之前添加以下规则
.
In web.config
, add the following rule before any other rule that has stopProcessing="true"
.
<rule name="RedirecttoHTTPS">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{URL}" pattern="/$" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="SeeOther" />
</rule>
如果您想要 *.azurewebsite.net 通配符证书,也可以只使用普通的 http.createServer(app)
进行生产.
You can also just use the normal http.createServer(app)
for production if you want to the *.azurewebsite.net wildcard certificate.
参考资料:
这篇关于如何强制 Azure 网站中 node.js 上的 express 使用 https?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!