标签: nginx

robots.txt 404错误的解决

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

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

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

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

Nginx 通过FCGI支持Perl 脚本

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

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

$ eix fcgiwrap
[*] www-misc/fcgiwrap
     Available versions:  (~)1.0.2-r1 (**)9999
     Installed versions:  1.0.2-r1(22时48分47秒 2010年08月05日)
     Homepage:            ht tp://nginx.localdomain.pl/wiki/FcgiWrap
     Description:         Simple FastCGI wrapper for CGI scripts (CGI support for nginx)
$ sudo emerge fcgiwrapp

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

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

spawn-fcgi的配置文件

$ cat /etc/conf.d/spawn-fcgi.pl
# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/www-servers/spawn-fcgi/files/spawn-fcgi.confd,v 1.6 2009/09/28 08:38:02 bangert Exp $

# DO NOT MODIFY THIS FILE DIRECTLY! CREATE A COPY AND MODIFY THAT INSTEAD!

# The FCGI process can be made available through a filesystem socket or
# through a inet socket. One and only one of the two types must be choosen.
# Default is the inet socket.

# The filename specified by
# FCGI_SOCKET will be suffixed with a number for each child process, for
# example, fcgi.socket-1. 
# Leave empty to use an IP socket (default). See below. Enabling this, 
# disables the IP socket.
# 
FCGI_SOCKET=/var/run/fcgiwrap.sock

# When using FCGI_PORT, connections will only be accepted from the following
# address. The default is 127.0.0.1. Use 0.0.0.0 to bind to all addresses.
#
#FCGI_ADDRESS=127.0.0.1

# The port specified by FCGI_PORT is the port used
# by the first child process. If this is set to 1234 then subsequent child
# processes will use 1235, 1236, etc.
#
FCGI_PORT=

# The path to your FastCGI application. These sometimes carry the .fcgi
# extension but not always. For PHP, you should usually point this to
# /usr/bin/php-cgi.
#
FCGI_PROGRAM=/usr/sbin/fcgiwrap

# The number of child processes to spawn. The default is 1.
#
#FCGI_CHILDREN=1

# If you want to run your application inside a chroot then specify the
# directory here. Leave this blank otherwise.
#
#FCGI_CHROOT=

# If you want to run your application from a specific directiory specify
# it here. Leave this blank otherwise.
#
#FCGI_CHDIR=

# The user and group to run your application as. If you do not specify these,
# the application will be run as root:root.
#
FCGI_USER=nginx
FCGI_GROUP=nginx

# Additional options you might want to pass to spawn-fcgi
#
FCGI_EXTRA_OPTIONS="-M 0700"

# If your application requires additional environment variables, you may
# specify them here. See PHP example below.
#
ALLOWED_ENV="PATH"

Nginx配置文件

