thinkphp3.2在ubantu上的nginx服务器下,path_info模式的部署

本文详细介绍了如何配置Nginx以实现Pathinfo模式,并通过正则表达式匹配不同子域名,将请求转发至不同的PHP-FPM实例。同时,深入探讨了如何设置变量传递、FastCGI参数及如何重启Nginx和PHP-FPM服务。

要想是pathinfo模式的话,首先需要再配置文件中设置:'URL_MODEL'=>1,

然后,ngixn配置文件为:

server {
        listen 80;
        server_name *.mobissp.com;
        
        if ($http_host ~* "^(.*?)\.mobissp\.com$") {    #正则表达式
                set $domain $1;                     #设置变量
        }
        
        
        location / {
            if ($domain ~* "ads") {
               proxy_pass http://127.0.0.1:9001;      #域名中有ads,转发到9001端口
            }
            if ($domain ~* "dashboard") {
               proxy_pass http://127.0.0.1:9002;      #域名中有dashboard,转发到9002端口
            }
 
            tcp_nodelay     on;
            proxy_set_header Host            $host;
            proxy_set_header X-Real-IP       $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #以上三行,目的是将代理服务器收到的用户的信息传到真实服务器上
            
            root   html;
            index  index.html index.htm;            #默认情况
        }
    }

 

server {
        listen 9001;
        server_name 127.0.0.1;
        index index.php;
        root /data/project/;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        #include /mnt/project/.htaccess;
        
        location / {
                #index index.php;
                #ThinkPHP REWRITE支持
                #if (!-e  $request_filename) {
                #    rewrite ^/(.*)$ /index.php?s=$1 last;
                #}
                
                if (!-e $request_filename) {
                   #rewrite  ^(.*)$  /index.php?s=$1  last;
                   rewrite  ^/(.*)$  /index.php/$1  last;
                }
       }
        
        location ~ \.php {
            root /data/project/;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            
            set $path_info "";
            set $real_script_name $fastcgi_script_name;
            if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                set $real_script_name $1;
                set $path_info $2;
            }
            
            fastcgi_param SCRIPT_NAME $real_script_name;
            fastcgi_param PATH_INFO $path_info;    
        }
    }

 

server {
        listen 9002;
        server_name 127.0.0.1;
        #server_name dashboard.mobissp.com;
        index index.php;
        root /data/mobissp/mobissp_cmn/;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        #include /mnt/project/.htaccess;
    access_log /data/log/nginx/access.log;
    error_log /data/log/nginx/error.log;
        
        location / {                
                if (!-e $request_filename) {
                   #rewrite  ^(.*)$  /index.php?s=$1  last;
                   rewrite  ^/(.*)$  /index.php/$1  last;
                }
       }
        
        location ~ \.php {
            root /data/mobissp/mobissp_cmn/;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            
            set $path_info "";
            set $real_script_name $fastcgi_script_name;
            if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                set $real_script_name $1;
                set $path_info $2;
            }
            
            fastcgi_param SCRIPT_NAME $real_script_name;
            fastcgi_param PATH_INFO $path_info;    
        }
    }

重启ngixn和php-fpm(php-fpm也一定要重启,否则配置有可能会无效);

关于php-fpm的重启方法:百度的资料如下:

  • php中php-fpm的重启、终止操作命令:查看php运行目录命令:whichphp/usr/bin/php查看php-fpm进程数:psaux|grep-cphp-fpm查看运行内存/usr/bin/php -i|grepmem重启php-fpm/etc/init.d/php-fpmrestart在phpinfo()输出内容可以看到php相关配置。LoadedConfigurationFile /etc/php.ini==================
  • php 中php-fpm 的重启、终止操作命令:

    查看php运行目录命令:
    which php
    /usr/bin/php

    查看php-fpm进程数:
    ps aux | grep -c php-fpm

    查看运行内存
    /usr/bin/php  -i|grep mem

    重启php-fpm
    /etc/init.d/php-fpm restart

    在phpinfo()输出内容可以看到php相关配置。
    Loaded Configuration File /etc/php.ini

    ==============================

    首先要找到php-fpm.conf配置文件,查看pid的配置路径(不是安装路径),然后把下面对应的地方改掉才能正常执行。

    [aliyunzixun@xxx.com ~]# ps aux | grep php-fpm   
    root     11799  0.0  0.0 103248   880 pts/0    S+   13:51   0:00 grep --color php-fpm
    root     11973  0.0  0.0 417748   964 ?        Ss   Jun01   0:20 php-fpm: master process (/etc/php-fpm.conf)

    cat /etc/php-fpm.conf
    看到
    pid = /var/run/php-fpm/php-fpm.pid

    php-fpm 启动:
    /usr/local/php/sbin/php-fpm
    php-fpm 关闭:
    kill -INT `cat /var/run/php-fpm/php-fpm.pid`
    php-fpm 重启:
    kill -USR2 `cat /var/run/php-fpm/php-fpm.pid`

    查看php-fpm进程数:
    ps aux | grep -c php-fpm

    =============================

    [aliyunzixun@xxx.com ~]# find / -name 'php-fpm' -type d
    /var/log/php-fpm
    /var/run/php-fpm

    用这个find命令查找出来的路径是不对的

     which php
    /usr/bin/php

     

    shell脚本重启:

    #!/bin/bash
    # @create by jbxie 2014-02-10
    cat /opt/php-5.3.6p2/var/run/php-fpm.pid |xargs kill -USR2

     

     

    shell脚本暂停:

    #!/bin/bash
    # @create by jbxie 2014-02-10
    cat /opt/php-5.3.6p2/var/run/php-fpm.pid |xargs kill -QUIT

ThinkPHP5 中,`__APP__` 常量的功能可以通过 `request()` 对象来实现等效功能。具体来说,在 ThinkPHP3.2 中,`__APP__` 表示当前应用的 URL 地址(不包含域名)。而在 ThinkPHP5 中,可以使用 `\think\Request::instance()->domain().\think\Request::instance()->baseUrl(true)` 来获取类似的路径。 以下是具体的实现方式: ```php // 获取当前应用的基础URL (类似于 __APP__) $appUrl = \think\Request::instance()->domain() . \think\Request::instance()->baseUrl(true); ``` 此代码片段通过组合请求对象的方法实现了与 ThinkPHP3.2 的 `__APP__` 类似的功能[^1]。其中: - `\think\Request::instance()->domain()` 返回当前站点的域名部分。 - `\think\Request::instance()->baseUrl(true)` 返回应用程序的基础路径,并自动排除入口文件名。 如果需要更简洁的方式,可以直接调用 `app()` 辅助函数访问 Request 实例: ```php $request = app(&#39;http&#39;)->request; $appUrl = $request->domain() . $request->baseUrl(true); ``` 这种写法更加现代化且符合 ThinkPHP5 的设计风格[^2]。 ### 示例代码 以下是一个完整的例子展示如何替换 `__APP__` 常量: ```php <?php namespace app\index\controller; use think\Controller; class Index extends Controller { public function index() { // 替代 __APP__ 常量的实现 $appUrl = request()->domain() . request()->baseUrl(true); return "Current App Url: " . $appUrl; } } ``` 在此示例中,`$appUrl` 将保存当前应用的基础 URL 路径,其效果相当于 ThinkPHP3.2 中的 `__APP__` 常量[^3]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值