伊人博客 一款有技术又有生活的博客

记录博客网址从ECS搬迁到阿里云共享虚拟主机的过程

发布时间:2017-04-03 18:57:30 标签:ECS,云虚拟机,伪静态 | 阅读(1459)

记录博客系统从阿里云ECS搬迁到阿里云共享虚机的过程,云共享虚机是本科阶段,阿里云免费赠送的虚机,后来自己又续费了两年,之前乱七八糟的什么东西都放,以前也尝试过各种免费的主机空间,很多很杂,从一开始使用SAE、BAE、GAE等云引擎到主机屋免费主机空间。后来购买了阿里云ECS学生机,9.9元/月,然后我就一次性购买到了毕业,后来又续费了三年,用着很爽。我用这个服务器做过很多事情,最简单的就是挂百度浏览器刷百度在线积分(好low),中间给一些没有服务器的老师或者好友做页面时用过,后来做APP时也做过后台,后来放了一个博客系统到上边。

说起博客,总有一堆话要说,每个程序员都有一个博客梦,可惜我一直都把它当梦,一直没去实现。为了更好的被搜索引擎收录,网站URL希望能够伪静态,然后在配置文件web.config上添加了

<?xml version="1.0" encoding="UTF-8"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <section name="entityFramework" 
    type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, 
    EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
    requirePermission="false" />
    <!-- 我的ECS伪静态配置要求 -->
    <section name="RewriterConfig" 
    type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
    <!-- 我的ECS伪静态配置要求 -->
  </configSections>
  <connectionStrings>
    <add name="数据库连接" connectionString="" />
  </connectionStrings>
  <!-- 我的ECS伪静态配置要求 -->
  <RewriterConfig>
    <Rules>
      <RewriterRule>
        <LookFor>~/Default.shtml</LookFor>
        <SendTo>~/Default.aspx</SendTo>
      </RewriterRule>
 
      <RewriterRule>
        <LookFor>~/article/([0-9]*).shtml</LookFor>
        <SendTo>~/page/Content.aspx?id=$1</SendTo>
      </RewriterRule>
 
      <RewriterRule>
        <LookFor>~/list/([tksm])/([0-9]*)/(.*).shtml</LookFor>
        <SendTo>~/page/List.aspx?$1=$3&amp;p=$2</SendTo>
      </RewriterRule>
      <RewriterRule>
        <LookFor>~/list/([tksm])/(.*).shtml</LookFor>
        <SendTo>~/page/List.aspx?$1=$2</SendTo>
      </RewriterRule>
 
 
      <RewriterRule>
        <LookFor>~/Mobile.shtml</LookFor>
        <SendTo>~/Mobile.aspx</SendTo>
      </RewriterRule>
      <RewriterRule>
        <LookFor>~/marticle/([0-9]*).shtml</LookFor>
        <SendTo>~/page/MContent.aspx?id=$1</SendTo>
      </RewriterRule>
      <RewriterRule>
        <LookFor>~/mlist/([tksm])/([0-9]*)/(.*).shtml</LookFor>
        <SendTo>~/page/MList.aspx?$1=$3&amp;p=$2</SendTo>
      </RewriterRule>
      <RewriterRule>
        <LookFor>~/mlist/([tksm])/(.*).shtml</LookFor>
        <SendTo>~/page/MList.aspx?$1=$2</SendTo>
      </RewriterRule>
 
    </Rules>
  </RewriterConfig>
  <!-- 我的ECS伪静态配置要求 -->
  <system.web>
    <compilation targetFramework="4.0" />
    <httpRuntime requestValidationMode="2.0" />
    <pages validateRequest="false" />
 
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="6000" defaultUrl="~/" />
    </authentication>
    <customErrors>
      <error redirect="/Page/404.aspx" statusCode="404" />
    </customErrors>
 
  </system.web>
  <system.webServer>
 
    <!-- 我的ECS伪静态配置要求 -->
    <handlers>
      <add name="htmlHandler" verb="*" path="*.shtml" 
      type="URLRewriter.RewriterFactoryHandler, URLRewriter" 
      preCondition="integratedMode" />
      <add name="aspxHandler" verb="*" path="*.aspx" 
      type="URLRewriter.RewriterFactoryHandler, URLRewriter" 
      preCondition="integratedMode" />
    </handlers>
    <!-- 我的ECS伪静态配置要求 -->
    <modules runAllManagedModulesForAllRequests="true" />
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="DotNetOpenAuth.Core" 
        publicKeyToken="2780ccd10d57b246" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="DotNetOpenAuth.AspNet" 
        publicKeyToken="2780ccd10d57b246" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory 
    type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

