nginx的rewrite配置
ThinkPHP依赖 PATH_INFO进行路由,这个变量要添加进去(用fastcgi_param)。
三步:
- 添加重写判断。
- 修改php的location正则。
- 增加PATH_INFO变量。
server {
listen 9090;
server_name _;
#charset koi8-r;
access_log /opt/www/logs/access.log main;
error_log /opt/www/logs/error.log;
root /opt/www;
index index.php index.html index.htm;
location / {
index index.php index.html index.htm;
# URL重写
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}
# 将$修改为(/|$)
location ~ \.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
# 取path_info
set $fastcgi_script_name2 $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
set $fastcgi_script_name2 $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name2;
fastcgi_param PATH_INFO $path_info;
}
location ~ /\.ht {
deny all;
}
}