Posts Tagged ‘nginx’

1and1 cloud server datasheet

Wednesday, July 7th, 2010

从 1&1 新订了一个合同,cloud server,就是为了让 magento 跑快一些。那 1and1 的 cloud server 究竟能有多快呢?

先看看 cat /proc/cpuinfo 的情况
processor : 0
vendor_id : AuthenticAMD
cpu family : 16
model : 2
model name : Quad-Core AMD Opteron(tm) Processor 2352
stepping : 3
cpu MHz : 2109.718
cache size : 512 KB
fpu : yes
fpu_exception : yes
cpuid level : 4
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat clflush mmx fxsr sse sse2 syscall mmxext fxsr_opt lm 3dnowext 3dnow pni cx16 popcnt lahf_lm cr8_legacy altmovcr8 abm sse4a misalignsse
bogomips : 4219.43
TLB size : 1024 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management:

processor : 1
vendor_id : AuthenticAMD
cpu family : 16
model : 2
model name : Quad-Core AMD Opteron(tm) Processor 2352
stepping : 3
cpu MHz : 2109.718
cache size : 512 KB
fpu : yes
fpu_exception : yes
cpuid level : 4
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat clflush mmx fxsr sse sse2 syscall mmxext fxsr_opt lm 3dnowext 3dnow pni cx16 popcnt lahf_lm cr8_legacy altmovcr8 abm sse4a misalignsse
bogomips : 4220.53
TLB size : 1024 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management:

processor : 2
vendor_id : AuthenticAMD
cpu family : 16
model : 2
model name : Quad-Core AMD Opteron(tm) Processor 2352
stepping : 3
cpu MHz : 2109.718
cache size : 512 KB
fpu : yes
fpu_exception : yes
cpuid level : 4
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat clflush mmx fxsr sse sse2 syscall mmxext fxsr_opt lm 3dnowext 3dnow pni cx16 popcnt lahf_lm cr8_legacy altmovcr8 abm sse4a misalignsse
bogomips : 4223.97
TLB size : 1024 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management:

processor : 3
vendor_id : AuthenticAMD
cpu family : 16
model : 2
model name : Quad-Core AMD Opteron(tm) Processor 2352
stepping : 3
cpu MHz : 2109.718
cache size : 512 KB
fpu : yes
fpu_exception : yes
cpuid level : 4
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat clflush mmx fxsr sse sse2 syscall mmxext fxsr_opt lm 3dnowext 3dnow pni cx16 popcnt lahf_lm cr8_legacy altmovcr8 abm sse4a misalignsse
bogomips : 4219.25
TLB size : 1024 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management:

在 cloud server 上我使用 zend server。我特意从 1 Virtual processor core 1GB Ram 一步步往上加,使用 ab -c 5 -n 500 对比测试,客户端是 10 Mb down stream / 1Mb up stream adsl 连接。

