Ubuntu下Nginx配置ThinkPHP的Pathinfo和URl Rewrite模式

2019-10-17 20:17:01王振洲


#如果请求既不是一个文件,也不是一个目录,则执行一下重写规则
    if (!-e $request_filename)
    {
            #地址作为将参数rewrite到index.php上。
            rewrite ^/(.*)$ /index.php/$1;
            #若是子目录则使用下面这句,将subdir改成目录名称即可。
            #rewrite ^/subdir/(.*)$ /subdir/index.php/$1;
    }

网友解决方案

location / {
                root /var/www;
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                try_files $uri $uri/ /index.html;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
                if (!-e $request_filename)
                {
                        rewrite ^/PHPParser/(.*)$ /PHPParser/index.php?s=$1 last;
                        break;
                }
        }

然后在localhost ~ .php{}配置栏目中添加如下两行:

fastcgi_split_path_info ^(.+.php)(.*)$;                            
fastcgi_param PATH_INFO $fastcgi_path_info;

完整配置如下:

location ~ .php$ {
                root /var/www;
                try_files $uri = 404;
                fastcgi_split_path_info ^(.+.php)(/.+)$;