但是由于云虚拟机现在是空闲着,每月有10G的免费流量,再加上网站访问量正在被我“优化”,若依然使用ECS的话,首先会造成服务器上的其他应用速度变慢,其次每月会白白损失8-9G的流量。所以再三考虑,最终决定转到云虚拟机上。

然而,将网站代码搬迁到云虚拟机上后,发现只有首页可以访问,其他页面直接报500错误,很纳闷,然后突然想到了伪静态问题,于是翻阅了阿里云的问题解答,发现,里边有解决方案如下:

虚拟主机/轻云服务器伪静态环境默认已经预装,只需用户在程序里设置伪静态规则即可。 

Window服务器设置方法:

用户可以在web.config文件设置伪静态规则,Windows虚拟主机UrlRewrite伪静态测试样例

Windows虚拟主机默认支持UrlRewrite伪静态,以下为我们提供的测试样例,可以上传到主机内部验证伪静态功能是否正常。

ASPASP.NETPHP脚本测试样例供参考:点击下载【UrlRewrite使用手册

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
    <rewrite>
        <rules>
          <rule name="rule1" stopProcessing="true">
          <match url="^article/([0-9]+)/([_0-9a-z-]+)"/>
          <action type="Rewrite" url="article.aspx?id={R:1}&amp;title={R:2}"/>
        </rule>
        <rule name="rule2" stopProcessing="true">
          <match url="^tags/([0-9]+)"/>
          <action type="Rewrite" url="tags.php?tags={R:1}"/>
        </rule>
        <rule name="rule3" stopProcessing="true">
          <match url="^test.html"/>
          <action type="Rewrite" url="test.asp"/>
        </rule>
      </rules>
    </rewrite>
    </system.webServer>     
</configuration>

url匹配的模式重新修改了一下,结果发现还是没有办法访问,在将要放弃的时候,竟然发现原来ECS上的配置是~开头,而云虚拟机上是^,修改之后全部搞定。但是有一点需要注意,将云虚拟机上的配置信息直接添加到配置文件中(也就是两套规则都存在时),这样的配置文件在云虚拟机上可以正常执行,但是放回到我的ECS中会出现500错误,所以只能哪天搬回ECS时将这段新代码删掉或者注释掉即可。

