How do I prevent azure cdn from serving dynamic content? I only want it to serve static files: images, scripts and css but when I choose my web app as origin it serves also the root url (/), that is a dynamic page. Can I create restrictions based on file extension?
Not at this time. Any request to the CDN edge node is going to hit the origin for the asset and cache it, whether it's dynamic or static content. The solution, then, is to ensure that your application doesn't link to any dynamic content via the CDN.
If your static content is located in specific folders (images/, css/, etc), you can prevent the CDN from reaching content outside of those folders.
In order to prevent search engines from indexing our site via the CDN domain we
have disabled site browsing on the CDN domain (except for a specific set of folders that serve resources) by adding the following urlRewrite rule to web.config:
<rule name="Deny site browsing on CDN domain" stopProcessing="true">
<match url="^(storage/|ui/)(.*)" negate="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_X_HOST}" pattern="\.azureedge.net$" />
</conditions>
<action type="CustomResponse" statusCode="404" statusReason="Not found" statusDescription="Not found" />
</rule>
Using web server as CDN origin is a "lazy" way to setup CDN. After you set it up, in your website instead of using:
/mypic.jpg
replace all the relative url with
xxx.azureedge.net/mypic.jpg
You don't really need to care about CDN is serving the dynamic content or not, you just need to pick the content you want. CDN just blindly serve the content from origin you specified.
Related
I am trying to figure out what I would need to do for a rewrite rule to apply to tags if possible to append to the source to basically proxy the content of HTTP links.
Looks like google does it for its webmail and I need something similar for my self hosted webmail. I could ingore the Not Fully Secure warning but I want to try to finish it.
Unfortunately I dont remember how I did it in the past and I deleted the old folder to look at the web.config file that would have been in there.
This way I could look for image links with an image of http://example.com/image.png and make it https://example.org/proxy.php?url=http://example.com/image.png and then it would load the same image but now be secure. I have the php setup as needed to do this but now I need to figure out this outbound rewrite.
You can use rules like this.
<rule name="rule" preCondition="ISHTML">
<match filterByTags="A, Img" pattern="(.*)" />
<action type="Rewrite" value="https://example.org/proxy.php?url=http://{HTTP_HOST}{R:1}" />
</rule>
According to the law of image name and php page name, you can customize regular expressions to match the image you want to rewrite and the rewritten url
A Temporary down page (e.g. updating your servers SW) should ideally have a response code of 503, but you could get away with 307, but in no case should it be 200 (as google will index this and it will affect your SEO)
In IIS rewrite rules, you have 3 options for implementing a redirect to a down page, rewrite, redirect and customResponse:
<action type="Rewrite" url="/site1.html" />
<action type="Redirect" url="/site1.html" redirectType="Temporary" />
<action type="CustomResponse" statusCode="503" subStatusCode="0" statusReason="Site Unavailable" statusDescription="Down for maintenance" />
The problem is if you want a 503 response, you cant redirect to the required page.
We have 3 websites for different brands using episerver CMS.
When we do maintenance, or just want to take a site down, we have a single azure web app (aka iis) which has 3 holding pages, one for each.
so our site down website has 3 pages:
/site1.html
/site2.html
/site3.html
We use Azure traffic manager to point to the live site or the site down page, and we currently have redirects which work, but incorrectly give 200 response:
<rewrite>
<rules>
<rule name="site1" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="mysite" />
</conditions>
<action type="Rewrite" url="/site1.html" />
</rule>
<rule name="site2" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="myothersite" />
</conditions>
<action type="Rewrite" url="/site2.html" />
</rule>
etc.
</rules>
</rewrite>
In order to fix this issue (offer a different site down page per site, and respond with 503), what are the options?
I would guess its possible to setup something like 3 different named virtual hosts, each with nothing except a custom 503 error page + a catch all CustomResponse action? Any examples of such a config?
To be clear, our app may well be running normally, but we may want to use our traffic manager to point the public at a "down" page which has a 503 respose during maintenance. The website sorving the down page has nothing to do with the website serving the site/applicaion itself.
Sadly, as the Microsoft document describes – there is no way to customize the 503 HTTP error.
Even use rewrite to make it display custom 503 page, In fact the request get into IIS and rewrite successful, then response to client. The whole process is perfect and your web service doesn’t stop.
The error is detected by the IIS server as it attempts to hand the incoming request to application. Everything application does is performed in its app pool. Modules like rewrite and custom error page are all executed in this way. 503 handled by the http.sys you cannot create a custom error page at all, as it is processed before it gets to iis. Therefore, both hope that the web server will stop reporting 503, but also hope that the server can process the request to display the page you defined. These two conflicts.
If your application is asp.net, there’s another way to custom 503. You can place a text file named "app_offline.htm" in the root of the site, all requests to that website will redirect to that app_offline.htm file. Basically, if you need to take an entire ASP.NET site offline, you can place some nice message in that file. Then, any new requests to a URL, any URL, in that website will redirect to that file allowing you to do maintenance to the site, upgrades, or whatever. It is not really a redirect though. ASP.NET essentially shuts down the site, unloads it from the server, and stops processing any requests to that site. That is, until you delete the app_offline.htm file - then things will continue as normal and your ASP.NET site will load up and start serving requests again.
We are migrating our old site to a new one (wordpress hosted on Azure) but for some reasons we need to maintain the old one one online. Suppose the site are:
new site www.site.com
old site www.oldsite.com
This is what we need:
if a user enter www.site.com/somepath and this doesn't exist, it must be redirected to www.oldsite.com/somepath.
Is it possible by setting url rewrites in web.config or by redirecting 404 error to the old domain?
Thank you in advance,
Marco
From your information, I'd assume you are using WordPress template (not WordPress on Linux) from Azure Marketplace (in which during the WordPress web provisioning, you are asked to chose whether Azure MySQL or MySQL in App option). You can download IIS Manager extension and remotely connect to your site then start writing your rule. You can also download the web.config file (via FTP and make it up yourself). Here is the reference of web.config https://social.technet.microsoft.com/wiki/contents/articles/32229.azure-create-an-url-rewrite-azure-web-app.aspx
The below is sample rule
<rewrite>
<outboundRules>
<rule name="404redirection">
<match filterByTags="None" serverVariable="{RESPONSE_STATUS}" pattern="^404" />
<conditions>
<add input="{HTTP_HOST}" type="Pattern" pattern="^http://newwebsite.net/somepath(.*)">
</conditions>
<action type="Redirect" value="http://oldwebsite.net/somepath(.*)" />
</rule>
</outboundRules>
</rewrite>
Note that this is the sample rule based on the web server variable with HTTP response (404). If you WordPress uses some custom query strings or so on, the rule is supposed to be more complicated than this one.
I have React app which ends up being built with webpack. We host an API which this app talks to on Azure and would like to host the UI built on top of it out there as well.
When hitting the URL the first time, and navigating around, all of the history location stuff works. However, when we refresh the page, we receive a 404 error (which is to be expected if IIS is serving this).
Is it possible to configure Azure to handle this sort of thing? Should we just give up on this type of application and host a webpack vm?
Similar questions:
Reactjs HistoryLocation get 404 on refresh
React-router urls don't work when refreshing or writting manually
If do understand your question and situation correctly, the solution would be to redirect all requests to your only one HTML page (assuming index.html).
This can be achieved with the URL rewrite module of IIS.
By default any Azure Web App will have a file in its web root folder named web.config. This is standard XML file. Open it with favorite editor and locate <system.webserver> section. Then place the following additional sections inside. Note, the <rewrite> element should be direct descendant of the <system.webserver> one:
<rewrite>
<rules>
<rule name="Rewrite to index.html">
<match url=".*" />
<action type="Rewrite" url="index.html" />
</rule>
</rules>
</rewrite>
Again, based on the assumption that you serve everything from index.html, you can change this file in the rewrite rule to represent the file you are serving content from.
I have an Azure CDN with a website origin for my domain and it picked www.mydomain.co.uk automatically (with no option for me to change it). I'd rather not use a Storage origin as I have automatic deployment from Github. So, azxxxxxxx.vo.msecnd.net will retrieve content from www.mydomain.co.uk and cache it locally.
However, I have a URL write rule which will redirect www.mydomain.co.uk to mydomain.com (for various SEO reasons). This will cause requests for CDN content to return a 301 redirect, rather than serving up content to the CDN for it to cache. Is there some way to stop the URL rewrite when content is requested by the Azure CDN?
You're more than welcome to see the code and URL rewrite rules at https://github.com/brentnewbury/PersonalSite/blob/master/web.config
For others who might want to know how I solved the issue, I decided to write a URL rewrite rule (see below) that looks for /cdn/ and route that through and no longer process other rules. This seems to work nicely. It means references in my HTML will look like //azxxxxxxx.vo.msecnd.net/cdn/img/pic.jpg for a image that is stored in /img/pic.jpg.
<rule name="CDN Passthrough" stopProcessing="true">
<match url="(.*)(cdn/)([\S]+)" />
<action type="Rewrite" url="{R:1}/{R:3}"/>
</rule>