19 08年4月

Apache的Mod_rewrite 例子


防止GetRight和Curl来抓取网內的数据的代码

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} ^curl [OR]
RewriteCond %{HTTP_USER_AGENT} ^GetRight
RewriteRule ^.* - [F]

阅读全部»

贝贝爸 发表在 技术文章 | 1个评论

25 07年5月

apache child pid exit signal File size limit exceeded error

关键字:, ,

一个正常使用的网站,LAMP架构,突然打不开了,但是另外一个二级域名的网站,在同一台服务器上可以打开,静态文件也可以打开,因为出故障前有段时间程序没有更新,程序错误的可能性不大。
telnet 到网站的80端口,也可以打开,现象很奇怪。
重起了apache,没用,mysql用命令行也可以使用,没问题,一时没了方向。
于是tail了apache的log,一个是正常的web log,一个是error_log。
观察之后,发现了异常现象,apache的error_log一直输出错误信息如下:

[Thu May 24 23:11:27 2007] [notice] child pid 23406 exit signal File size limit exceeded (25)
[Thu May 24 23:12:01 2007] [notice] child pid 23544 exit signal File size limit exceeded (25)
[Thu May 24 23:12:47 2007] [notice] child pid 23633 exit signal File size limit exceeded (25)
[Thu May 24 23:12:53 2007] [notice] child pid 22799 exit signal File size limit exceeded (25)

说有文件大小超过了限制。
google了一下,说是超过了apache 2G的文件限制,试用find命令在系统里面查找超大的文件

find / -size +1000000k

查找到2个文件,一个是

/xxx/xxx/page_parse_time.log
/xxx/yyy/support-popbytes_log

第一个文件page_parse_time.log的大小在2G左右,压缩备份后,

> /xxx/xxx/page_parse_time.log

,问题解决。
page_parse_time.log的文件内容是程序记录sql query的log以及执行时间的log,每个页面请求都会写上一些数据,这么超过2G也不奇怪了。

上次还遇到过一次mysql数据条数过多,的问题
报错信息如下:

LOGSQL Insert Failed: insert into adodb_logsql (created,sql0,sql1,params,tracer,timer) values( NOW(),?,?,?,?,?) The table 'adodb_logsql' is full

经查

mysql> select count(*) from adodb_logsql ;
 +----------+
|
count(*) |
 +----------+
|
33127244 |
+----------+

3千万,,,可能是有点大了,最后truncate了这个表,问题解决
可是最后查mysql的文档,关于Scalability and Limits部分没有说mysql最多可以存多少条记录,估计跟表的类型也相关。原文说的是
Handles large databases. We use MySQL Server with databases that contain 50 million records. We also know of users who use MySQL Server with 60,000 tables and about 5,000,000,000 rows.
只是不知道它说的50 million是一个database里面的所有记录还是所有表的记录加起来。后边的应该是所有60,000个表加起来的数据量。
————————
结论,必要的日志是需要的,但是要控制大小,有循环机制,或者有监控机制,切记,切记。

贝贝爸 发表在 原创技术文章 | 等您写评论

15 07年5月

如何做301转向

关键字:,

301 redirect: 301代表永久性转移(Permanently Moved),301重定向是网页更改地址后对搜索引擎友好(SEO)的最好方法,只要不是暂时搬移的情况,都建议使用301来做转址。

现在知道的有2类,一类是在web服务软件上做,还有就是在程序里面做

先说在web服务软件

如果是iis:
打开internet信息服务管理器,在要重定向的网页或目录上按右键

  1. 选中“重定向到URL”
  2. 在对话框中输入目标页面的地址
  3. 选中“资源的永久重定向”
  4. 点击“应用

如果是apache方法多一些,也方便一些
如果要把访问www.juhui.com的请求全部301到www.juyimeng.com

<VirtualHost www.juhui.com>
Redirect 301 / http://www.juyimeng.com/
CustomLog logs/redir "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
</VirtualHost>

上面的
Redirect 301 / http://www.juyimeng.com/
可以写为
Redirect permanent / http://www.juyimeng.com/

也可以用apache的.htaccess,怎么使.htaccess起效,点这里
.htaccess文件的内容

RewriteEngine on
rewriterule abc\.html /index\.html [R=301]

其总用是把abc.html301到index.html

用程序来做

用PHP来做301重定向

