nginx 一个server模块配置两个php后台项目

使用场景:

php作为后台对外提供api接口,前端vue项目通过https请求,获取php后台返回的数据进行展示,

老板要求相同域名下要对外提供两组接口,本来再一个php项目里面提供两组接口即可,但是两个项目针对不同的客户,必须拆分成两个项目,所以只能研究nginx进行不同的分发了

以前nginx配置如下:


server {
    listen       80 backlog=50000;
    server_name www.example.com;
    server_name 127.0.0.1;

    root   /home/www/projectA/index;
 
   location  / {
        index  index.html index.htm;
        add_header X-Location /;
        try_files $uri /index.php$is_args$args;
        fastcgi_pass   unix:/dev/shm/php73-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # pass the PHP scripts to FastCGI server listening on unix:/dev/shm/php73-fpm.sock;
    #
    location ~ \.php$ {
        root       /home/www/projectA/index;
        fastcgi_pass   unix:/dev/shm/php73-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        client_max_body_size  128m;
        include        fastcgi_params;
        limit_req zone=mylimit burst=22 nodelay;
    }
}

这中配置可以直接将http的请求分发到/home/www/projectA/index/index.php

以前的http请求是:http://www.example.com/biz_a/user/login

新项目新增的接口类似于;http://www.example.com/biz_b/user_login

接口名称几乎一直,实现方式略有不同,

于是新增一个location监听/biz_b即可

server {
    listen       80 backlog=50000;
    server_name www.example.com;
    server_name 127.0.0.1;

    root   /home/www/projectA/index;

    location /biz_cs/ {
       root       /home/www/projectB/index;
       fastcgi_pass   unix:/dev/shm/php73-fpm.sock;
       fastcgi_index  index.php;
       try_files      $uri /index.php$is_args$args /index.php$is_args$args;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       client_max_body_size  128m;
       include        fastcgi_params;
   }

   location  / {
        index  index.html index.htm;
        add_header X-Location /;
        try_files $uri /index.php$is_args$args;
        fastcgi_pass   unix:/dev/shm/php73-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # pass the PHP scripts to FastCGI server listening on unix:/dev/shm/php73-fpm.sock;
    #
    location ~ \.php$ {
        root       /home/www/projectA/index;
        fastcgi_pass   unix:/dev/shm/php73-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        client_max_body_size  128m;
        include        fastcgi_params;
        limit_req zone=mylimit burst=22 nodelay;
    }
}

/biz_a开头的请求还是和以前一样,不影响

/biz_b开头的请求会匹配到location /biz_b/ {}

遇到的问题是try_files; 直接从以前的配置复制出来的try_files只有两个参数,新增的try_files只使用两个参数的话不知道为什么匹配到以前的配置中去了,使用三个参数的话,就可以匹配了,尝试过$uri/index.php$is_args$args,/index.php.$is_args$args等,但是具体应该怎么设置没有深入了解,问题既然解决了就等以后再研究了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值