让nginx支持php和path_info

默认情况下,nginx是不支持php和path_info的,我们需要做些配置让它支持,关于nginx的其他配置信息不再复述,只来说下server中的设置。

    server{
        listen 80;
        server_name 127.0.0.1;
        root /web/project;
        location / {
            index index.php;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_split_path_info  ^(.+\.php)(.*)$;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO $fastcgi_path_info;
            include fastcgi_params;
        }
    }
  • 首先来说下root,在server里面可以定义root,在location里面也可以定义root,区别在于定义在location里面的root不可以作用于其他的location中,$document_root指的是定义在server中的root,如果未定义,默认为nginx安装目录下的html文件夹。
  • 如果只是单纯的想要支持php,location可以简化为如下
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

但是如果要支持path_info,就需要更改配置
1. ~ \.php改为~ \.php(.*),因为要接收.php后面的参数,不能让它被当做目录处理。
2. 添加fastcgi_split_path_info,该参数后面需指定正则表达式,而且必须要有两个捕获,第一个捕获将会重新赋值给$fastcgi_script_name,第二个捕获将会重新赋值给$fastcgi_path_info。
3. 添加fastcgi_param PATH_INFO,值为$fastcgi_path_info。
更多关于nginxfastcgi的知识可以点击上述网址。

猜你喜欢

转载自blog.csdn.net/a7442358/article/details/81911915
今日推荐