低版本的mysql对嵌套查询支持的不够好,在mysql5中,如果要使用嵌套查询,如:
select count(*) from (select * from table where 1=1) as tmp_table;
这里要说明的是,对于子select需要as成一个临时表,否则就语法错误。
DevOps
低版本的mysql对嵌套查询支持的不够好,在mysql5中,如果要使用嵌套查询,如:
select count(*) from (select * from table where 1=1) as tmp_table;
这里要说明的是,对于子select需要as成一个临时表,否则就语法错误。
php memcache模块版本:
php –re memcache
Extension [ <persistent> extension #28 memcache version <no_version> ] {
今天使用memcache时,发现这么一个问题,使用同一个mc_client对象,依次连接多个memcached,第二个memcached确实进行了连接,但是getStats()时仍然使用的是第一个memcached的连接,如果要依次连接不同的memcached则需要重新new Memcache();
下面是测试脚本:
<?php
$arrHosts = array(‘10.10.10.11’=>‘11211’,‘10.10.10.12’=>‘11211’);
$mc = new Memcache();
echo serialize($mc)."\n";
foreach($arrHosts as $host=>$port) {
echo ‘start ‘.$host."\n";
$mc->connect($host,$port);
echo serialize($mc)."\n";
$stats = $mc->getStats();
echo $stats[‘pid’]."\n";
sleep(10);
}
?>
下面是strace的部分调用:观察发现两次connect使用的是不同的fd,而send却使用的是同一个fd
1054 fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 1), …}) = 0
1055 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, –1, 0) = 0xb7de9000
1056 write(1, "start 10.10.10.11\n", 18) = 18
1057 socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP) = 3
1058 close(3) = 0
1059 gettimeofday({1223351084, 844527}, NULL) = 0
1060 socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 3
1061 fcntl64(3, F_GETFL) = 0x2 (flags O_RDWR)
1062 fcntl64(3, F_SETFL, O_RDWR|O_NONBLOCK) = 0
1063 connect(3, {sa_family=AF_INET, sin_port=htons(11211), sin_addr=inet_addr("10.10.10.11")}, 16) = -1 EINPROGRESS (Operation now in progress)
1064 poll([{fd=3, events=POLLIN|POLLOUT|POLLERR|POLLHUP, revents=POLLOUT}], 1, 1000) = 1
1065 getsockopt(3, SOL_SOCKET, SO_ERROR, [0], [4]) = 0
1066 fcntl64(3, F_SETFL, O_RDWR) = 0
1067 send(3, "stats\r\n", 7, 0) = 7
1068 poll([{fd=3, events=POLLIN|POLLERR|POLLHUP, revents=POLLIN}], 1, 1000) = 1
1069 recv(3, "STAT pid 15727\r\nSTAT uptime 1293"…, 8192, 0) = 555
1070 write(1, "15727\n", 6) = 6
1071 write(1, "start 10.10.10.12\n", 18) = 18
1072 gettimeofday({1223351084, 845984}, NULL) = 0
1073 socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 4
1074 fcntl64(4, F_GETFL) = 0x2 (flags O_RDWR)
1075 fcntl64(4, F_SETFL, O_RDWR|O_NONBLOCK) = 0
1076 connect(4, {sa_family=AF_INET, sin_port=htons(11211), sin_addr=inet_addr("10.10.10.12")}, 16) = -1 EINPROGRESS (Operation now in progress)
1077 poll([{fd=4, events=POLLIN|POLLOUT|POLLERR|POLLHUP, revents=POLLOUT}], 1, 1000) = 1
1078 getsockopt(4, SOL_SOCKET, SO_ERROR, [0], [4]) = 0
1079 fcntl64(4, F_SETFL, O_RDWR) = 0
1080 send(3, "stats\r\n", 7, 0) = 7
1081 poll([{fd=3, events=POLLIN|POLLERR|POLLHUP, revents=POLLIN}], 1, 1000) = 1
1082 recv(3, "STAT pid 15727\r\nSTAT uptime 1293"…, 8192, 0) = 555
1083 write(1, "15727\n", 6) = 6
1084 close(3) = 0
1085 close(4) = 0
1086 close(0) = 0
下面的php memcache模块是没有问题的:
Extension [ <persistent> extension #26 memcache version 2.2.4 ] {
所以,使用php memcache模块时注意版本号哦,
代理ip
Variable Value
DOCUMENT_ROOT /data1/apache/sso/htdocs/
HTTP_ACCEPT */*
HTTP_ACCEPT_ENCODING gzip, deflate
HTTP_ACCEPT_LANGUAGE zh-cn
HTTP_CONNECTION close
HTTP_HOST 123.103.65.99
HTTP_MAX_FORWARDS 10
HTTP_UA_CPU x86
HTTP_USER_AGENT Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler )
HTTP_X_FORWARDED_FOR 10.218.19.130 // 最终的客户端ip
HTTP_X_FORWARDED_HOST ljj.com
HTTP_X_FORWARDED_SERVER ljj.com
HTTP_X_REAL_IP 10.218.19.94 // 代理的ip
PATH /usr/kerberos/sbin:/usr/kerberos/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/home/junjie2/bin
REMOTE_ADDR 61.135.152.194 // 代理的出口ip
REMOTE_PORT 43217
SERVER_ADDR 123.103.65.99 // 最终访问的apache服务器的ip
SERVER_PORT 80
SERVER_SIGNATURE <ADDRESS>Apache/1.3.33 Server at login.sina.com.cn:80 Port 80</ADDRESS>
SERVER_SOFTWARE Apache/1.3.33 (Unix) PHP/5.1.5 mod_ssl/2.8.24 OpenSSL/0.9.8d
GATEWAY_INTERFACE CGI/1.1
SERVER_PROTOCOL HTTP/1.0
REQUEST_METHOD GET
QUERY_STRING no value
REQUEST_URI /sso/phpinfo.php
SCRIPT_NAME /sso/phpinfo.php
下面解释一下上面ip的含义:
10.218.19.130 -> 10.218.19.94 -> 123.103.65.99
10.218.19.120 通过代理服务器10.218.19.94访问123.103.65.99 ,服务器正真做tcp连接的server是61.135.152.194,是代理服务器的出口ip,也只有这一个ip是无法伪造的,其它的以HTTP开头的ip都是从http协议头里取的,都是可以伪造的。客户端到服务器中间经过了两次代理 访问的是 10.218.19.94:80 -> 10.218.94.80:81 -> 123.103.65.99
今天想了解一下PHP的加密函数,但是需要mcrypt模块,就自己编译一遍,没想到如此复杂,所以记录下来,也好和大家分享
PHP模块mcrypt安装步骤:
1. 确认是否已安装libmcrypt
ldconfig -p | grep libmcrypt
如果没有安装:
下载地址:http://sourceforge.net/projects/mcrypt
可以同时下载libmcrypt 和 mcrypt
先安装libmcrypt, 默认安装目录为 /usr/local , PHP 手册建议–disable-posix-threads ,不知何故
./configure && make && make install
ldconfig
再安装mcrypt, 默认安装目录为 /usr/local
./configure –with-libmcrypt-prefix=/usr/local
2. 确认是否已安装mhash
ldconfig -p | grep mhash
如果没有安装:
下载地址:http://mhash.sourceforge.net/
./configure && make && make install
ldconfig
3. 现在可以安装mcrypt模块了
cd php-x.x.x/ext/mcrypt
./configure –with-mcrypt=/usr/local/ && make && make install
4. 修改php.ini 就可以了
测试一下:
<?php
// Designate string to be encrypted
$string = "Applied Cryptography, by Bruce Schneier, is a wonderful cryptography reference.";
$key = "Four score and twenty years ago"; // Encryption/decryption key
$cipher_alg = MCRYPT_TWOFISH;
$iv=mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,MCRYPT_MODE_ECB), MCRYPT_RAND);
echo "Original string: $string \n";
$encrypted_string = mcrypt_encrypt($cipher_alg, $key, $string, MCRYPT_MODE_CBC, $iv);
echo "Encrypted string: ".bin2hex($encrypted_string)."\n";
$decrypted_string = mcrypt_decrypt($cipher_alg, $key,
$encrypted_string, MCRYPT_MODE_CBC, $iv);
echo "Decrypted string: $decrypted_string\n";
?>
mysql_query($sql,[$link]); 其中第二个参数是缺省的,但是使用中发现最好还是不要缺省;如果php.ini 里配置了正确的mysql的主机名、用户名、密码的话,你不需要mysql_connect(); 直接执行如下代码都是可以的:
<?php
ini_set(‘mysql.trace_mode’,true);
$result = mysql_query(‘select 1+1’);
print_r(mysql_fetch_assoc($result));
mysql_free_result($result);
?>
这样一来,如果mysql_query() 不使用第二个参数,我们的php.ini 有没有配置正确的mysql链接信息,当我们使用mysql_connect() 产生的连接丢失后,我们将会发现mysql却在试图连接一个我们不知道的地方(就是php.ini里配置的地方)
小提示:mysql_query() 的结果集是需要用mysql_free_result释放的,否则将产生一个警告(Warning: Unknown: 1 result set(s) not freed. Use mysql_free_result to free result sets which were requested using mysql_query() in Unknown on line 0)只是这个警告在mysql.trace_mode 为off时时不显示的,所以我们可能会很容易忽略mysql_free_result的使用
Http Keep-Alive seems to be massively misunderstood. Here’s a short description of how it works, under both 1.0 and 1.1, with some added information about how Java handles it.
Http operates on what is called a request-response paradigm. This means that a _client_ generates a request for information, and passes it to the server, which answers it. In the original implementation of HTTP, each request created a new socket connection to the server, sent the request, then read from that connection to get the response.
This approach had one big advantage – it was simple. Simple to describe, simple to understand, and simple to code. It also had one big disadvantage – it was slow. So, keep-alive connections were invented for HTTP.
HTTP/1.0
Under HTTP 1.0, there is no official specification for how keepalive operates. It was, in essence, tacked on to an existing protocol. If the browser supports keep-alive, it adds an additional header to the request:
Connection: Keep-Alive
Then, when the server receives this request and generates a response, it also adds a header to the response:
Connection: Keep-Alive
Following this, the connection is NOT dropped, but is instead kept open. When the client sends another request, it uses the same connection. This will continue until either the client or the server decides that the conversation is over, and one of them drops the connection.
HTTP/1.1
Under HTTP 1.1, the official keepalive method is different. All connections are kept alive, unless stated otherwise with the following header:
Connection: close
The Connection: Keep-Alive header no longer has any meaning because of this.
Additionally, an optional Keep-Alive: header is described, but is so underspecified as to be meaningless. Avoid it.
Not reliable
HTTP is a stateless protocol – this means that every request is independent of every other. Keep alive doesn抰 change that. Additionally, there is no guarantee that the client or the server will keep the connection open. Even in 1.1, all that is promised is that you will probably get a notice that the connection is being closed. So keepalive is something you should not write your application to rely upon.
KeepAlive and POST
The HTTP 1.1 spec states that following the body of a POST, there are to be no additional characters. It also states that "certain" browsers may not follow this spec, putting a CRLF after the body of the POST. Mmm-hmm. As near as I can tell, most browsers follow a POSTed body with a CRLF. There are two ways of dealing with this: Disallow keepalive in the context of a POST request, or ignore CRLF on a line by itself. Most servers deal with this in the latter way, but there’s no way to know how a server will handle it without testing.
Java abstraction – client side
On the client side, Java abstracts the notion of a keepalive request away from the programmer. The HttpURLConnection class implements keepalive automatically, without intervention being either required or possible on the part of the programmer. It does this through an internal cache of client connections, which is maintained as one of the implementation details of the class. Incidentally, this means that keepalive is an implementation detail of the Javasoft class library, which may or may not be available in other class libraries.
Java abstraction – server side
On the server side, Java again abstracts out the notion of a keepalive request. The HttpServlet, HttpServletRequest, and HtppServletResponse classes implement keepalive automatically. In this case, however, some intervention is possible. As mentioned in the KeepAliveServlet that comes with the JavaWebServer, the actual keepalive behavior that occurs depends on two factors: setting content length, and size of output. If content length is set as part of the response, keepalive will be used if the client supports it. If content length is not set, the servlet implementation will attempt to buffer the response to determine it’s content length. If the buffering is successful, keepalive will be used. In the Javasoft implementation, a 4k buffer is used. This means that if the content length is not set, and the amount of data returned is over 4k, keepalive will not be used. As with HttpURLConnection, this is an implementation detail of the Javasoft class library, which may or may not be available in other class libraries.
ps -ef|grep httpd|wc -l的值小。
netstat -an | grep :80 |wc -l的值大。
摘自:http://hi.baidu.com/toughguy/blog/item/626410fb51abfa176d22eb38.html
最近几年,市场上出现了很多有趣的 Web 服务器实现,包括 lighthttpd、litespeed 和 mongrel 等。这些 Web 服务器都宣称结合了性能、易管理性、可移植性、安全性和其他相关价值。下面的工程研究将调查轻量级 Web 服务器,以帮助您选择最可能满足下一个项目的技术需求的 Web 服务器。
“轻量级” Web 服务器,例如 lighthttpd
、 litespeed
和 mongrel
,可以为项目带来很多的好处。本文调查这种可能性,并展示这些 Web 服务器的适用性。
第一个重要的方面是清楚地理解所调查的领域(请参阅 参考资料,以了解更详细的信息)。终端用户在 Internet 上的基本动作就是 “进入一个 Web 页面”。从大处讲,这牵涉到两个应用程序之间的协作:
所有 Web 用户直接与浏览器交互,因此他们的选择和分析相应地有些狂热。而服务器只对站点的技术人员可见。根据 Netcraft 最近的调查,虽然存在很多不同的 Web 服务器,但是其中两种 Web 服务器就占据了 90% 的份额,这两种 Web 服务器是 Apache 和 Internet Information Server (IIS)。它们都是经过高度锤炼的产品,并且声称不仅具有广泛的内在技术特性,而且有很多配套的书籍、增件、顾问、提供商等。那么,它们是否还有值得改造的地方呢?
答案是肯定的。评价一个 Web 服务器的重要指标有:
Apache 和 IIS 不能同时在那么多的标准方面做到最好。理论上讲,显然那些定向的产品至少能在以上的一至两个方面超越市场领头产品。
关于轻量级 Web 服务器的一件有趣的、值得调查的事情是,它们之间的竞争远远不止是理论上的:仔细研究表明,它们有很多 东西可以提供,并且即使在很多常见的情况下,它们相对于 Apache 和 IIS 也坚持了自己的风格。虽然可以合理地认为市场领头产品已经经过了小心的优化,从而能够有效地在性能(举个例子)方面避免被击败,但是很多小型的竞争对手因为只提供简单的静态 Web 页面服务,速度反而更快。当使用这些 Web 服务器运行测试时,您会感觉好像是在赛道上驾驶一辆 go-kart 小车,不知不觉竟然超过了 Porsche 和 Viper 车。这还不是全部:有时候,轻量级 Web 服务器可作为那些大哥级服务器的有效补充,而不只是与它们竞争。即使您知道自己将使用 Apache,有时候通过将它与一个轻量级伙伴搭档,反而可以最大限度地利用它。最好的解决方案有时候需要两个或更多 Web 服务器的协作。
本调查中重点关注的 “轻巧性” 实际上是一种主观质量,就像 “艺术” 或 “风格”。它通常意味着简单、易于安装、流线化、要求低和健壮 —— 比 Apache 和 IIS 更小、更简单,当然,在试图满足大量市场的过程中,它们已经变得异常复杂。出于这个目的,虽然 Java Web Server、AOLserver 和 Zeus 拥有迷人的可移植性和性能优势,但是它们的复杂性和大小使其不得不被拒之门外。
轻量级 Web 服务器可以适用于市场领头产品和其他 “重量级” 服务器无法胜任的情况。例如,整个服务器可以打包在一个文件中。这意味着开发人员可以方便地携带生产环境所需的所有工具。即使在生产服务器上运行的是 Apache,也仍然可以在宾馆的房间里,借助只需数秒钟就可以安装完毕的轻量级 Web 服务器以尝试新想法。而且,由于轻量级 Web 服务器要求很低,因此可以在那些无法负担 IIS 的主机上顺畅地运行。
![]() |
|
小的、轻量级的 Web 服务器还可以在小功率的主机上良好地运行。在我们的公司(Phaseit —— 见 侧栏)中,我们在远程的、条件欠佳或配置较低的环境中的工业计算机上运行专用的 硬件。在这些情况下,能够通过一个对处理能力或磁盘空间要求很低的应用程序来提供 Web 页面是一个很大的优势。这意味着我们的机器可以避免 Apache 的开发和处理能力所带来的开销,构建基于 Web 的管理控制台。
从某种程度上讲,几乎所有轻量级 Web 服务器都是开放源码的。如果我们需要某一款 Web 服务器所特有的行为,那么下面概述的一些 Web 服务器都非常小巧,易于理解,也易于增强,只有两个例外。这些 Web 服务器为嵌入 Web 服务的项目提供极好的原始材料,不管这些 Web 服务是在特殊的硬件中,还是在为在通用计算机上运行而设计的特定应用程序中。它们还广泛用于具有传统外观的 Web 站点:
下面的例子说明了开发人员使用轻量级服务器的轻巧性:在我们公司,我们采用专门的硬件提供办公室电话解决方案。它基于定制的、以传统的 Linux® 应用程序的形式运行的软件。只需一个附加文件和一点 init.d
配置,很容易添加一个强大的 “Web 控制台”,该 Web 控制台能提供硬件和软件的管理界面。 终端用户可以从任何浏览器中监视和配置他们的计算机,而不必安排专门的硬件连接或解决使用 “垂直” 硬件时常见的其他复杂性。
面向服务架构(SOA)被认为难以使用。在我们的经验中,SOA 至少有一部分这方面的缺点阻碍了 Web 服务的使用。我们利用轻量级 Web 服务来设置快速的 SOA,以进行演示。
轻量级服务器甚至可以用于生产数据中心,包括前面列出的 high-profile 站点。性能非常高的站点会将操作分开,从而最大限度地利用缓存、代理等技术。例如,一个基于 Apache 的站点可能采用一种这样的架构:通过小型的 Web 服务器从专用的文件系统提供缓慢变化的图片。终端用户看到的结果实际上是 Apache 和一个或多个辅助 Web 服务器通过协作得到的输出,它们各自担任自己擅长的角色。这样的安排可以以非常低的计算成本提供非常 快的结果。
虽然轻量级 Web 服务器有很多共同之处,但是各有各的不同。大多数轻量级 Web 服务器是用 C 编写的,但是实践证明,有些其他实现语言也可以成功地用于实现服务器,对此我已经做了实验,这些语言包括 Erlang、Java、Lisp、Lua、Perl、Python 和 Tcl。如果其中有您喜欢的语言,那么也许可以找到适合您的 Web 服务器。
由于很多特定的原因,您可能会将目光投向某种 “不常见” 的语言:
Athana 可以作为这些主题的例子。它是用 Python 编写的 Web 服务器。它支持 HTTP 多部分(上传)、会话、 cookie 等。从 0.2.1 版开始,Athana 一直被编写在一个单独的、精心组织的源文件中。
如前所述,不同的轻量级 Web 服务器有着不同的优点,它们或多或少独立于编程语言。所有轻量级 Web 服务器都比 Apache 更小、更易于配置。与 Apache 相比,有些轻量级 Web 服务器更快,有些则快得多。有些则强调安全性、重负载下的从容性、可扩展性或者内存占有量。在任何情况下,都可以以一种不适用于 Apache 的方式彻底地理解这些服务器。
哪些特定的产品使这些可能性成为现实?即使只留意 “轻量级” 服务器,面对的也是一个很大的难于管理的产品集合。不过可以将它们按子类来划分:超轻型、关注安全型、支持特定语言型等等。
我特别喜欢其中的超轻型 Web 服务器,它们比 Apache 小得多。如此小的应用程序可以直接记住,系统地、严密地加以考虑,以证明 它们的安全性或可伸缩性。小型 Web 服务器包括:
inetd
提供页面。
glibc
。而且它也非常快。
体积小并不妨碍这些服务器被正式使用。例如,fnord 可以处理数千个同时进行的连接。
也许轻量级作为一个类别最令人印象深刻的成就是高性能服务器:
有些 Web 服务器被实现为类或库,以便嵌入 到较大的应用程序中。 在这些 Web 服务器当中,我发现特别有趣的有:
Python 是几种适合不寻常环境的 Web 服务器的实现语言,这些 Web 服务器包括:
还有其他一些用 Perl 和其他不出名的语言编写的轻量级 Web 服务器:
有时候您可能需要其他一些用 C 编写的、具有不常见的次要优势的轻量级 Web 服务器:
chroot
、 Basic Authentication 等。 Web 服务器远远不止是 Apache 和 IIS 的天下。您可以发现很多其他的 Web 服务器,它们很小,易于理解,但是又足够快,可以被正式使用。这样的 Web 服务器可以很好地加快您的下一个项目。
学习
获得产品和技术
Cameron Laird 长期为 developerWorks 撰稿,并且曾担任专栏作家。他经常撰写关于能加快其雇主的应用程序开发的开放源代码项目的文章,着重关注可靠性和安全性。