我有一个 Lumen api 项目,其中包含多个用于 api 版本控制的 git 标签。所以我必须部署该项目的多次 checkout 。
服务器上的文件夹结构如下所示:
var
www
api-staging
master
v1
public
index.php
...
v2
public
index.php
...
lastest
public
index.php
...
...
现在我想通过 nginx 为项目提供服务,以便 url 看起来像这样。
http://BRANCH.domain.tld/VERSION/例如。 http://master.domain.tld/lastest/
我已经尝试了很多正则表达式,但没有任何效果。我希望你能帮助我。
最佳答案
您需要使用正则表达式 server_name
语句捕获 BRANCH
。请参阅this document了解更多。
根是通过将 /public
附加到捕获的 VERSION
来构造的,这需要正则表达式 location
和 alias
声明。请参阅this document了解更多。
例如:
server {
...
server_name ~^(?<branch>.+)\.domain\.tld$;
location ~ ^/(?<version>[^/]+)/(?<name>.*)$ {
alias /var/www/api-staging/$branch$version/public/$name;
if (!-e $request_filename) { rewrite ^ $version/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
...
}
}
}
关于regex - 如何在 Nginx 中提供嵌套项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45592289/