在 1 Virtual processor core 1GB Ram 时,Requests per second: 5.31 [#/sec]
在 2 Virtual processor cores 1GB Ram 时,Requests per second: 8.34 [#/sec]
在 2 Virtual processor cores 2GB Ram 时,Requests per second: 8.26 [#/sec]
在 3 Virtual processor cores 1GB Ram 时,Requests per second: 10.57 [#/sec]
在 4 Virtual processor cores 1GB Ram 时,Requests per second: 11.33 [#/sec]

在 shopping cart 里有 7 条不同商品时,checkout/cart/index 页面时间为 10 秒左右。

回头看看老 server
# cat /proc/cpuinfo
processor : 0
vendor_id : AuthenticAMD
cpu family : 15
model : 67
model name : Dual-Core AMD Opteron(tm) Processor 1216 HE
stepping : 3
cpu MHz : 1000.000
cache size : 1024 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 2
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 1
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp lm 3dnowext 3dnow rep_good nopl pni cx16 lahf_lm cmp_legacy svm extapic cr8_legacy
bogomips : 1999.96
TLB size : 1024 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 40 bits physical, 48 bits virtual
power management: ts fid vid ttp tm stc

processor : 1
vendor_id : AuthenticAMD
cpu family : 15
model : 67
model name : Dual-Core AMD Opteron(tm) Processor 1216 HE
stepping : 3
cpu MHz : 1000.000
cache size : 1024 KB
physical id : 0
siblings : 2
core id : 1
cpu cores : 2
apicid : 1
initial apicid : 1
fpu : yes
fpu_exception : yes
cpuid level : 1
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp lm 3dnowext 3dnow rep_good nopl pni cx16 lahf_lm cmp_legacy svm extapic cr8_legacy
bogomips : 1999.96
TLB size : 1024 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 40 bits physical, 48 bits virtual
power management: ts fid vid ttp tm stc

运行的是 nginx,同等条件下,Requests per second: 8.12 [#/sec],checkout/cart/index 页面生成时间也是 10 秒左右。

虽 zend server 和 nginx 不同,靠不是完全对等的对比结果,我还是可以得出两个结论:

  1. 瓶颈仍是 cpu 速度;
  2. cloud server 并未显著提高速度,我对它预期过高,略有失望。

Nginx try_files syntax

Thursday, February 4th, 2010

今天在一台很久不用的服务器上测试 Magento search result page,URL /catalogsearch/result/?q=searchword,发现它不工作,但其他页面正常。这个症状让我联想到以前碰到的类似问题,Magento 无法获得 query_string,所以含问号的 URL 都不能处理,页面重定向到 referring URL。应该是 server rewrite 规则没有写正确,我想。打开 nginx 的配置文件一看,果然,当中一条规则用的是很久以前的写法,后来在不同的服务器上几经改进,production server 都已经用上了新规则。

新规则的写法:


location @magento {
root $php_script_root;
index index.php;
if ($uri ~ ^/(media|js|skin)/) {
break;
}
if (!-e $request_filename) {
rewrite .* /index.php last;
}
}

而老规则的写法:


location @magento {
root $php_script_root;
index index.php;
if ($uri ~ ^/(media|js|skin)/) {
break;
}
try_files $uri $uri/ /index.php;
}

效果略有区别,我在 Difference of try_files to rewrite in Nginx 文章里有提及。不过,今天我还有一个新发现。

我倾向于使用简介语法,try_files 就比 rewrite 简洁得多,难道 try_files 就没有办法应付带问号的 URL 吗?非也,是我不知道 Nginx 原本可以这么奥妙——用 $args 变量!

因此,最新一条完美规则出炉了:


location @magento {
root $php_script_root;
index index.php;
if ($uri ~ ^/(media|js|skin)/) {
break;
}
try_files $uri $uri/ /index.php?$args;
}

Avoid PEM pass phrase

Thursday, November 26th, 2009

我在制作 SSL key file 时输入了一个 pass phrase。CA 把 SSL 证书发给我后,我在 Nignx 试着加载 key 和 证书,发现每次重启 Nginx 时,都会被要求 Enter PEM pass phrase。岂不很烦,而且万一服务器重启,岂不还要人工干预才能重启 web server?

原本以为把 pass phrase 从 key 文件里拿掉后,要找 CA 重新制作证书,后来发现不用,证书跟 pass phrase 无关。Nginx 的文档没有提及,Apache 倒是有提:

If necessary, you can also create a decrypted PEM version (not recommended) of this RSA private key with:

openssl rsa -in server.key -out server.key.unsecure

拿到 pass phrase 后安全性自然降低了,不过完全值得。

Gracefully restart Nginx

Friday, August 7th, 2009

Follow these 3 steps to gracefully stop and start Nginx without losing any queries. It works like a charm.

  1. Test new configuration is correct
    nginx -t
  2. Find the Pid
    ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
  3. And kill it. Meanwhile, new configuration is already effective.
    kill -HUP ????
    (replace ???? with the Pid given in step 2)

Speed bottleneck of the web server

Tuesday, August 4th, 2009

非常典型的多层架构:
第一层 Nginx
第二层 php fastcgi
第三层 memcached
第四层 MySql

Apache 有个 mod_php,相当于合并了第一层和第二层,Nginx 没有 module for php,这不是什么问题,分层更利于扩展。第三层的加入完全是为了减轻数据库压力,提高性能。目前第2,3,4层之间的优化差不多到极限了(或者说到我能力的极限了),但第1,2层之间尚有潜力可挖。

not_in_use.php 和 not_in_use.html 都是一个静态文件,没有数据库操作。但 php 文件必须由 Nginx 经由 php fastcgi (使用 unix socket)产生,html 则由 Nginx 直接访问文件系统,就单因素分析,php fastcgi 是普通文件系统速度的34%,所以要想办法绕开 php fastcgi。以下测试在数据中心主机上直接运行 ApacheBench。

测试一:
$ ab -kc 100 -n 500 http://magento/not_in_use.php
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking magento (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Finished 500 requests

Server Software: nginx/0.6.36
Server Hostname: magento
Server Port: 80

Document Path: /not_in_use.php
Document Length: 7686 bytes

Concurrency Level: 100
Time taken for tests: 0.336355 seconds
Complete requests: 500
Failed requests: 0
Write errors: 0
Keep-Alive requests: 0
Total transferred: 4089329 bytes
HTML transferred: 4004406 bytes
Requests per second: 1486.52 [#/sec] (mean)
Time per request: 67.271 [ms] (mean)
Time per request: 0.673 [ms] (mean, across all concurrent requests)
Transfer rate: 11871.39 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 11 17.8 3 58
Processing: 13 49 17.9 50 94
Waiting: 3 45 18.8 46 88
Total: 25 60 14.2 61 94

Percentage of the requests served within a certain time (ms)
50% 61
66% 70
75% 72
80% 74
90% 79
95% 80
98% 84
99% 86
100% 94 (longest request)

=======================================================
测试二:
$ ab -kc 100 -n 500 http://magento/not_in_use.html
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking magento (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Finished 500 requests

Server Software: nginx/0.6.36
Server Hostname: magento
Server Port: 80

Document Path: /not_in_use.html
Document Length: 7686 bytes

Concurrency Level: 100
Time taken for tests: 0.115725 seconds
Complete requests: 500
Failed requests: 0
Write errors: 0
Keep-Alive requests: 500
Total transferred: 3959000 bytes
HTML transferred: 3843000 bytes
Requests per second: 4320.59 [#/sec] (mean)
Time per request: 23.145 [ms] (mean)
Time per request: 0.231 [ms] (mean, across all concurrent requests)
Transfer rate: 33406.78 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 3 7.3 0 22
Processing: 7 17 4.1 18 23
Waiting: 7 16 4.0 17 23
Total: 7 20 9.9 18 41

Percentage of the requests served within a certain time (ms)
50% 18
66% 20
75% 22
80% 37
90% 39
95% 40
98% 41
99% 41
100% 41 (longest request)

php always takes apache as its session owner

Tuesday, July 14th, 2009

今天用 yum update php 到 5.2.10 版,发现升级了以后 /var/lib/php/session 的 ownship 又成了 root:apache。这有点恼人,难道每次升级我都要手工改一次 chown nginx:nginx /var/lib/php/session? 或是以后用 apache 的身份来跑 nginx? 我觉得都不太好,太多的场合只认 apache as an only http server,搞得 nginx 很孤立 :(

Difference of try_files to rewrite in Nginx

Sunday, June 7th, 2009

try_files 是 Nginx 自0.6.36 新增的 directive。通常对 try_files 的理解是——它是
if (!-e $request_filename) {
rewrite .* /index.php;
}

的更简洁的表达。但我发现它们之间还是略有区别,主要表现在两点:

  1. 如使用 rewrite,Nginx 要求 document_root 下必须有一个 index 文件,即使我把另一个目录别名整个 document_root,但因为 Nginx 先校验 document_root 下是否有一个 index 文件,所以得放置一个空 index 文件。try_files 则没有 index 文件校验,直接去第二、第三… document_root 找对应的文件。
  2. 如使用 try_files $uri $uri/ /index.php,index.php 不能取得 get 变量,例如在 Magento 下,对 http://mydomain/catalogname?mode=list 的访问不能起效,Nginx 交付的页面是 http://mydomain/catalogname。这时必须使用 rewrite。这或许是 try_files 的一个 bug。

Varnish vs Nginx

Wednesday, June 3rd, 2009

Fedora 10 release note 提及 Varnish,据说“Varnish 是一个高性能 HTTP 加速器”,又看了一些其他网站的介绍,据说比 squid 快了好几倍。squid 是什么?没听说过。但既然同是反向代理服务器,Varnish 和 Nginx 相比如何?

我没实测,但引用别人的测试结论:

  • 根据 Connection Times 获知 Varnish 连接速度快于 Nginx,但 Nginx 处理速度快于 Varnish,等待时间几乎一致,总时间 Nginx 要快于 Varnish 15%左右。
  • 根据 HTML transferred 项获知大负载下 Varnish存在较多的丢包问题。

我想我还是坚持走 Nginx,并走好它。

Config CentOS to use the latest software

Saturday, May 23rd, 2009

1and1 VPS OS 可选清单里没有 Fedora,不得不说是个遗憾。当然 CentOS 也不错,可是以前都没接触,使用 CentOS 会有很长的学习过程,但没办法,只能装个 CentOS 用用看。

以前我调查过,CentOS 讲究兼容性,Fedora 追求技术领先。CentOS 安装真得比当初装 Fedora 要容易(Fedora 从6开始到10,我没有一次是一次启动就安装成功的),今天装CentOS 5很顺利(CentOS 兼容性真的那么好?还是我从 Fedora 学来的经验丰富了?)

装完 CentOS,我就面临一个问题:不能 yum install nginx;yum install php 得来的版本也只是 5.1.6。这个简单的比较才觉得 Fedora 的好。我无法想象我还要下载编译 nginx(那一套我还不熟,怕搞砸了)。不过,Extra Packages for Enterprise Linux (EPEL) 已经做好了一个 repository,只要添加这个 repository,

sudo rpm -Uvh http://download.fedora.redhat.com/pub/epel/5Server/x86_64/epel-release-5-3.noarch.rpm

然后就可以用 yum install nginx。

但是,yum install php 得来的版本只能是 5.1.6,目前还没找到好办法。

Create a password file for Nginx basic authentication

Saturday, May 16th, 2009

It is really a hard time for me to find a way to create a password file for Nginx basic authentication, because I did not realise htpasswd crpty is not available on Windows.

Nginx documentation only mentions Use crypt(3) encryption for passwords, so I tried so hard with htpasswd.exe but could not generate a file recognised by Nginx. Hours later I found Apache documentation mentions -d is

the default on all platforms but Windows, Netware and TPF. Though possibly supported by htpasswd on all platforms, it is not supported by the httpd server on Windows, Netware and TPF.

I have two things to blame -

  1. I still use Windows to do my everyday work. If my first choice was htpasswd on Linux even without furthur instructions, it would have save me a lot of time.
  2. Nginx documentionation. If Nginx documentation is as good as Apache’s, Nginx may overwhelm the world.