$ cat /etc/nginx/vhosts/example.com.conf
server {
    listen 127.0.0.1;
    server_name *.example.com example.com;
    access_log /var/log/nginx/localhost.access_log main;
    error_log /var/log/nginx/localhost.error_log info;
    root /var/www/example.com;

    location ~ .*.php$ {
	include /etc/nginx/fastcgi.conf;
	fastcgi_pass 127.0.0.1:1234;
	fastcgi_param  SCRIPT_FILENAME  /var/www/example.com/$fastcgi_script_name;
	fastcgi_index index.php;
    }
    
    location ~ .*.pl$ {
	include /etc/nginx/fastcgi_params;
	fastcgi_pass unix:/var/run/fcgiwrap.sock-1;
	fastcgi_param  SCRIPT_FILENAME  /var/www/example.com/$fastcgi_script_name;
	fastcgi_index index.pl;
    }

这里要注意的是,网上很多教程和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; 否则,就会出现文件找不到的错误,我在这里花了不少的时间才明白这个事情。

最后,启动相关服务:

$ sudo /etc/init.d/spawn-fcgi.pl start
$ 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不同。

Gentoo下安装与配置Nginx

####1、安装Nginx

为Nginx加入fastcgi(用来支持PHP)和ssl(支持https加密链接)USE标记:

echo "www-servers/nginx fastcgi ssl" >> /etc/portage/package.use

然后emerge nginx:

emerge -av nginx

####2、配置Nginx

Nginx的配置文件位于 /etc/nginx/下。(/etc/nginx/nginx.conf是主要的配置文件)

通常需要修改的是服务器配置部分:

/etc/nginx/nginx.conf

server {
listen          80;
server_name     www.example.com;
access_log      /var/log/nginx/www.example.com.access_log main;
error_log       /var/log/nginx/www.example.com.error_log info;
root /var/www/www.example.com/htdocs;
}

####配置 Fast CGI

如果站点是只有html的静态文件,可以不用CGI支持,如果你想使用动态脚本(如PHP),则要通过CGI来实现。Nginx并不直接支持CGI,因此需要安装一个辅助程序将CGI的输出结果返回给Nginx,这里使用spawn-fcgi。

spawn-fcgi还未进入稳定分支,因此要先加入keywords

echo www-servers/spawn-fcgi ~amd64 >> /etc/portage/package.keywords

然后就可以emerge它了:

emerge spawn-fcgi

安装PHP并加入cgi和force-cgi-redirect支持。

echo dev-lang/php cgi force-cgi-redirect >> /etc/portage/package.use
emerge php

为Spawn-fcgi创建一个启动脚本链接

ln -sf /etc/init.d/spawn-fcgi /etc/init.d/spawn-fcgi.php

创建spawn-fcgi.php配置文件:

cp /etc/conf.d/spawn-fcgi /etc/conf.d/spawn-fcgi.php

配置文件内容:

/etc/conf.d/spawn-fcgi.php

# Copyright 1999-2004 Gentoo Foundation

# Distributed under the terms of the GNU General Public License v2

# $Header: /var/cvsroot/gentoo-x86/www-servers/lighttpd/files/spawn-fcgi.confd,v 1.1 2005/02/14 11:39:01 ka0ttic Exp
# $Configuration file for the FCGI-Part of /etc/init.d/lighttpd

## Set this to "yes" to enable SPAWNFCGI
ENABLE_SPAWNFCGI="yes"

## ABSOLUTE path to the spawn-fcgi binary
SPAWNFCGI="/usr/bin/spawn-fcgi"

## ABSOLUTE path to the PHP binary
FCGI_PROGRAM="/usr/bin/php-cgi"

## bind to tcp-port on localhost
FCGI_PORT="65532"

## number of PHP childs to spawn
PHP_FCGI_CHILDREN=5

## number of request server by a single php-process until is will be restarted
PHP_FCGI_MAX_REQUESTS=1000

## IP adresses where PHP should access server connections from
FCGI_WEB_SERVER_ADDRS="127.0.0.1"

# allowed environment variables sperated by spaces
ALLOWED_ENV="PATH USER"

# do NOT change line below
ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS FCGI_WEB_SERVER_ADDRS"

## if this script is run as root switch to the following user
USERID=nginx
GROUPID=nginx

通常要配置的选项是 FCGI_PROGRAM, FCGI_PORT, USERID and GROUPID。

FCGI_PROGRAM : CGI程序的绝对路径。

FCGI_PORT : Nginx监听的端口,可以随便定义,注意不要和其它服务有冲突。

USERID 和 GROUPID :以什么用户和用户组来运行。

配置好后就可以启动这个服务和把它加入到开机自动运行。

/etc/init.d/spawn-fcgi.php start
rc-update add spawn-fcgi default

在Nginx的配置文件加入fastcgi支持,加入以下内容:

/etc/nginx/nginx.conf

index index.php index.html index.htm default.html default.htm
location ~ .*.php$ {
    include /etc/nginx/fastcgi.conf;
    fastcgi_pass  127.0.0.1:65532;
    fastcgi_index index.php;
}

fastcgi_pass 是设置监听 spawn-fcgi 的地址和端口,注意这里的端口应该跟刚才在/etc/conf.d/spawn-fcgi.php里设置的一致。

启动Nginx服务:

/etc/init.d/nginx start

或者把Nginx加入开机启动:

rc-update add nginx default

至此,Nginx的安装完成。

参考:
Gentoo wiki