最近公司在搞lumen 框架,需要用到一个场景。
就是
XXX.com/apiv1/controller/action
XXX.com/apiv2/controller/action
XXX.com/apiv3/controller/action
以此类推
由于lumen 比较坑,必须用项目下的public 为 server_root.
本来我们有一个很懒的解决方案就是用无限的子域名堆出无限个的virtualhost.
apiv1.XXX.com/controller/action
apiv2.XXX.com/controller/action
apiv3.XXX.com/controller/action
不过后来想到域名生效、域名子域名数量限制等问题,就放弃了。
老老实实搞nginx 配置。
经过半天的折腾,完美解决。
nginx 配置如下:
效果如下:
不过这个配置有个缺点,就是如果url后面不跟任何东西的话,比如(http://haha.17ping.cn/sdk2) 就会找不到index.php.
一般这种页面 lumen 是用来打印版本信息的。
如果有人觉得强迫症不爽的话,直接从public 下面复制一个 index.php 到 上一级目录,然后修改14行的
$app = require __DIR__.'/../bootstrap/app.php';
改成
$app = require __DIR__.'/./bootstrap/app.php';
把导入的相对路径调成平级就行了。效果如下:
注:本文撰写实践时参考了一下链接:
https://stackoverflow.com/questions/37366484/is-it-possible-to-install-lumen-or-laravel-only-in-a-sub-directory
https://stackoverflow.com/questions/32186105/lumen-multisite-using-subdirectories-on-nginx
https://stackoverflow.com/questions/17805576/nginx-rewrite-in-subfolder
https://laracasts.com/discuss/channels/general-discussion/nginx-setup-for-subdirectory
https://gist.github.com/mreschke/27bfafb84add38d3bab8
https://stackoverflow.com/questions/45378367/lumen-application-workwith-trailing-slash-using-nginx-server
https://stackoverflow.com/questions/34823913/lumen-in-a-subfolder-trailing-slashes-issue
就是
XXX.com/apiv1/controller/action
XXX.com/apiv2/controller/action
XXX.com/apiv3/controller/action
以此类推
由于lumen 比较坑,必须用项目下的public 为 server_root.
本来我们有一个很懒的解决方案就是用无限的子域名堆出无限个的virtualhost.
apiv1.XXX.com/controller/action
apiv2.XXX.com/controller/action
apiv3.XXX.com/controller/action
不过后来想到域名生效、域名子域名数量限制等问题,就放弃了。
老老实实搞nginx 配置。
经过半天的折腾,完美解决。
nginx 配置如下:
server {
listen 80;
server_name haha.17ping.cn;
root /data/www/sdkHoster;
location / {
index index.php index.htm index.html;
autoindex on;
}
location /sdk1 {
root /data/www/sdkHoster;
index index.php index.htm index.html;
try_files $uri $uri/ /sdk1/public/index.php?$query_string;
}
location /sdk2 {
root /data/www/sdkHoster;
index index.php index.htm index.html;
try_files $uri $uri/ /sdk2/public/index.php?$query_string;
}
location ~ \.php$ {
set $newurl $request_uri;
if ($newurl ~ ^/sdk(\d+)(.*)$) {
set $newurl $2;
}
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
fastcgi_param REQUEST_URI $newurl;
}
location ~ \.(css|js|swf|php|htm|html)$ {
add_header Cache-Control no-store;
}
location ~ /\.ht {
deny all;
}
}
效果如下:
root@iZuf6c8x9fhxje4raqhanuZ:/data/www/sdkHoster# curl -X POST http://haha.17ping.cn/sdk1/account/index
{"code":0,"message":"OK","data":{"msg":"\u5982\u9047\u8d26\u53f7\u95ee\u9898\u8bf7\u8054\u7cfbQQ:115693753"}}
root@iZuf6c8x9fhxje4raqhanuZ:/data/www/sdkHoster# curl -X POST http://haha.17ping.cn/sdk2/account/index
{"code":0,"message":"OK","data":{"msg":"\u5982\u9047\u8d26\u53f7\u95ee\u9898\u8bf7\u8054\u7cfbQQ:115693753"}}
root@iZuf6c8x9fhxje4raqhanuZ:/data/www/sdkHoster#
不过这个配置有个缺点,就是如果url后面不跟任何东西的话,比如(http://haha.17ping.cn/sdk2) 就会找不到index.php.
一般这种页面 lumen 是用来打印版本信息的。
如果有人觉得强迫症不爽的话,直接从public 下面复制一个 index.php 到 上一级目录,然后修改14行的
$app = require __DIR__.'/../bootstrap/app.php';
改成
$app = require __DIR__.'/./bootstrap/app.php';
把导入的相对路径调成平级就行了。效果如下:
root@iZuf6c8x9fhxje4raqhanuZ:/data/www/sdkHoster# curl -X GET http://haha.17ping.cn/sdk1/
Lumen (5.5.2) (Laravel Components 5.5.*)
root@iZuf6c8x9fhxje4raqhanuZ:/data/www/sdkHoster# curl -X GET http://haha.17ping.cn/sdk2/
Lumen (5.5.2) (Laravel Components 5.5.*)
root@iZuf6c8x9fhxje4raqhanuZ:/data/www/sdkHoster#
注:本文撰写实践时参考了一下链接:
https://stackoverflow.com/questions/37366484/is-it-possible-to-install-lumen-or-laravel-only-in-a-sub-directory
https://stackoverflow.com/questions/32186105/lumen-multisite-using-subdirectories-on-nginx
https://stackoverflow.com/questions/17805576/nginx-rewrite-in-subfolder
https://laracasts.com/discuss/channels/general-discussion/nginx-setup-for-subdirectory
https://gist.github.com/mreschke/27bfafb84add38d3bab8
https://stackoverflow.com/questions/45378367/lumen-application-workwith-trailing-slash-using-nginx-server
https://stackoverflow.com/questions/34823913/lumen-in-a-subfolder-trailing-slashes-issue