新的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <section name="entityFramework" 
    type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, 
    EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
    requirePermission="false" />
    <!-- 我的ECS伪静态配置要求 -->
    <section name="RewriterConfig" 
    type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
    <!-- 我的ECS伪静态配置要求 -->
  </configSections>
  <connectionStrings>
    <add name="数据库连接" connectionString=" " />
  </connectionStrings>
  <!-- 我的ECS伪静态配置要求 -->
  <RewriterConfig>
    <Rules>
      <RewriterRule>
        <LookFor>~/Default.shtml</LookFor>
        <SendTo>~/Default.aspx</SendTo>
      </RewriterRule>
 
      <RewriterRule>
        <LookFor>~/article/([0-9]*).shtml</LookFor>
        <SendTo>~/page/Content.aspx?id=$1</SendTo>
      </RewriterRule>
 
      <RewriterRule>
        <LookFor>~/list/([tksm])/([0-9]*)/(.*).shtml</LookFor>
        <SendTo>~/page/List.aspx?$1=$3&amp;p=$2</SendTo>
      </RewriterRule>
      <RewriterRule>
        <LookFor>~/list/([tksm])/(.*).shtml</LookFor>
        <SendTo>~/page/List.aspx?$1=$2</SendTo>
      </RewriterRule>
 
 
      <RewriterRule>
        <LookFor>~/Mobile.shtml</LookFor>
        <SendTo>~/Mobile.aspx</SendTo>
      </RewriterRule>
      <RewriterRule>
        <LookFor>~/marticle/([0-9]*).shtml</LookFor>
        <SendTo>~/page/MContent.aspx?id=$1</SendTo>
      </RewriterRule>
      <RewriterRule>
        <LookFor>~/mlist/([tksm])/([0-9]*)/(.*).shtml</LookFor>
        <SendTo>~/page/MList.aspx?$1=$3&amp;p=$2</SendTo>
      </RewriterRule>
      <RewriterRule>
        <LookFor>~/mlist/([tksm])/(.*).shtml</LookFor>
        <SendTo>~/page/MList.aspx?$1=$2</SendTo>
      </RewriterRule>
 
    </Rules>
  </RewriterConfig>
  <!-- 我的ECS伪静态配置要求 -->
  <system.web>
    <compilation targetFramework="4.0" />
    <httpRuntime requestValidationMode="2.0" />
    <pages validateRequest="false" />
 
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="6000" defaultUrl="~/"/>
    </authentication>
    <customErrors>
      <error redirect="/Page/404.aspx" statusCode="404" />
    </customErrors>
 
  </system.web>
  <system.webServer>
 
    <!--阿里云虚拟服务器上伪静态代码-->
    <rewrite>
      <rules>
        <rule name="rule1" stopProcessing="true">
          <match url="^Default.shtml" />
          <action type="Rewrite" url="Default.aspx"/>
        </rule>
        <rule name="rule2" stopProcessing="true">
          <match url="^article/([0-9]+).shtml" />
          <action type="Rewrite" url="page/Content.aspx?id={R:1}"/>
        </rule>
        <rule name="rule3" stopProcessing="true">
          <match url="^list/([tksm])/([0-9]+)/(.*).shtml" />
          <action type="Rewrite" url="page/List.aspx?{R:1}={R:3}&amp;p={R:2}"/>
        </rule>
        <rule name="rule4" stopProcessing="true">
          <match url="^list/([tksm])/(.*).shtml"/>
          <action type="Rewrite" url="page/List.aspx?{R:1}={R:2}"/>
        </rule>
 
        <rule name="rule5" stopProcessing="true">
          <match url="^Mobile.shtml" />
          <action type="Rewrite" url="Mobile.aspx"/>
        </rule>
        <rule name="rule6" stopProcessing="true">
          <match url="^marticle/([0-9]+).shtml"/>
          <action type="Rewrite" url="page/MContent.aspx?id={R:1}"/>
        </rule>
        <rule name="rule7" stopProcessing="true">
          <match url="^mlist/([tksm])/([0-9]+)/(.*).shtml"/>
          <action type="Rewrite" url="page/MList.aspx?{R:1}={R:3}&amp;p={R:2}"/>
        </rule>
        <rule name="rule8" stopProcessing="true">
          <match url="^mlist/([tksm])/(.*).shtml"/>
          <action type="Rewrite" url="page/MList.aspx?{R:1}={R:2}"/>
        </rule>
      </rules>
    </rewrite>
    <!--阿里云虚拟服务器上伪静态代码-->
 
    <!-- 我的ECS伪静态配置要求 -->
    <handlers>
      <add name="htmlHandler" verb="*" path="*.shtml" 
      type="URLRewriter.RewriterFactoryHandler, URLRewriter" 
      preCondition="integratedMode" />
      <add name="aspxHandler" verb="*" path="*.aspx" 
      type="URLRewriter.RewriterFactoryHandler, URLRewriter" 
      preCondition="integratedMode" />
    </handlers>
    <!-- 我的ECS伪静态配置要求 -->
    <modules runAllManagedModulesForAllRequests="true" />
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="DotNetOpenAuth.Core" 
        publicKeyToken="2780ccd10d57b246" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" 
        newVersion="4.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="DotNetOpenAuth.AspNet" 
        publicKeyToken="2780ccd10d57b246" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" 
        newVersion="4.1.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory 
    type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>


原创文章如转载,请注明出处“伊人博客

站内搜索
关注公众号(随时搜索)
文章归档
热门推荐
随机推荐
冷门晾晒