在笔者的环境,设置documentRoot目录为:root/data0/htdocs/NetBeansProjects/_example/passport;设置php文件转发给fastcgi:location~.*\.(php|php5)?${#fastcgi_passunix:/tmp/php-cgi.sock;fa
在笔者的环境,设置documentRoot目录为:
root /data0/htdocs/NetBeansProjects/_example/passport;
设置php文件转发给fastcgi:
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
现在的要求是实现二级目录转发,这在apache中很简单,做个二级目录的alias即可:
alias /lowgame/ "
/data0/htdocs/NetBeansProjects/_example/lowgame/"
在
nginx中的对应写法是:
location /lowgame/ {
alias /data0/htdocs/NetBeansProjects/_example/lowgame/;
index index.html index.htm index.php;
}
至此,访问localhost/lowgame/ddd.jpg等非php文件即相当于访问/data0/htdocs/NetBeansProjects/_example/lowgame/ddd.jpg,这已经不成问题。现在很多人都会面临一个问题:我们想当然的希望/lowgame/*.php文件也提交给fastcgi解析,结果并非这样,当我访问localhost/lowgame/ddd.php的时候,fastcgi返回一个错误:
No input file specified.
ddd.php千真万确存在,权限也无问题,显然是fastcgi找不到文件,nginx实际上提交这个路径/data0/htdocs/NetBeansProjects/_example/passport/lowgame/ddd.php,当然找不到了。尝试添加以下代码:
location ~ /lowgame/(.*/)?.*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
但会先匹配到上面的location ~
.*\.(php|php5)?$,在网上搜了一番,没有一个中用的。后来想到apache的虚拟主机配置有个策略就是特定的二级域名写在前面,如果最后都没匹配到,最后再设置一个通用的匹配。nginx是不是也有这个特性呢?把location
~ /lowgame/(.*/)?.*\.(php|php5)?$代码段放到location ~
.*\.(php|php5)?$代码段的前面,终于搞定!
以下是我的nginx配置文件:
user www www;
worker_processes 2;
error_log /data0/logs/nginx_error.log crit;
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be
opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-Javascript text/css
application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /data0/htdocs;
#limit_conn crawler 20;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
log_format access ‘$remote_addr - $remote_user [$time_local]
“$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” $http_x_forwarded_for’;
access_log /data0/logs/access.log access;
location /status {
stub_status on;
access_log off;
#auth_basic “NginxStatus”;
#auth_basic_user_file conf/htpasswd;
}
}
server
{
listen 80;
server_name localpassport.nb;
index index.html index.htm index.php;
root /data0/htdocs/NetBeansProjects/_example/passport;
access_log /data0/logs/access.log access;
#limit_conn crawler 20;
# 以下的貌似注释掉也会自动加斜线
# optimize_server_names off; 此方法已不推荐使用
server_name_in_redirect off; ## 这两行及下面三行是为了让子目录自动加 /
if (-d $request_filename) {
# rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
location ~
^/(lowgame|lowproxy|lowadmin|passportadmin)/(.*\.php)$
{
alias /data0/htdocs/NetBeansProjects/_example/$1/$2;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ .*\.(php|php5)?$ {
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location /lowgame/ {
alias /data0/htdocs/NetBeansProjects/_example/lowgame/;
}
location /lowproxy/ {
alias /data0/htdocs/NetBeansProjects/_example/lowproxy/;
}
location /lowadmin/ {
alias /data0/htdocs/NetBeansProjects/_example/lowadmin/;
}
location /passportadmin/ {
alias /data0/htdocs/NetBeansProjects/_example/passportadmin/;
}
}
}
注:location的写法:
location = / {…} #等号精确匹配
location path {…} #路径名表示匹配所有以这开头的,因此/表示匹配所有
loation ~ regex {…} #~后接正则表达式,按正则匹配
location ^~ regex {…} #^即取非之意,应该是表示不匹配regex的时候,尝试用过,不太好用。比如:
server {
index index.html index.htm index.php;
location ~ ^/test/ {
index index.htm index.html index.php;
}
}
意思很好理解,访问localhost/test/时匹配location ~
^/test/的设置,即显示index.htm,如果是这样呢:
server {
index index.html index.htm index.php;
location ^~ ^/test/ {
index index.htm index.html index.php;
}
}
此时的意义显然不是“除test/目录以外的所有访问均匹配location ^~
^/test/设置”,笔者访问localhost/,仍然显示index.html,而不是index.htm。