问题
I have a Lumen api project with multiple git tags for api versioning. So I have to deploy multiple checkouts of the project.
The folder structure on the server looks like this:
var
www
api-staging
master
v1
public
index.php
...
v2
public
index.php
...
lastest
public
index.php
...
...
Now I'd like to serve the projects via nginx so that the url looks something like this.
http://BRANCH.domain.tld/VERSION/ eg. http://master.domain.tld/lastest/
I have tried a lot with regexp, but nothing really worked. I hope you can help me out.
回答1:
You will need to capture the BRANCH
using a regular expression server_name
statement. See this document for more.
The root is constructed by appending /public
to the captured VERSION
, which requires a regular expression location
and an alias
statement. See this document for more.
For example:
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;
...
}
}
}
来源:https://stackoverflow.com/questions/45592289/how-can-i-serve-nested-projects-in-nginx