So Tired !_! 逆水行舟, 不进则退!

26Jul/12

解决IIS7返回header里面的date值不正确导致firefox里cookies无效的问题.

Posted by Nick Xu

问题:

突然有一天,easybug.net上的登录功能无法使用,原本以为是自己电脑有问题,所以没有理会.后来又有好些用户反应无法登录.我就奇怪了,最近都没有更新过什么,怎么会突然就登录不了了呢.

用了很多方法都没有查到具体的原因,后来因为我点击切换语言的时候,发现这个功能也失效了.所以大致的原因可以锁定在cookies上了.用firebug上的net和cookies模块来监控http请求和cookies的值,发现在net里面是有带了cookies,但是在cookies标签里却看不到系统创建的cookies,就是说浏览器没有把cookies写入到浏览器里面,查了半天没找到什么原因,太神奇了...

后来想想既然在net标签里都已经有cookies信息了,那浏览器应该也是收到的了,会不会是在header里有什么问题呢?果然,看到一个很奇特的信息,在header里面看到这么一行信息:

date:2013-7-25 19:32:28

奇怪了,怎么日期是2013年呢,现在才2012年呢...于是顺藤摸瓜,查一下http协议相关的资料,果然被我查到这样一篇内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Problem Description
 
W3SVC logs show incorrect time - stuck on the same time 2007-11-04 04:56:06
REPRO STEPS
 
    Changed the system time to a future time (e.g. 11th May 2020)
    Browsed any site - verified the log file created in the new date/time
    Changed the system time to be correct (today's date and time)
    Now, browsed any website and saw that the time on the log files is now stuck on 11th May 2020's time
 
RESOLUTION
 
    net stop http
    net start w3svc
 
OR
 
    net stop http
    net start http
    iisreset
 
You must restart the http service whenever you change the date in the server.
 
Verified in the code and confirmed that this is by design and you need to follow the above steps to make IIS logging the correct time.

 

大致的意思是在IIS上,如果修改了服务器当前的日期到2020年,然后访问服务器上的站点,日志文件会以当前日期来记录日志内容,此时再把服务器上的日期修改回正确的日期,再访问站点,日志文件里的日期还是2020年的日期,不会自动修改回来,要解决这种问题,只有重启IIS,而且还一定要重启http这个服务,不然不会有效果的.我按上面的步骤试了一下,再来看一下header里面的date,果然现在已经是正确的值了,试一下登录easybug.net,yeah!!登录成功了!!!

总结出现以上问题的原因来自几个方面:
1. 这算是IIS的一个小小的BUG,不过为何会出现日期不一至呢?大部分是人为的原因造成的.
2.各个浏览器里面,好像只有Firefox在IIS出现这种情况的时候有问题,其它浏览器没有问题.
3.通过设置cookies的失效时间大于header里面date的时间应该也可以解决这个问题,不过没有去测试.
4.百年一遇的情况,RP问题啊:(

 

16Jul/12

Setting up a Mercurial server under IIS7 on Windows Server 2008 R2

Posted by Nick Xu

This guide walks you through setting up a Mercurial server under IIS7 on Windows Server 2008 R2.

Note: This post uses Mercurial 1.4.3 and Python 2.5.4, although this process will also work with Mercurial 1.5 and Python 2.6.4

Install Mercurial

First you’ll need to download and install Mercurial. I won’t walk through this as it’s a simple case of pressing "next" several times.

Install Python

Next you’ll need to install Python. Note that you need to use the same version of Python that was used to build Mercurial. This guide uses Mercurial 1.4.3 with Python 2.5.4, but if you’re installing Mercurial 1.5 then you’ll need to use Python 2.6 instead.

Be sure to install the x86 version of Python even if you’re running on an x64 system.

Get hgwebdir

Next you’ll need to download hgwebdir.cgi. This is the python script that will allow serving Mercurial repositories through a web server.

Hgwebdir is part of the Mercurial source code, so you’ll need to download the source package to get it. This can be found on the Mercurial site or you can check out the hg source by running the following command:

hg clone http://selenic.com/repo/hg

Once downloaded, hgwebdir.cgi is in the root of the source distribution.

Install IIS

Under Windows Server 2008 you can install IIS under the Server Manager and clicking "Add Roles". Proceed through the wizard and select the "Web Server (IIS)" role.

Under "Role Services" ensure that you enable Basic Authentication as well as CGI extensibility.

Configure Python for IIS

First, create a new directory under the wwwroot directory (C:inetpubwwwroot). I’m going to call it "hg".

In the "Handler mappings" section for this directory select "Add Script Map":

Next, enter *.cgi as the Request Path and the Executable Path should be set toc:Python25python.exe -u "%s". Enter "Python" as the Name.

At this point, you can test whether Python is working properly by creating a simple python script:

print 'Status: 200 OK'
print 'Content-type: text/html'
print

print '<html><head>'
print ''
print '<h1>It works!</h1>'
print ''
print ''

Save this in the directory that you created (C:inetpubwwwroothg) as test.cgi. Now, when you point your browser to http://localhost/hg/test.cgi you should see the following output:

Enabling hgwebdir.cgi

First, copy hgwebdir.cgi (that you downloaded in step 3) and paste it into c:inetpubwwwroothg. Open this file in a text editor and scroll down to the end. The last lines should look like this:

application = hgwebdir('hgweb.config')
wsgicgi.launch(application)

Change the first line to explicitly specify the path to your hg directory:

application = hgwebdir('c:inetpubwwwroothghgweb.config')
wsgicgi.launch(application)

Next, you’ll need to unzip the Mercurial library into c:inetpubwwwroothg. This can be found inLibrary.zip under the c:program files (x86)Mercurial directory.

You’ll now need to copy the hgweb templates directory into c:inetpubwwwroothg. This is located in the root of the Mercurial installation directory (C:program files (x86)Mercurial)

Finally, create a file called hgweb.config in c:inetpubwwwroothg. This file can be empty for now (we’ll be putting something in it shortly).

At this point, visiting http://localhost/hg/hgwebdir.cgi will show you an empty repository page:

Configuring Repositories

Now you’ll need to create some repositories to publish. To do this, create a directory in the root of the C: drive called "Repositories". This is where our repositories are going to be stored.

Next, I’m going to create a "test" repository by issuing the following commands:

cd c:repositories
mkdir test
hg init test

Now we have a repository created, we need to tell hgwebdir where to find it. We can do this by opening up the hgweb.config file we created earlier and adding the following lines:

[collections]
C:repositories = C:repositories

Now, visiting http://localhost/hg/hgwebdir.cgi should display our "test" repository

At this point it should now be possible to clone the test repository from the server with the following command:

hg clone http://localhost/hg/hgwebdir.cgi/test

Pretty URLs

Personally, I don’t like having to specify "hgwebdir.cgi" in the URLs. I’d much prefer something like http://localhost/hg/test to access my test repository.

This can be achived by using the URL rewriting extension for IIS which can be downloaded from Microsoft.

Once installed, you can access the URL rewriting settings though the "URL Rewrite" section of the IIS Manager. Select the "hg" subdirectory in the Connections pane and then select "URL Rewrite":

In the URL rewrite section add a new blank rule. The name of the rule is going to be "rewrite to hgwebdir".

Under the "Match URL" section set "Using" to "Wildcards" and set the "Pattern" to "*"

Under "Conditions" we want to ensure that we do not re-write URLs to any physical files, so add a condition for "Is Not a File":

In the "Rewrite URL" box at the bottom of the screen enter hgwebdir.cgi/{R:1}

The end result will look like this:

Finally, re-open your hgweb.config and add the following section:

[web]
baseurl = /hg

This will ensure that hgwebdir generates urls to /hg rather than /hg/hgwebdir.cgi

Now, visiting http://localhost/hg will display our repositories page and http://localhost/hg/test will show our test repository. Likewise, we can now clone repositories using this url format.

Pushing Changes

By default, all repositores served via hgwebdir are read only – you cannot push changes to them. To change this, we can specify the users that should be able to push to the repositores by adding an "allow_push" section to our hgweb.config:

[collections]
c:repositories = c:repositories

[web]
baseurl = /hg
allow_push = Jeremy

This means that the user account "Jeremy" (a local user account on the server) will have push access to the repository.

However, if we try and push changes we’ll get an error:

c:projectstest&gt;hg push
pushing to http://localhost/hg/hgwebdir.cgi/test
searching for changes
ssl required

For now, we’ll disable SSL by setting push_ssl to false in our hgweb.config:

[collections]
c:repositories = c:repositories

[web]
baseurl = /hg
allow_push = Jeremy
push_ssl = false

Now when we try and push we get a different error:

c:projectstest&gt;hg push
pushing to http://localhost/hg/hgwebdir.cgi/test
searching for changes
abort: authorization failed

This happens because by default IIS is serving up our site without authentication. We need to enable Basic Authentication in the Authentication area of IIS:

Now you’ll be prompted to enter your username and password:

After specifying the credetails, the changes will be pushed up. We can view the commit in our web UI:

Enabling SSL

When you use Basic authentication, your username and password will be sent over the wire in plain text. To make this more secure we can enable SSL. For this example I’m going to use a self-signed certificate, although this will also work with a real SSL certificate purchased from a provider.

First, you’ll need to go into the IIS manager, select "Server Certificates" and click "Create Self-Signed Certificate"

Now, add a binding for your Web Site for https on port 443 by right clicking on the site and selecting "Edit Bindings".

Add a new binding for https on port 443:

Once this is done, you should now be able to access the hgwebdir site by using https (https://localhost/hg). You’ll probably get an invalid certificate warning in your browser.

Now you can re-clone the repository using the secure url (also be sure to remove the "push_ssl = false" line from hgweb.config)

All done!

At this point, you should have successfully set up everything you need to use Mercurial in IIS7.

25May/10

在不同版本的IIS下使用ASP.NET MVC

Posted by Nick Xu

在这篇文章中我们学习在不同版本的IIS中使用ASP.NET MVC和URL Routing。我们学习针对IIS7.0、IIS6.0和更早版本的IIS的处理策略
ASP.NET MVC框架依赖于URL Routing,要充分利用URL Routing,我们需要对Web服务器(IIS)进行一些额外的配置。
IIS的最新版本是Windows2008中的IIS7.0,我们也可以Vista系统中(除了Home Basic版)安装IIS7.0。
IIS7.0是提供了两种处理请求的模式-集成模式传统模式如果使用IIS7.0的话,那我们不需要做任何配置,如果要使用传统模式的 话则需要我们做一些额外的配置工作。
在Windows2003中安装的是IIS6.0,如果是Windows Server 2003的话,那我们可以把IIS6.0升级到IIS7.0。但如果我们使用IIS 6.0的话,那需要我们做一些额外的配置工作。
Windows XP专业版安装的IIS5.1版本,因此我们也需要对IIS5.1进行一些额外的配置工作。
最后Windows2000使用的是IIS5.0,我们也需要对IIS5.0进行一些额外的配置工作。
下面是我们对不同版本IIS的总结:
IIS7.0(集成模式)- 不需要作任何配置就可以使用URL Routing。
IIS7.0(传统模式) - 需要我们进行特别配置来使用URL Routing。
IIS6.0或更低版本 - 需要我们进行特别配置来使用URL Routing。

一、集成模式与传统模式(原创:灰灰虫的家 http://hi.baidu.com/grayworm
IIS7.0可以使用两种模式来处理请求-集成模式和传统模式。集成模式提供更好、更多的功能;传统模式则是为了向后兼容较早版本的 IIS。
请求的处理模式是由程序池决定的我们可以通过指定程序池与应用程序关联的方式 来设置web应用程序的请求处理模式。步骤如下:
1. 运行IIS服务管理器
2. 在Connections窗口选择一个应用程序
3. 在Actions窗口中单击Basic Settings连接打开Edit Application对话框,如下图所示。
4. 设置Application pool。
默认情况下,IIS被配置为支持两种应用程序池:DefaultAppPoolClassic .NET AppPool。如果设为DefaultAppPool,那我们的应用程序 就运行在集成请求处理模式下。如果设置Classic .NET AppPool,那我们的应用程序就运行在传统的请求处理模式下。
《图1》
需要注意的是,我们通过点击Edit Application对话框中的”Select”按钮来改变程序池与应用程序的关联关系,来修改请求处理模式。但ASP.NET应用程序在从传统模式迁 移到集成模式的时候需要解决几个兼容性的问题。更多的信息请参见下列文章:
Upgrading ASP.NET 1.1 to IIS 7.0 on Windows Vista and Windows Server 2008 --http://learn.iis.net/page.aspx/270/upgrading-aspnet-11-to-iis7-on-windows-vista--windows-server-2008/
ASP.NET Integration With IIS 7.0 - http://learn.iis.net/page.aspx/243/aspnet-integration-with-iis7/
如果我们的ASP.NET应用程序使用的是DefaultAppPool,那我们不用做任何配置就可以使用URL Routing功能了。但如果ASP.NET程序被配置为Classic .NET AppPool,那请继续向下看。

二、在旧版本的IIS中使用ASP.NET MVC(原创:灰灰虫的家 http://hi.baidu.com/grayworm
如果我们使用的IIS版号比IIS7.0低或者使用的是IIS7.0传统模式,那我们有两种选择:
1、 修改路由表,加上文件扩展名。如我们把/Store/Details的URL地址换为/Store.aspx /Details
2、 创建wildcard script map。wildcard script map使我们把每个请求都映射到ASP.NET框架上。
如果我不能修改服务器的配置,那我们只好采用第一种方式,如果我们不想修改URL地址,那我们必须采用第二种方式,去配置IIS web服务器。

在这是我们分别讨论两种方法:
(一)向路由表中加扩展名。
要使URL Routing能够在旧版本IIS上运行的最简单的方法就是打开Global.asax文件,修改我们的路由表。路由表的代码如下:
Listing 1 – Global.asax (unmodified)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcAppCS
{
public class GlobalApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",// Route name
"{controller}/{action}/{id}",// URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
}
默认的路由配置可以对我们下面的ULR地址进行路由:
/Home/Index
/Product/Details/3
/Product
然而不幸的是,旧版本的IIS不会把这样的请求传递给ASP.NET框架,因此这些请求也不会被路由 到控制器。比如我们请求/Home/Index这个URL时,会产生一个错误页面的提示。如下图

《图2》
旧版的IIS只能把那些具有特定扩展名的URL请求发送给ASP.NET框架。比如/SomePage.aspx请求会被映射到ASP.NET框架,而 /SomePage.htm则不会被映射到ASP.NET框架。
因此,要想使URL Routing能够正常工作,我们必须要修改默认路由,使其包含文件扩展名,以映射到ASP.NET 框架中去。能 够映射到ASP.NET框架中的扩展名有.aspx .axd 和.ashx
修改后的Global.asax文件如下所示
Listing 2 – Global.asax (modified with extensions)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcAppCS
{
public class GlobalApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}.aspx/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = ""}// Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
}
注意:在修改完Global.asax文件后要记得重新编译我们的ASP.NET MVC应用程。
在上面的代码中,我们做了一个很小但很重要的修改,我们把默认路由修改为如下格式:
{controller}.aspx/{action}/{id}
由于这种更改,因此ASP.NET MVC应用程路由只能映射下面这种形式了
/Home.aspx/Index
/Product.aspx/Details/3
/Product.aspx
当我们修改完路由表后,我们还要确保程序中的所有超连接的URL地址也都已做相应的修改。换句话说,要保证所有的超链接导航地址中都包含.aspx扩展 名。如果我们使用Html.ActionLink()方法生成的超链接,那我们不需要对超链接做更改。

(二)创建Wildcard Script Map(原创:灰灰虫的家 http://hi.baidu.com/grayworm
如果我们不想修改ASP.NET MVC应用程序中的URL地址,并且可以访问到Web服务器,那我们可以通过创建 wildcard script map方式来把所有请求映射到ASP.NET框架中去。这样就避免修改默认的由表了。
要搞明的一点是,这种修改会使IIS对每个请求都会处理,那怕是请求一张图片、ASP页面、HTML页面。因此使用wildcar script map会使操作更隐式。

启用IIS7.0的wildcard script map
1. 在Connections窗口中选择我们的应用程序
2. 确定选中了Features视图。
3. 双击Handler Mappings按钮。
4. 单击Add Wildcard Script Map,如图。
5. 输入aspnet_isapi.dll文件的路径
6. 在Name文本框中输入MVC
7. 点击OK按钮。

《图3》

在IIS6.0中创建wildcar script map的步骤:
1、 右击站点,选择属性
2、 选择“Home Directory”选项卡
3、 点击“Configuration”按钮
4、 选择“Mappings”选项卡
5、 点击“Insert”按钮,如下图所云汉
6、 在Executeable文本框中输入aspnet_isapi.dll文件所在的路径
7、 去掉Verify that file exists复选框前的勾。
8、 点击OK按钮
《图4》

当我们配置完wildcard script map我们可以使用默认的路由表来处理下面的URL地址
/Home/Index
/Product/Details/3
/Product

总结

这篇文章中我们解释了如何在旧版本IIS(或IIS7.0传统模式下)运行ASP.NET MVC程。我们讨论了两种方式来解决URL Routing与旧版本IIS协同工作问题 :修改默认路由表或创建wildcard script map

第一种方法需要我们修改ASP.NET MVC应用程序,这种做法的好处是,我们不需要操作web服务器,而只是修改一下程序中的路由表即可。

第二种方法是需要我们创建wildcard script map,这种做法的好处是,我们不需要修改我们的代码,但缺点是会影响ASP.NET MVC程序的性能。

Tagged as: , , No Comments
   
site
site