robots.txt 404错误的解决

最近查看网站日志,在404错误的条目里,出现最多的就是robots.txt这个文件了。原本以为是我真没有这个文件,但一检查发出根目录下是有这个文件的,但为什么还是显示page not found呢?然后我再以为是Drupal的问题,但是在网上搜索了好久,也没听说这个跟Drupal有什么关系。最后才突然想到可能是Nginx服务配置的问题,一查配置文件,果然是我没有加上txt文件支持的原因。

知道原因就好办了,在配置文件里加入下面几句就可以了:

  1. location ~ (^\/|\.html|\.css|\.jpg|favicon\.ico|robots\.txt|\.png|\.js|\.gif)$ {
  2. root /var/www/hosts/nofool.info/htdocs/;
  3. access_log off;
  4. expires 30d;
  5. }

上面的例子是为Nginx加入一些静态文件如html、css、jpg、png、js、gif的支持,txt文件则只支持robots.txt,ico文件则只支持favicon.ico

阅读全文

自动把网址转换超链接代码(PHP 版)

有时候需要在提交内容的时候,自动把一些网址自动加上超链接。下面是一个PHP的样例,支持转换普通的链接、FTP地址、E-mail地址、Twitter类的链接,里面的正则表达式稍作修改也可以用于其它的语言。

<?php
function add_links($text) {
    
$textpreg_replace("/(^|[\s ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is""$1$2<a href=\"$3\" >$3</a>"$text); /* http[s]//** */
    
$textpreg_replace("/(^|[\s ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is""$1$2<a href=\"http://$3\" >$3</a>"$text); /* ftp://** */
    
$textpreg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i""$1<a href=\"mailto:$2@$3\">$2@$3</a>"$text); /* E-mail */
    
$textpreg_replace("/@(\w+)/"'<a href="http://www.twitter.com/$1" target="_blank">@$1</a>'$text); /* @twitter 用户 */
    
$textpreg_replace("/\#(\w+)/"'<a href="http://search.twitter.com/search?q=$1" target="_blank">#$1</a>',$text); /* #twitter 搜索 */
    
return $text;
}
?>

阅读全文

Nginx 通过FCGI支持Perl 脚本

折腾了几天,总算让Nginx也支持Perl程序了。因为之前已经配置好PHP的环境了(请看这里),所以这一次只是想在原来的的基础上增加Perl的支持而已,所以并不打算需要太复杂的设置。在网上找了很久,都没有找到比较好的教程,很多都需要自己写一个接口程序和启动脚本,并且很多都是不太符合Gentoo启动脚本的格式。最后没办法,只有自己慢慢的摸索了,参照PHP的相关设置,绕了不少的弯路,才总算成功了。趁自己还没有忘记之前,把过程记录下来吧。

首先,需要的是一个Perl的接口程序,找了好久我才找到了fcgiwrap这个程序,还好,Gentoo的Portage里也有,可以直接emerge它:

  1. $ eix fcgiwrap
  2. [*] www-misc/fcgiwrap
  3. Available versions: (~)1.0.2-r1 (**)9999
  4. Installed versions: 1.0.2-r1(2248472010年08月05日)
  5. Homepage: ht tp://nginx.localdomain.pl/wiki/FcgiWrap
  6. Description: Simple FastCGI wrapper for CGI scripts (CGI support for nginx)
  7. $ sudo emerge fcgiwrapp

创建一个spawn-fcgi启动脚本的链接

  1. $ sudo ln -s /etc/init.d/spawn-fcgi /etc/init.d/spawn-fcgi.pl

spawn-fcgi的配置文件

  1. $ cat /etc/conf.d/spawn-fcgi.pl
  2. # Copyright 1999-2009 Gentoo Foundation
  3. # Distributed under the terms of the GNU General Public License v2
  4. # $Header: /var/cvsroot/gentoo-x86/www-servers/spawn-fcgi/files/spawn-fcgi.confd,v 1.6 2009/09/28 08:38:02 bangert Exp $
  5.  
  6. # DO NOT MODIFY THIS FILE DIRECTLY! CREATE A COPY AND MODIFY THAT INSTEAD!
  7.  
  8. # The FCGI process can be made available through a filesystem socket or
  9. # through a inet socket. One and only one of the two types must be choosen.
  10. # Default is the inet socket.
  11.  
  12. # The filename specified by
  13. # FCGI_SOCKET will be suffixed with a number for each child process, for
  14. # example, fcgi.socket-1.
  15. # Leave empty to use an IP socket (default). See below. Enabling this,
  16. # disables the IP socket.
  17. #
  18. FCGI_SOCKET=/var/run/fcgiwrap.sock
  19.  
  20. # When using FCGI_PORT, connections will only be accepted from the following
  21. # address. The default is 127.0.0.1. Use 0.0.0.0 to bind to all addresses.
  22. #
  23. #FCGI_ADDRESS=127.0.0.1
  24.  
  25. # The port specified by FCGI_PORT is the port used
  26. # by the first child process. If this is set to 1234 then subsequent child
  27. # processes will use 1235, 1236, etc.
  28. #
  29. FCGI_PORT=
  30.  
  31. # The path to your FastCGI application. These sometimes carry the .fcgi
  32. # extension but not always. For PHP, you should usually point this to
  33. # /usr/bin/php-cgi.
  34. #
  35. FCGI_PROGRAM=/usr/sbin/fcgiwrap
  36.  
  37. # The number of child processes to spawn. The default is 1.
  38. #
  39. #FCGI_CHILDREN=1
  40.  
  41. # If you want to run your application inside a chroot then specify the
  42. # directory here. Leave this blank otherwise.
  43. #
  44. #FCGI_CHROOT=
  45.  
  46. # If you want to run your application from a specific directiory specify
  47. # it here. Leave this blank otherwise.
  48. #
  49. #FCGI_CHDIR=
  50.  
  51. # The user and group to run your application as. If you do not specify these,
  52. # the application will be run as root:root.
  53. #
  54. FCGI_USER=nginx
  55. FCGI_GROUP=nginx
  56.  
  57. # Additional options you might want to pass to spawn-fcgi
  58. #
  59. FCGI_EXTRA_OPTIONS="-M 0700"
  60.  
  61. # If your application requires additional environment variables, you may
  62. # specify them here. See PHP example below.
  63. #
  64. ALLOWED_ENV="PATH"

