thinkPHP下配置Nginx支持pathinfo及隐藏地址栏URL中index.php文件

每次配置nginx支持pathinfo及隐藏index.php都会查阅网上好多资料,才能配置成功。这次果断记录下来,以备后用。这是经过试验并且结合网上的方法。

试验环境:

服务器:阿里云

nginx版本:1.6

php版本:5.4

完整配置文件如下:


server {
listen 8080;
server_name localhost;

location / {
root /var/www;
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?s=/$1 last;
break;
}
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www;
}

location ~ \.php$ {
root /var/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;

set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;

index index.php index.html index.htm;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
}
}

其中实现nginx支持pathinfo的配置为:
set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;

index index.php index.html index.htm;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
实现URL地址栏中隐藏index.php文件:
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?s=/$1 last;
break;
}

thinkPHP下配置Nginx支持pathinfo及隐藏地址栏URL中index.php文件》有2个想法

新用户149656进行回复 取消回复

邮箱地址不会被公开。