使用场景:
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等,但是具体应该怎么设置没有深入了解,问题既然解决了就等以后再研究了