Nginx配置文件

  1. $ cat /etc/nginx/vhosts/example.com.conf
  2. server {
  3. listen 127.0.0.1;
  4. server_name *.example.com example.com;
  5. access_log /var/log/nginx/localhost.access_log main;
  6. error_log /var/log/nginx/localhost.error_log info;
  7. root /var/www/example.com;
  8.  
  9. location ~ .*.php$ {
  10. include /etc/nginx/fastcgi.conf;
  11. fastcgi_pass 127.0.0.1:1234;
  12. fastcgi_param SCRIPT_FILENAME /var/www/example.com/$fastcgi_script_name;
  13. fastcgi_index index.php;
  14. }
  15.  
  16. location ~ .*.pl$ {
  17. include /etc/nginx/fastcgi_params;
  18. fastcgi_pass unix:/var/run/fcgiwrap.sock-1;
  19. fastcgi_param SCRIPT_FILENAME /var/www/example.com/$fastcgi_script_name;
  20. fastcgi_index index.pl;
  21. }

这里要注意的是,网上很多教程和fcgiwrap的手册(man page)都是把fastcgi_pass设置成 fastcgi_pass unix:/var/run/fcgiwrap.sock,但是这样是不行的,查看Nginx的Log会发现有提示如下的错误:
connect() to unix:/var/run/fcgiwrap.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: *.example.com, request: "GET /test.pl HTTP/1.1", upstream: "fastcgi://unix:/var/run/fcgiwrap.sock:", host: "example.com"

原因嘛,/etc/conf.d/spawn-fcgi.pl 这个配置文件里关于FCGI_SOCKET的注释有写到,FCGI_SOCKET会为每个子进程添加一个数字后缀,所以需要把这个后缀也要添加上去,例如:fastcgi_pass unix:/var/run/fcgiwrap.sock-1; 否则,就会出现文件找不到的错误,我在这里花了不少的时间才明白这个事情。

最后,启动相关服务:

  1. $ sudo /etc/init.d/spawn-fcgi.pl start
  2. $ sudo /etc/init.d/nginx start

如果还是出现502错语并且Nginx Log 里有这样的出错提示:upstream closed prematurely FastCGI stdout while reading response header from upstream, client: 127.0.0.1, server: *.example.com, request: "GET /test.pl HTTP/1.1", upstream: "fastcgi://unix:/var/run/fcgiwrap.sock-1:", host: "example.com",那就要检查一下你的Perl文件是否有读取跟执行的权限了(755),在这里又花了我很长的时间才明白Perl脚本是还需要执行的权限的,这一点跟PHP不同。

阅读全文

Jquery 取消链接的默认行为

在写JS脚本,特别是AJAX脚本的过程中,经常要取消掉对象的默认行为。比如说点击某个链接,就在特定的块里面显示另一个页面的内容,通常是这样来写:

HTML

  1. </p>
  2. <div id="ajax">
  3. </div>
  4. <a href="ajax.html" class="load"></a>

Jquery Js

  1. $('.load a').bind ('click', function() {
  2. var link = $(this).attr("href");
  3. $('#ajax').load(link);
  4. });

这样的确可以把ajax.html的内容填入到#ajax中,但脚本执行完之后,页面还是会跳转到ajax.html这个页面,这可不是我所希望的结果。最后,查资料得到解决的方法,在脚本执行后返回flase取消默认的行为并阻止事件起泡(bubbling up),最后的脚本如下:

  1. $('.load a').bind ('click', function() {
  2. var link = $(this).attr("href");
  3. $('#ajax').load(link);
  4. return false;
  5. });

另外,通过preventDefault() 方法只取消默认的行为,通过使用 stopPropagation() 方法只阻止一个事件起泡。

参考网址:http://api.jquery.com/bind/

阅读全文