Ngnix 502 bad gateway error after a recent php-fpm update

I recently updated php-fpm from 5.4.16 to 5.5.16. After the update, Nginx comes up with 502 bad gateway error. Googling the error pointed me to look into permission of php socket file. In php-fpm 5.4.16, if no value is given to listen.mode, php socket file is assumed to 0666. But in php-fpm 5.5.16, if no… Continue reading Ngnix 502 bad gateway error after a recent php-fpm update

Hands on GeoIP for Nginx on Fedora and CentOS

最近研究了一下如何根据用户的 IP 获得他的地理位置(我只关心 country 级,不关心 city 级),区别性地在 Ngnix 层面做一些限制性访问。这里用到 GeoIP 软件包和 MaxMind 的数据库。 在 Fedora 20 下,Nginx (目前 1.4.7) 依赖于 GeoIP。等于说,装好 Nginx,ngx_http_geoip_module 自然就启用了,只要在两个配置文件略作修改。 1. 在/etc/nginx/nginx.conf 里添加 2. 在 /etc/nginx/fastcgi_params 里添加 还有其他参数可用,请自行查手册。重启 Nginx 就可以使用 GeoIP 的信息了。 在 CentOS 6.5 下,事情要复杂一些。网上说 CentOS 下的 Nginx 同样依赖于 GeoIP,可在我这里事实并非如此。同样是最新的 1.4.7 版,Nginx for CentOS 是不带 ngx_http_geoip_module 编译的。即使我用 yum install GeoIP 安装了 GeoIP 软件包,但… Continue reading Hands on GeoIP for Nginx on Fedora and CentOS

SSL, Nginx and Magento

SSL, Nginx and Magento 这三件东西对我都不陌生。但三件全排在一起,着实挑战了我一下,发生错误是 secure page redirect loop。 原因是 $_SERVER[] 里缺少 HTTPS directive,需要在 fastcgi_params 里添加一行 fastcgi_param HTTPS on; 以 Magento 1.4.0.1 为例深究一下—— 在 $_SERVER[‘HTTPS’] 缺失时 app/code/core/Mage/Core/Model/Store.php 的 isCurrentlySecure() 返回值 false,所以 Magento 不停地重定向到 secure url 而不知道当前 url 已经是 secure 了。 我一直觉得用 php 探测当前 protocol 是否为 https 的算法比较“土”,曾经以为会有更好的探测办法,目前看来是没有。不光是没有,而且不可能有。就如有个小秘负责拆信,然后把有用的信纸交给 CEO 阅读,所以 CEO 不可能知道某封信是拿什么信封装的。

1and1 cloud server datasheet

从 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… Continue reading 1and1 cloud server datasheet

Nginx try_files syntax

今天在一台很久不用的服务器上测试 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 ~… Continue reading Nginx try_files syntax

Avoid PEM pass phrase

我在制作 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… Continue reading Avoid PEM pass phrase

SERVER_NAME vs HTTP_HOST

If server_name is something like “*.mydomain.com”, $server_name is exactly “*.mydomain.com”. If $server_name is passed on to fastcgi_param as SERVER_NAME, in the program, for example, php $_SERVER[‘SERVER_NAME’] will be exactly “*.mydomain.com”. However, _SERVER[“HTTP_HOST”] shows the value most of us would expect, i.e. the host name in the address bar. In Nginx, I have set up a… Continue reading SERVER_NAME vs HTTP_HOST

Gracefully restart Nginx

Follow these 3 steps to gracefully stop and start Nginx without losing any queries. It works like a charm. Test new configuration is correct nginx -t Find the Pid ps -ef | grep “nginx: master process” | grep -v “grep” | awk -F ‘ ‘ ‘{print $2}’ And kill it. Meanwhile, new configuration is already… Continue reading Gracefully restart Nginx

Speed bottleneck of the web server

非常典型的多层架构: 第一层 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 apache-2.0 Copyright 1996 Adam… Continue reading Speed bottleneck of the web server

php always takes apache as its session owner

今天用 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 很孤立 :(