账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    nginx配置php接口多版本,不同版本指向不同目录
    34
    0

    问一个nginx配置php的问题

    server {
        listen       80;
        server_name t.timophp.com;
    
        charset utf-8;
        access_log off;
        
        location /v1 {
            root /data/a/b;
            index   index.php index.html index.htm;
            if (!-e $request_filename) {
                rewrite ^/v1/(.*)$ /index.php/$1 last;
            }
        }
    
        location /v2/ {
            root /data/c/d;
            index   index.php index.html index.htm;
            if (!-e $request_filename) {
                rewrite ^/v2/(.*)$ /index.php/$1 last;
            }
        }
    
        location ~ \.php(/|$) {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            fastcgi_pass   unix:/dev/shm/php-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO        $fastcgi_path_info;
            include        fastcgi_params;
        }
        
        location ~ /\.ht {
            deny  all;
        }
    }

    一个域名下面配置多个版本接口
    版本一

    路径/data/a/b

    版本二

    路径/data/c/d

    版本三

    路径/data/e/f
    

    按照我的配置行不通

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 画船听雨眠~ 普通会员 1楼

      在Nginx中配置PHP接口多版本,不同版本指向不同目录,可以通过定义多个location块,并使用URL前缀或者header信息等方式进行区分。以下是一个示例:

      ```nginx server { listen 80; server_name your.domain.com;

      # PHP7.4版本的接口
      location /api/v1/php74/ {
          alias /var/www/html/api-php74/;
          try_files $uri $uri/ /index.php$is_args$args;
      
          location ~ \.php$ {
              fastcgi_pass   127.0.0.1:9074; # 假设PHP7.4 FPM监听这个端口
              fastcgi_index  index.php;
              fastcgi_param  SCRIPT_FILENAME $request_filename;
              include        fastcgi_params;
          }
      }
      
      # PHP7.3版本的接口
      location /api/v1/php73/ {
          alias /var/www/html/api-php73/;
          try_files $uri $uri/ /index.php$is_args$args;
      
          location ~ \.php$ {
              fastcgi_pass   127.0.0.1:9073; # 假设PHP7.3 FPM监听这个端口
              fastcgi_index  index.php;
              fastcgi_param  SCRIPT_FILENAME $request_filename;
              include        fastcgi_params;
          }
      }
      

      } ```

      在这个配置中,访问your.domain.com/api/v1/php74/下的接口会使用PHP7.4版本,对应的文件位于 /var/www/html/api-php74/ 目录下;访问 your.domain.com/api/v1/php73/ 下的接口则会使用PHP7.3版本,对应文件位于 /var/www/html/api-php73/ 目录下。

      注意:实际配置时,请确保你已经正确安装并启动了不同版本的PHP-FPM服务,并且它们监听在正确的端口上。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部