最近尝试启用Nginx作为替代lighttpd的备选,尝试了服务的切换,但让我有点抓狂,location指令太扯了。

启用Nginx替代lighttpd有两个简单的理由:
1 lighttpd重启服务有中断,不管这是1秒还是半秒,这都会让正在连接的服务中断
2 lighttpd还没法支持日志里面记录cookie,很遗憾

其它方面,我觉得两者都差不多,但Nginx有主进程,理论上也比lighttpd要稳定些,记录cookie的方法见之前的文档《Nginx日志记录cookie》。

在转换lighttpd的服务的过程中,先看一下lighttpd的写法:

fastcgi.server = ( ".php" =>
      (( "host" => "127.0.0.1",
         "port" => 9000,
      ))
)
$HTTP["host"] == "www.aslibra.com" {
        server.document-root = "/Data/webapps/www.aslibra.com"
        url.rewrite-once = (
                "^/a" => "/script_a.php",
                "^/b" => "/script_b.php",
        )
        $HTTP["url"] =~ "^\/script_a" {
                accesslog.filename = "|/Data/apps/cronolog/sbin/cronolog /Data/weblog/%Y/%m/%d/a.%m%d%H"
        }
        $HTTP["url"] =~ "^\/script_b" {
                accesslog.filename = "|/Data/apps/cronolog/sbin/cronolog /Data/weblog/%Y/%m/%d/b.%m%d%H"
        }
}


前面是设置fastcgi服务器,后面是rewrite和分开记录日志,当然,rewrite可以简洁些的,这不改了

这一段改写为nginx的配置可以大概这么写:

server {
  listen       88;
  server_name www.aslibra.com;
  root   /Data/webapps/www.aslibra.com/;

  rewrite  ^/(a|b)  /script_$1.php;

  location ~* ^/script_a.php {
    fastcgi_pass  127.0.0.1:9000;
    access_log  /Data/log/nginx/sub.log  log_psc;
  }
  location ~* ^/script_b.php {
    fastcgi_pass  127.0.0.1:9000;
    access_log  /Data/log/nginx/start.log  log_psc;
  }
  location ~* .*\.php?$ {
    fastcgi_pass  127.0.0.1:9000;
  }
}


这里当然忽略了fastcgi的更多配置,那些可以在http段就设置好,这样在这里就不用设置了
使用nginx,要注意的是,很多指令是规定了在哪个指令里面的:

引用
rewrite
syntax: rewrite regex replacement flag
default: none
context: server, location, if


比如,rewrite是必须在server、location和if语句里面,在http里面无法编写默认的rewrite

令我抓狂的不是rewrite而是location,本来想着匹配顺序的,先把这些请求rewrite,然后分开记录日志,然后设置php的解析
结果居然没有运行php,直接返回php文件内容了。弄了很久才清楚,location只能匹配一个,所以上面location设置了日志后,下面正则处理php的就无效了,所以得在每个location里面都要完成所有的处理,麻烦啊

引用
To summarize, the order in which directives are checked is as follows:
1 Directives with the = prefix that match the query exactly. If found, searching stops.
2 All remaining directives with conventional strings. If this match used the ^~ prefix, searching stops.
3 Regular expressions, in the order they are defined in the configuration file.
4 If #3 yielded a match, that result is used. Otherwise, the match from #2 is used.


也就是说,找到一个最佳的location段的设置后,停止匹配,就是只会进入一个location段了。

上面的fastcgi_pass只能在 “location, if in location” 里面,所以这里必然要使用location段,只能每个location单独处理了。
access_log的设置只能在 http, server, location 里面,要分地址记录日志,那肯定得用location,if语句都不行,出错。另外,还不支持正则匹配出结果,要明文写好,不过好像也可以支持的,wiki有介绍,不过有一些限制。

在lighttpd里面,很多可以默认设置的内容在这里似乎还不太好处理,有点陌生的感觉,包含server的定义都不太好匹配,都是只是选择其中一个的,没有默认server,后面修改特殊定义的道理。



原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘
中国第一花木网 Homepage Email
2011/07/03 10:26
终于找到了呵呵
新易网 Homepage
2009/07/17 11:47
博主辛苦了
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemotemotemotemotemotemotemotemotemot
emotemotemotemotemotemotemotemotemotemotemotemot
打开HTML 打开UBB 打开表情 隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]
               

验证码 不区分大小写
 

阅读推荐

服务器相关推荐

开发相关推荐

应用软件推荐