我如何在 Nginx 中提供嵌套项目
How can I serve nested projects in Nginx
我有一个 Lumen api 项目,其中包含用于 api 版本控制的多个 git 标签。所以我必须部署项目的多个签出。
服务器上的文件夹结构如下所示:
var
www
api-staging
master
v1
public
index.php
...
v2
public
index.php
...
lastest
public
index.php
...
...
现在我想通过 nginx 为项目提供服务,这样 url 看起来像这样。
http://BRANCH.domain.tld/VERSION/ eg. 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;
...
}
}
}
我有一个 Lumen api 项目,其中包含用于 api 版本控制的多个 git 标签。所以我必须部署项目的多个签出。
服务器上的文件夹结构如下所示:
var
www
api-staging
master
v1
public
index.php
...
v2
public
index.php
...
lastest
public
index.php
...
...
现在我想通过 nginx 为项目提供服务,这样 url 看起来像这样。
http://BRANCH.domain.tld/VERSION/ eg. 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;
...
}
}
}