邢栋博客

邢栋博客,Action博客,记录工作和生活中的点点滴滴

最完美 ThinkPHP(tp) 的 Nginx 配置文件

===系统===
LSB Version:    :....(太长了)
Distributor ID: CentOS
Description:    CentOS Linux release 8.2.2004 (Core) 
Release:        8.2.2004
Codename:       Core


===使用官网给的没有生效===

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

===于是===

server {
    listen       80;
    server_name  mydomain.com;
    access_log /data/logs/access_nginx.log combined;
    root /data/www/test_sdk/public;
    index  index.html index.htm index.php;    
    location / {
        try_files $uri @rewrite;
    }
    
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }

    location @rewrite {
        set $static 0;
        if  ($uri ~ \.(css|js|jpg|jpeg|png|gif|ico|woff|eot|svg|css\.map|min\.map)$) {
            set $static 1;
        }
        if ($static = 0) {
            rewrite ^/index.php(.*)$ /index.php?s=/$1 last;
        }
    }
    location ~ \.php/ {
       if ($request_uri ~ ^(.+\.php)(/.+?)($|\?)) { }
       #fastcgi_pass remote_php_ip:9000;
       fastcgi_pass unix:/dev/shm/php-cgi.sock;
       include fastcgi_params;
       fastcgi_param SCRIPT_NAME     $1;
       fastcgi_param PATH_INFO       $2;
       fastcgi_param SCRIPT_FILENAME $document_root$1;
    }
    location ~ \.php$ {
        #fastcgi_pass remote_php_ip:9000;
        fastcgi_pass unix:/dev/shm/php-cgi.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) {
      deny all;
    }
}



===参考链接===

https://www.kancloud.cn/manual/thinkphp6_0/1037488

https://www.seasidecrab.com/tp/487.html

http://www.thinkphp.cn/topic/34380.html


thinkphp的nginx重写兼容配置
 if (!-e $request_filename) {
 rewrite ^/index.php(.*)$ /index.php?s=$1 last;
 rewrite ^(.*)$ /index.php?s=$1 last;
 break;
 }
thinkphp3.2同时支持mysql和mongodb

1.如果只是支持mysql,就不用说了,先说说只支持mongodb吧

在config.php中

<?php
return array(
//'配置项'=>'配置值'
'DB_TYPE'=>'mongo',
'DB_HOST'=> '127.0.0.1', 
'DB_USER'=>'',      
'DB_PWD'=>'',        
'DB_PORT'=>'27017',
'DB_NAME'=> 'test',     
 'DB_PREFIX'=> '',

);

新建一个TestModel.class.php文件

<?php
namespace Home\Model;
use Think\Model\MongoModel;
Class TestModel extends MongoModel{

}
?>

然后在控制器中 

$user =  D("users")->select();

var_dump($user);

就可以了。


2.同时支持mysql和mongodb

测试后发现只能以mogodb为主是可以使用的。

在config.php配置文件中

<?php
return array(
//'配置项'=>'配置值'
'DB_TYPE'=>'mongo',
'DB_HOST'=> '127.0.0.1', 
'DB_USER'=>'',      
'DB_PWD'=>'',        
'DB_PORT'=>'27017',
'DB_NAME'=> 'test',     
 'DB_PREFIX'=> '',

'DB_MYSQL' => array(
'DB_TYPE' => 'mysql', // 数据库类型
'DB_HOST' => 'localhost', // 服务器地址
'DB_NAME' => 'test, // 数据库名
'DB_USER' => 'root', // 用户名
'DB_PWD' => '1314', // 密码
'DB_PORT' => 3306, // 端口
'DB_PREFIX' => 'action_' // 数据库表前缀
),

);


然后mongodb的调用方法不变,mysql调用方法

mysql的可以这样:
$user = M('user','action_','DB_MYSQL')->select();
var_dump($user):


手册介绍地址:http://document.thinkphp.cn/manual_3_2.html#connect_db

让nginx支持ThinkPHP3.2.2的URL重写并可通过网址访问
server {
    listen       80;
    server_name  www.xingdong365.com;


    location / {
        #root   /usr/share/nginx/html;
        root    /data/www/xingdong365/;
        index  index.html index.htm index.php;
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php/$1;
            break;   
    }
    }


    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

 
    location ~ \.php {
        root /data/www/xingdong365/ ;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include        fastcgi_params;
        
           #定义变量 $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;
                }
                #配置fastcgi的一些参数
                fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
                fastcgi_param SCRIPT_NAME $real_script_name;
                fastcgi_param PATH_INFO $path_info;
    }
    
}

nginx下支持thinkphp3.1.3的url重写
nginx下支持thinkphp的url重写,项目分组后localhost://project/index.php/Admin

//二级目录
location /project/ { 
index index.php; 
if (!-e $request_filename) { 
rewrite ^/project/(.*)$ /project/index.php/$1 last; 
break; 



location ~ .+.php($|/) { 
set $script $uri; 
set $path_info "/"; 
if ($uri ~ "^(.+.php)(/.+)") { 
set $script $1; 
set $path_info $2; 


fastcgi_pass 127.0.0.1:9000; 
fastcgi_index index.php?IF_REWRITE=1; 
include fastcgi_params; 
fastcgi_param PATH_INFO $path_info; 
fastcgi_param SCRIPT_FILENAME $document_root/$script; 
fastcgi_param SCRIPT_NAME $script; 


优惠券
最新微语