<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.test.com" );
?>

用JSP (Java)来做301重定向

<%
response.setStatus(301);
response.setHeader( "Location", "http://www.test.com/" );
response.setHeader( "Connection", "close" );
%>

用CGI PERL 来做301重定向

$q = new CGI;
print $q->redirect("http://www.test.com/");

用ASP来做301重定向

<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently";
Response.AddHeader("Location","http://www.test.com/");
%>

用ColdFusion 来做301重定向

<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="http://www.test.com">

用ASP .NET Redirect来做301重定向

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.test.com");
}
</script>

用Ruby on Rails Redirect来做301重定向

def old_action
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.test.com/"
end

html的meta refresh和javascript也可以转向

<meta http-equiv="Refresh" content="0; url=http://test.com/page.html">
 
<script>
window.location="http://www.test.com/test.html";
</script>

但是上述的方法不推荐

如果要Redirect的源url是html这种结尾也可以,改apache的配置,把html扩展名的文件当Php或者其他脚本来处理就可以了
AddType application/x-httpd-php .html
AddType application/x-httpd-php .htm

上述改造是否成功,可以用httpwatch来查看

贝贝爸 发表在 技术文章 | 等您写评论

14 07年5月

apache tips

关键字:

apache如何多个域名指向一个site?
用ServerAlias

<VirtualHost www.juyimeng.com>
ServerName www.juyimeng.com
ServerAdmin juhui@test.com
ServerAlias www.abc.com
ServerAlias www.def.com
DocumentRoot /usr/juhui/htdocs/
ErrorLog logs/juyimenng.error_log
</VirtualHost>

如何不用.htaccess这个文件名字?

AccessFileName .config
如何让.htaccess文件起作用?
如果.htaccess文件不起作用,请检查apache的配置文件里面的
AllowOverride 设置
简单的用
AllowOverride All
最少要是
AllowOverride FileInfo

通过在客户端缓存css.js,图片等文件来加速网站浏览速度

ExpiresActive On
ExpiresByType image/gif A259200
ExpiresByType image/png A259200
ExpiresByType image/jpg A259200
ExpiresByType image/jpeg A259200
ExpiresByType text/css A259200
ExpiresByType   application/x-javascript A259200

259200是3天的秒数,A的意思是从最后一个访问开始,缓存三天。

推荐一个apache+mysql+php+java等的安装文件包http://www.apachefriends.org/
方便得很,直接集成多种服务,包括
Apache、MySQL、PHP + PEAR、Perl、mod_php、mod_perl、mod_ssl、OpenSSL、phpMyAdmin、Webalizer、Mercury Mail Transport System for Win32 and NetWare Systems v3.32、JpGraph、FileZilla FTP Server、mcrypt、eAccelerator、SQLite 和 WEB-DAV + mod_auth_mysql
等,你要用的有,想到的有,没想到的也有好,测试,本地开发环境使用,谁用谁知道阿。

贝贝爸 发表在 技术文章 | 1个评论

25 07年4月

subversion “Propfind 302 found” error

关键字:, , ,

svn服务出了问题,可以checkout,已经存在的文件,可以修改,提交,但是新加的文件add后不能commit。
报错说什么”Propfind xxx 302 found”

