zoukankan      html  css  js  c++  java
  • linux下配置Nginx,支持thinkphp

      

    前言引入

      一个刚入行的朋友,刚换工作,入职了一个新公司。新公司一个php开发,就是他。俨然老板把他当成公司扛把子了,把服务器都给了他,让他部署整个php的开发环境。那个朋友是wamp爱好者。然后面对linux服务器,我还是强烈建议他安装nginx。他同意了!内心应该不太愿意吧!

    遇到问题

      在nginx安装好后,把tp项目导入,发现本地好好的项目,线上不能使用。在找我解决之前,听说搞了好几天了,内心煎熬,手拍桌子头撞墙,瘦了六七斤,还是不能解决。

      问题有两个:

        第一个:apache的.htacess用来做rewrite的规则在nginx下无效。nginx的原始配置不支持隐藏入口文件index.php的重定向功能

        第二个:nginx不支持原始pathinfo。TP里面的U()方法不能使用了,返回了错误的路径值。

    解决

      第一个问题:

        重定向配置:有两种方法

        在nginx配置文件中添加如下配置

       

    location / {  
            index  index.htm index.html index.php;  
            #访问路径的文件不存在则重写URL转交给ThinkPHP处理  
            if (!-e $request_filename) {  
               rewrite  ^/(.*)$  /index.php/$1  last;  
               break;  
    } 
    

      

    还可以不写rewrite规则:

    location / {  
            index  index.htm index.html index.php;  
            #先尝试访问$uri  不存在在尝试访问/index.php$uri
          try_files $uri /index.php$uri; 
    }
    

      

    第二个问题:

      pathinfo配置:也有两个方法

      

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

    还能这么写:

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

      

    这样配置的话,得在入口文件先定义好常量,不要忘记了

          define('_PHP_FILE_',$_SERVER['SCRIPT_NAME']);

      

      

      

       

    朋友按照上面配置,终于解决了问题,眉开眼笑,眼看扛把子地位要坐稳了!!

  • 相关阅读:
    17.1.2.1 Advantages and Disadvantages of Statement-Based and Row-Based Replication
    17.1.2 Replication Formats
    Setting the Master Configuration on the Slave
    17.1.1.9 Introducing Additional Slaves to an Existing Replication Environment
    17.1.1.8 Setting Up Replication with Existing Data
    17.1.1.7 Setting Up Replication with New Master and Slaves
    17.1.1.6 Creating a Data Snapshot Using Raw Data Files
    列出display的值,并说明它们的作用
    CSS设置DIV居中
    CSS选择符有哪些?哪些属性可以继承?优先级算法如何计算?
  • 原文地址:https://www.cnblogs.com/zmfly/p/8710030.html
Copyright © 2011-2022 走看看