zoukankan      html  css  js  c++  java
  • [转]最完美解决Nginx部署ThinkPHP项目的办法

    From : http://www.htmltec.com/archives/302

    网上通用解决方法的配置如下:

    server {
        location / {
            index  index.htm index.html index.php;
            #访问路径的文件不存在则重写URL转交给ThinkPHP处理
            if (!-e $request_filename) {
               rewrite  ^/(.*)$  /index.php/$1  last;
               break;
            }
        }
        location ~ .php/?.*$ {
            root        /var/www/html/website;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #加载Nginx默认"服务器环境变量"配置
            include        fastcgi.conf;
     
            #设置PATH_INFO并改写SCRIPT_FILENAME,SCRIPT_NAME服务器环境变量
            set $fastcgi_script_name2 $fastcgi_script_name;
            if ($fastcgi_script_name ~ "^(.+.php)(/.+)$") {
                set $fastcgi_script_name2 $1;
                set $path_info $2;
            }
            fastcgi_param   PATH_INFO $path_info;
            fastcgi_param   SCRIPT_FILENAME   $document_root$fastcgi_script_name2;
            fastcgi_param   SCRIPT_NAME   $fastcgi_script_name2;
        }
    }

    其实应该使用更简单的方法,fastcgi模块自带了一个fastcgi_split_path_info指令专门用来解决此类问题的,该指令会根据给定的正则表达式来分隔URL,从而提取出脚本名和path info信息,使用这个指令可以避免使用if语句,配置更简单。
    另外判断文件是否存在也有更简单的方法,使用try_files指令即可。

    server {
        location / {
            index  index.htm index.html index.php;
            #如果文件不存在则尝试TP解析
            try_files  $uri  /index.php$uri;
        }
        location ~ .+.php($|/) {
            root        /var/www/html/website;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
     
            #设置PATH_INFO,注意fastcgi_split_path_info已经自动改写了fastcgi_script_name变量,
            #后面不需要再改写SCRIPT_FILENAME,SCRIPT_NAME环境变量,所以必须在加载fastcgi.conf之前设置
            fastcgi_split_path_info  ^(.+.php)(/.*)$;
            fastcgi_param  PATH_INFO $fastcgi_path_info;
     
            #加载Nginx默认"服务器环境变量"配置
            include        fastcgi.conf;
        }
    }
  • 相关阅读:
    Web.config配置详解
    vs2010下创建webservice
    WinForm如何调用Web Service
    Android动画的两种使用方式。
    ES6介绍
    django批量form表单处理
    django生命周期示意图
    django中的构造字典(二级菜单,评论树,购物车)
    django中介模型,CBV模型,及logging日志配制
    django中csrftoken跨站请求伪造的几种方式
  • 原文地址:https://www.cnblogs.com/Athrun/p/nginx_thinkphp.html
Copyright © 2011-2022 走看看