随着项目不断迭代升级,功能越来越复杂,需要对接口实行版本管理,下面通过nginx的路由配置实现不同路由访问后端不同的项目。(后台使用的是laravel框架)
nginx 配置如下
server {
listen 80;
server_name www.xx_admin.com;
index index.html index.htm index.php;
#版本1
location ^~ /v1/ {
alias D:/workspace/v1/public/;
try_files $uri $uri/ @v1;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
location @v1{
rewrite /v1/(.*)$ /v1/index.php?/$1 last;
}
#版本2
location ^~ /v2/ {
alias D:/workspace/v2/public/;
try_files $uri $uri/ @v2;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
location @v2{
rewrite /v2/(.*)$ /v2/index.php?/$1 last;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
client_max_body_size 1000m;
}
这样配置后,请求地址 http://xx_admin.com/v1 访问的就是v1版本的项目, http://xx_admin.com/v2 访问的就是v2版本的项目。