nginx的SCRIPT_NAME, PATH_INFO多了index.php问题

在使用nginx进行 反向代理配置时通过正则表达式配置location后发现

通过$_SERVER['SCRIPT_NAME']; 与$_SERVER['PATH_INFO'] 发现直接获取了整个uri地址

访问uri: 127.0.0.1/index.php/site/login

正常情况应该为index.php

先获取为index.php/site/login

以下是解决方法

server { 
listen 80; 
server_name test.com; 
 
access_log /home/swin/oaih/dev/logs/access.log; 
error_log /home/swin/oaih/dev/logs/error.log; 
 
root /home/swin/oaih/dev/public; 
index index.php index.html index.htm; 
 
location / { 
if (!-e $request_filename) { 
rewrite ^/(.*)$ /index.php/$1 last; 
} 
} 
 
location ~ .php$ { 
fastcgi_pass 127.0.0.1:9000; 
fastcgi_index index.php; 
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
 
include /etc/nginx/fastcgi_params; 
} 
 
location ~ .php($|/) { 
 
set $script $uri; 
set $path_info ""; 
 
if ($uri ~ "^(.+.php)(/.+)") { 
set $script $1; 
set $path_info $2; 
} 
 
fastcgi_pass 127.0.0.1:9000; 
fastcgi_index index.php; 
fastcgi_param SCRIPT_FILENAME $document_root$script; 
fastcgi_param SCRIPT_NAME $script; 
fastcgi_param PATH_INFO $path_info; 
 
fastcgi_param QUERY_STRING $query_string; 
fastcgi_param REQUEST_METHOD $request_method; 
fastcgi_param CONTENT_TYPE $content_type; 
fastcgi_param CONTENT_LENGTH $content_length; 
 
fastcgi_param REQUEST_URI $request_uri; 
fastcgi_param DOCUMENT_URI $document_uri; 
fastcgi_param DOCUMENT_ROOT $document_root; 
fastcgi_param SERVER_PROTOCOL $server_protocol; 
 
fastcgi_param GATEWAY_INTERFACE CGI/1.1; 
fastcgi_param SERVER_SOFTWARE nginx; 
 
fastcgi_param REMOTE_ADDR $remote_addr; 
fastcgi_param REMOTE_PORT $remote_port; 
fastcgi_param SERVER_ADDR $server_addr; 
fastcgi_param SERVER_PORT $server_port; 
fastcgi_param SERVER_NAME $server_name; 
fastcgi_param REDIRECT_STATUS 200; 
 
} 
 
}

第一个位置指令设立的基本选项,而第二个location指令匹配一个请求到自身的index.php,并把它传递到FastCGI来处理。下一次请求index.php是没有进行正确的配置。

在处理我们的问题的整个配置文件的关键部分是这样的:

location ~ .php($|/) { 
 
set $script $uri; 
set $path_info ""; 
 
if ($uri ~ "^(.+.php)(/.+)") { 
set $script $1; 
set $path_info $2; 
} 
 
fastcgi_pass 127.0.0.1:9000; 
fastcgi_index index.php; 
fastcgi_param SCRIPT_FILENAME $document_root$script; 
fastcgi_param SCRIPT_NAME $script; 
fastcgi_param PATH_INFO $path_info; 
}

location指定匹配所有的php请求将不指定为先前的全部uri, 它还清空$path-info变量, if语句匹配任何类似下面的请求:http://mysite/index.php/foo和放入的index.php的$script变量和/ foo $path_info变量。

$script and $path_info将被插入到服务器变量SCRIPT_NAME和PATH_INFO的fastcgi的参数,并允许PHP妥善处理请求。

相关推荐