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

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

    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;
        }
    }

    转载:http://blog.csdn.net/tinico/article/details/18033573

    修改thinkphp让他可以在nginx上运行

    最近在用thinkphp做一个项目,基本完成后部署到nginx服务器上才发觉nginx是不支持pathinfo的,网上搜索了别人的解决方法,有两种思路:

      1、修改thinkphp让他可以在nginx上运行

      2、修改nginx让它支持pathinfo

      网上说nginx开启pathinfo是有一定风险的,能不用pathinfo最好不用,所以还是折腾thinkphp吧,个人觉得这种方法相对第2种方法来得简单

      修改nginx的rewrite

    location / { 
    if (!-e $request_filename) { 
    rewrite ^(.*)$ /index.php?s=$1 last; 
    break; 
    } 
    }

      然后项目配置下url模式改为2

    'URL_MODEL'=>2,

    如果是多个项目,布署项目时要把项目布署到目录里,如后台的项目放到Admin目录里,那么在nginx的rewrite里再写一条

    location /Admin/ { 
    if (!-e $request_filename) { 
    rewrite ^/Admin/(.*)$ /Admin/index.php?s=$1 last; 
    break; 
    } 
    }

      最后也不要忘记把这个项目的url模式改为2。

    转载:http://www.3lian.com/edu/2012/12-14/49363.html

    Nginx环境
    在Nginx低版本中,是不支持PATHINFO的,但是可以通过在Nginx.conf中配置转发规则实现:

    location / { // …..省略部分代码
       if (!-e $request_filename) {
       rewrite  ^(.*)$  /index.php?s=$1  last;
       break;
        }
    }

    其实内部是转发到了ThinkPHP提供的兼容模式的URL,利用这种方式,可以解决其他不支持PATHINFO的WEB服务器环境。

    如果你的ThinkPHP安装在二级目录,Nginx的伪静态方法设置如下,其中youdomain是所在的目录名称。

    location /youdomain/ {
            if (!-e $request_filename){
                rewrite  ^/youdomain/(.*)$  /youdomain/index.php?s=$1  last;
            }
        }

    转载:http://doc.thinkphp.cn/manual/hidden_index.html

  • 相关阅读:
    超详细的 Vagrant 上手指南
    简单聊聊 Ironic
    什么是裸金属
    使用 minikube 快速上手 Kubernetes | v1.7.3
    来看看你对Python变量理解到位了没有
    python HelloWorld 的 4 种姿势,你知道几种
    Windows 系统安装 Python 3.8 详解
    myeclipse2017下载安装与破解详细教程
    eclipse中tomcat的add and Remove找不到项目
    cmd中查看MySQL数据库表数据及结构
  • 原文地址:https://www.cnblogs.com/wawahaha/p/4645658.html
Copyright © 2011-2022 走看看