Adding: D:\workspace\test\New Text Document.txt 
Error: Commit failed (details follow): 
Error: PROPFIND request failed on '/svn/test/New%20Text%20Document.txt' 
Error: PROPFIND of '/svn/test/New%20Text%20Document.txt': 302 Found (http://svn.test.com)

google了一下,说是启用了ErrorDocument 404 handler 就会遇到这个问题,需要禁用Subversion目录特殊的404 error handling,
解决方法是添加

ErrorDocument 404 default

到svn的Location立面

改了之后,httpd.conf里面的svn配置大概如下:

LoadModule dav_svn_module     modules/mod_dav_svn.so
LoadModule authz_svn_module   modules/mod_authz_svn.so
<Location /svn>                                 #设置访问路径
# Uncomment this to enable the repository,
DAV svn
# Set this to the path to your repository
SVNParentPath /data/svn_repository/
# The following allows for basic http authentication.  Basic authentication
# should not be considered secure for any particularly rigorous definition of
# secure.
# to create a passwd file                     #按下面的步骤创建Apache用户验证文件
# # rm -f /etc/apache2/dav_svn.passwd
# # htpasswd2 -c /etc/apache2/dav_svn.passwd dwhedon
# New password:
# Re-type new password:
# Adding password for user dwhedon
#

# Uncomment the following 3 lines to enable Basic Authentication
AuthType Basic
AuthName "Subversion Repository"
AuthUserFile conf/extra/svnpasswd.file
# Uncomment the following line to enable Authz Authentication
AuthzSVNAccessFile conf/extra/dav_svn.authz
# The following three lines allow anonymous read, but make
# committers authenticate themselves.
#<LimitExcept GET PROPFIND OPTIONS REPORT>   
#允许匿名访问,不允许Commit,不能与AuthzSVNAccessFile同时使用
Require valid-user
#</LimitExcept>
ErrorDocument 404 default
</Location>

贝贝爸 发表在 原创技术文章 | 等您写评论

10 07年4月

用netstat和任务管理器查找占用端口的进程


昨天要用xampp,启动的时候报错,说是443端口被占用了,瞅了半天开的应用程序,觉得奇怪,那个程序会用443呢?用进程管理器一阵乱杀,,,,结果,没效果。本想重起,又一想,这样不解决根本问题,于是google了一把,找到了解决方法。
开始,运行,cmd
运行
netstat -ano
说明
a:显示所有连接和监听的端口
n:用ip地址的形式显示地址和端口
o:显示和连接想关的进程id。
用这个方法查出占用端口的进程id
然后按ctrl+shift+esc,打开windows task manager 任务管理器,切换到进程(processes),如果没有pid,进程id的column点击查看下面的select comumns… 选中PID(Process Identifier),确认后,按照pid排序,查找相应netstat命令查出来的pid对应的应用程序。

我这里查出来,居然是qq,杀掉后问题解决,不知道这个鬼东西为啥要用443端口。
以前还发现过skype用80端口的事情,如果不用apache等软件还好,不然还真麻烦,解决方法是skype的tools/options…/connection
去掉use port 80 and 443 as alternatives for incoming connections

查了一下,linux系统中,netstat用法基本相同,只需将o换成p,另外要用root帐号来查,windows应该也要有administrator权限,只不过用的帐号式管理员权限。

-p, --programs             display PID/Program name for sockets
[root@call root]# netstat -anp

查出来后,该kill还是killall就看你咯

贝贝爸 发表在 原创技术文章 | 等您写评论

22 07年3月

随机变化的.jpg图片

关键字:, ,

实现的原理很简单,就是apache的url_rewrite功能+php程序
url_rewrite代码

  1. [test@test htdocs]$ cat .htaccess
  2. # BEGIN WordPress
  3. <IfModule mod_rewrite.c>
  4. RewriteEngine On
  5. RewriteRule ^images/logo\.jpg$ /test/randomimg.php [L]
  6. </IfModule>

php代码

  1. <?php
  2. /*
  3. * Created on 2007-3-20
  4. *
  5. * To change the template for this generated file go to
  6. * Window - Preferences - PHPeclipse - PHP - Code Templates
  7. */
  8. header("Content-Type: image/jpeg");
  9. $dir    = './images/';
  10.  
  11. $imgarr=array();
  12. if ($handle = opendir($dir)) {
  13.     while (false !== ($file = readdir($handle))) {
  14.         if (preg_match ("/(.*?)\.jpg$/i", $file) ) {
  15.                 array_push($imgarr,$dir.$file);
  16.         }
  17.     }
  18.     closedir($handle);
  19. }
  20. //srand ((float) microtime() * 10000000);
  21. readfile($imgarr[array_rand ($imgarr)]);
  22. ?>

只要把把要显示的图片放到
$dir目录里就可以了:)
效果点击这里,或者刷新本页面,看右上角的图片。

贝贝爸 发表在 原创技术文章 | 2个评论

17 07年3月

apache url_rewrite的一个规则

关键字:,

实现的目的就是:
访问http://www.domin.com/user/*.*时
全部rewrite到对应的http://www.domin.com/*.*
当然包含子目录
譬如对http://www.domin.com/user/news/A01.htm时
会对应到http://www.domin.com/news/A01.htm

  1. Options +FollowSymLinks
  2. RewriteEngine on
  3. RewriteRule user/(.*)$ $1

放在“/”测试通过

贝贝爸 发表在 原创技术文章 | 等您写评论