以前也尝试过使用nginx代替apache,不过折腾了许久,竟然搞不定各种奇奇怪怪的配置,于是不了了之。昨晚深感VPS被内存大户apache耗用得太厉害了,于是启动切换至nginx的计划。(话说,前几天看到了nginx发布了1.0.0正式版,也算是支持一下 :)

手动编译安装。默认带的配置nginx.conf有简约的例子。照着做。不懂的问谷歌。

。。。可是。。。然后。。。我竟然搞不定配置。。。恨。。。

说不出哪里不对,折腾了一晚上。郁郁地睡去。

今天早上起床,不再看各种博客、教程、谷歌了,直奔nginx.org看英文wiki!于是,发现了第一个趣事

Nginx works perfectly well with a wide variety of applications, and WordPress is certainly one of them. NginX’s configuration language is very powerful and straightforward if one is familiar with it, but often people coming from other servers are not sure how things work in NginX and just copy and paste whatever they see from a blog that seems to fill their needs. Everyone, especially people new to NginX, should check out the nginx.org documentation for an overview of how things work and should be done in NginX.

加粗部分即为亮点。大意是说,nginx的配置是很牛X的,但以前使用其他HTTP服务器的人常常不知道nginx的配置咋整,然后就从网上随便找个博客改改别人的配置就贴过来了。。。天啊!这不就是在说我么!!多次经验证明,拷贝配置是最纳闷最悲剧的事了。。。所以,大家要多去看nginx的文档啊!

然后我就虚心地阅读文档。嗯,原来nginx.conf里面的配置段是层层继承的,server{}里的root配置会继承给location{},所以那些每个location{}里都定义root的人肯定都是折翼的天使了。不过,Reference location的说明中,对某个有趣的特性讲述得太简陋了,倒是在try_files的说明里对location的@特性有个很详细的举例,我这里做一个类比:

# @xxx 定义一个名称
location @dabr {
  rewrite /([a-z]*)/(.*)$ /$1/index.php?q=$2&$arg;
}
location /netputer/ {
  try_files $uri $uri/ @dabr;
}
location /mytwitterapi/ {
  try_files $uri $uri/ @dabr;
}

我的理解就是,@abc能够用来给try_files之类的命令提供一个增强处理的机会,进行if{}判断啊,rewrite一下啊什么的。很少在其他博客中看到对这个特性的介绍,发现它觉得有些小惊喜。嘿嘿

另外,在第一篇入门指引上还发现了一个对php.ini的配置的安全提示:

As said previously, Nginx does not care about files but rather locations. This location block matches a URI that ends in .php but it does not care if it’s a file or not. Therefore a request for /forum/avatars/user2.jpg/index.php will be matched and sent to PHP, and if PHP is not configured properly PHP will then execute /forum/avatars/user2.jpg when /forum/avatars/user2.jpg/index.php doesn’t exist. This provides a huge security risk. Do note that this is not a bug in Nginx, it’s the intended behaviour and as such will not be “fixed”.
This can also be fixed on the PHP side by setting cgi.fix_pathinfo=0 in the php.ini file.

我了个去!再次说明网上教程什么的真是不靠谱啊!!例如我搜索【nginx php debian】所看到的这篇教程,里面就特别说明了要把fix_pathinfo设置为1(默认是0的)。。。而我还傻傻地信了。。。

最后,就是照着wiki,参考Reference来配置好了nginx。由于是手动编译nginx的,所有/etc/init.d/里没有nginx的启动脚本。不过尝试执行nginx -h,却发现它的命令行参数及其简约。其中有个参数-s signal基本上类似于一个控制器了,能够让nginx进程重新读配置、退出等:

-s signal : send signal to a master process: stop, quit, reopen, reload

嗯,所以觉得nginx还是很好很强大的嘛。。。

PS: blogilo程序还是不够靠谱。发表完后总是还要到wordpress后台进行二次排版。囧。

,