nginx用于thinkphp的rewrite

原料:已经搭配好的lnmp环境,写好的基于thinkphp的网站。

问题:提示“file not found或者404”。

解决方案:

sudo vim /etc/nginx/sites-avalaible/default

配置如下即可:

 

  location / {
        #如果文件不存在则尝试TP解析  
        #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则
        if (!-e $request_filename)
        {
            #地址作为将参数rewrite到index.php上。
            rewrite ^/(.*)$ /index.php/$1;
        }
    }

    location ~ .php{
        index index.html index.htm index.php;
        #定义变量 $path_info ,用于存放pathinfo信息
        set $path_info "";
        #定义变量 $real_script_name,用于存放真实地址
        set $real_script_name $fastcgi_script_name;
        #如果地址与引号内的正则表达式匹配
        if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
            #将文件地址赋值给变量 $real_script_name
            set $real_script_name $1;
            #将文件地址后的参数赋值给变量 $path_info
            set $path_info $2;

        }
        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        #配置fastcgi的一些参数
        fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
        fastcgi_param SCRIPT_NAME $real_script_name;
        fastcgi_param PATH_INFO $path_info;
    }

主要是对/和php的重写!

posted @ 2017-01-21 12:59  devilyouwei  阅读(265)  评论(0编辑  收藏  举报