先简单说下需求吧:
前后端分离项目,一个域名可以访问所有的客户端,例如,我们的项目中前端有单独的访问域名,后端有单独的接口域名,我们的项目存在3个客户端,即小程序端
,boss后台管理端
,console客户端
,按照最简单的业务来解析域名,则需要6个域名。
由于种种原因吧,只能提供一个域名,所以就要借助Nginx的重定向或者rewrite功能;
大致流程和这里基本保持一致,点击查看Nginx单域名配置多Vue工程和PHP接口
先贴一下代码吧:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| server { listen 80; server_name btg.ma; index index.html index.htm index.php; location ^~ /boss/b { // 目录重定向 alias /Users/zhimma/Data/www/Btg_Base_Crs/boss-api/src/public/; try_files $uri $uri/ @bossBackend; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index /index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params; } } location @bossBackend { if (!-e $request_filename) { // 兼容路由模式-个人猜想 rewrite ^/boss/b/(.*)$ /boss/b/index.php?s=$1 last; rewrite /boss/b/(.*)$ /boss/b/index.php$is_args$args last; break; } } location ^~ /boss/f { alias /Users/zhimma/Data/www/Btg_Base_Crs/web/boss/; if (!-e $request_filename) { rewrite ^/boss/f/(.*) /boss/f/index.html last; break; } try_files $uri $uri/ @router; }
location ^~ /console/b { alias /Users/zhimma/Data/www/Btg_Base_Crs/console-api/src/public/; try_files $uri $uri/ @consoleBackend; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index /index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params; } } location @consoleBackend { if (!-e $request_filename) { rewrite ^/console/b/(.*)$ /console/b/index.php?s=$1 last; rewrite /console/b/(.*)$ /console/b/index.php$is_args$args last; break; } } location ^~ /console/f { alias /Users/zhimma/Data/www/Btg_Base_Crs/web/console/; if (!-e $request_filename) { rewrite ^/console/f/(.*) /console/f/index.html last; break; } try_files $uri $uri/ @router; }
location ^~ /crs/b { alias /Users/zhimma/Data/www/Btg_Base_Crs/crs-api/src/public/; try_files $uri $uri/ @crsBackend; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index /index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params; } } location @crsBackend { if (!-e $request_filename) { rewrite ^/crs/b/(.*)$ /crs/b/index.php?s=$1 last; rewrite /crs/b/(.*)$ /crs/b/index.php$is_args$args last; break; } } location ^~ /crs/f { alias /Users/zhimma/Data/www/Btg_Base_Crs/web/crs/; if (!-e $request_filename) { rewrite ^/crs/f/(.*) /crs/f/index.html last; break; } try_files $uri $uri/ @router; }
location @router { rewrite ~.*$ /index.html last; }
access_log /var/log/nginx/btg.ma.access.log main; error_log /var/log/nginx/btg.ma.error.log error; }
|
最终效果就成为这样子:
domain/origin/type
域名+管理端+前后端,f代表frontend
,b代表backend
boss:
前台页面:btg.ma/boss/f
后台接口地址:btg.ma/boss/b
console:
前台页面:btg.ma/console/f
后台接口地址:btg.ma/console/b
crs:
前台页面:btg.ma/crs/f
后台接口地址:btg.ma/crs/b
done;